Skip to content

Commit

Permalink
Add output-env option
Browse files Browse the repository at this point in the history
Signed-off-by: Curtis Vogt <[email protected]>
  • Loading branch information
omus committed Jan 15, 2025
1 parent 8e1d546 commit a28e9ed
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,31 @@ jobs:
DOCKER_METADATA_OUTPUT_ANNOTATIONS
DOCKER_METADATA_OUTPUT_JSON
no-output-env:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Docker meta
id: meta
uses: ./
with:
images: |
${{ env.DOCKER_IMAGE }}
ghcr.io/name/app
labels: |
maintainer=CrazyMax
annotations: |
maintainer=Foo
output-env: false
-
name: No output environment variables set
shell: bash
run: |
[[ "$(printenv | grep "^DOCKER_METADATA_OUTPUT_" | wc -l)" -eq 0 ]] || exit 1
bake-annotations:
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ The following inputs can be used as `step.with` keys:
| `sep-labels` | String | Separator to use for labels output (default `\n`) |
| `sep-annotations` | String | Separator to use for annotations output (default `\n`) |
| `bake-target` | String | Bake target name (default `docker-metadata-action`) |
| `output-env` | Bool | If `true`, sets each output as an environmental variable (default `true`) |

### outputs

Expand All @@ -319,7 +320,7 @@ The following outputs are available:
| `bake-file-labels` | File | [Bake file definition](https://docs.docker.com/build/bake/reference/) path with labels |
| `bake-file-annotations` | File | [Bake file definition](https://docs.docker.com/build/bake/reference/) path with [annotations](https://github.com/moby/buildkit/blob/master/docs/annotations.md) |

Alternatively, each output is also exported as an environment variable:
Alternatively, each output is also exported as an environment variable when `output-env` is `true`:

* `DOCKER_METADATA_OUTPUT_VERSION`
* `DOCKER_METADATA_OUTPUT_TAGS`
Expand Down
3 changes: 3 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('getInputs', () => {
sepTags: '\n',
sepAnnotations: '\n',
tags: [],
outputEnv: true,
} as Inputs
],
[
Expand All @@ -70,6 +71,7 @@ describe('getInputs', () => {
sepTags: ',',
sepAnnotations: ',',
tags: [],
outputEnv: true,
} as Inputs
],
[
Expand All @@ -89,6 +91,7 @@ describe('getInputs', () => {
sepTags: '\n',
sepAnnotations: '\n',
tags: [],
outputEnv: true,
} as Inputs
],
])(
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ inputs:
bake-target:
description: 'Bake target name (default docker-metadata-action)'
required: false
output-env:
description:
default: 'true'
required: true
github-token:
description: 'GitHub Token as provided by secrets'
default: ${{ github.token }}
Expand Down
2 changes: 2 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Inputs {
sepLabels: string;
sepAnnotations: string;
bakeTarget: string;
outputEnv: boolean;
githubToken: string;
}

Expand All @@ -35,6 +36,7 @@ export function getInputs(): Inputs {
sepLabels: core.getInput('sep-labels', {trimWhitespace: false}) || `\n`,
sepAnnotations: core.getInput('sep-annotations', {trimWhitespace: false}) || `\n`,
bakeTarget: core.getInput('bake-target') || `docker-metadata-action`,
outputEnv: (core.getInput('output-env') || 'true') === 'true',
githubToken: core.getInput('github-token')
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ actionsToolkit.run(
const toolkit = new Toolkit({githubToken: inputs.githubToken});
const context = await getContext(inputs.context, toolkit);
const repo = await toolkit.github.repoData();
const setOutput = inputs.outputEnv ? setOutputAndEnv : core.setOutput;

await core.group(`Context info`, async () => {
core.info(`eventName: ${context.eventName}`);
Expand Down Expand Up @@ -105,7 +106,7 @@ actionsToolkit.run(
}
);

function setOutput(name: string, value: string) {
function setOutputAndEnv(name: string, value: string) {
core.setOutput(name, value);
core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value);
}

0 comments on commit a28e9ed

Please sign in to comment.