Skip to content

Commit 6729545

Browse files
committed
Provide outputs as env vars
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
1 parent 05d22bf commit 6729545

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

.github/workflows/ci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ jobs:
3737
type=ref,event=tag
3838
type=ref,event=pr
3939
type=sha
40+
-
41+
name: Print envs
42+
run: env|sort
4043

4144
tag-schedule:
4245
runs-on: ubuntu-latest
@@ -224,6 +227,14 @@ jobs:
224227
echo "version=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}"
225228
echo "revision=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}"
226229
echo "created=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}"
230+
-
231+
name: JSON build arg
232+
uses: docker/build-push-action@v3
233+
with:
234+
context: ./test
235+
file: ./test/json.Dockerfile
236+
build-args: |
237+
BUILDINFO=${{ steps.meta.outputs.json }}
227238
228239
docker-push:
229240
runs-on: ubuntu-latest
@@ -350,3 +361,31 @@ jobs:
350361
with:
351362
script: |
352363
console.log(`${{ steps.meta.outputs.tags }}`);
364+
365+
output-env:
366+
runs-on: ubuntu-latest
367+
steps:
368+
-
369+
name: Checkout
370+
uses: actions/checkout@v3
371+
-
372+
name: Docker meta
373+
id: meta
374+
uses: ./
375+
with:
376+
images: |
377+
${{ env.DOCKER_IMAGE }}
378+
ghcr.io/name/app
379+
labels: |
380+
maintainer=CrazyMax
381+
-
382+
name: Build
383+
uses: docker/build-push-action@v3
384+
with:
385+
context: ./test
386+
file: ./test/output.Dockerfile
387+
build-args: |
388+
DOCKER_METADATA_OUTPUT_VERSION
389+
DOCKER_METADATA_OUTPUT_TAGS
390+
DOCKER_METADATA_OUTPUT_LABELS
391+
DOCKER_METADATA_OUTPUT_JSON

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,23 @@ Following outputs are available
297297
| `json` | String | JSON output of tags and labels |
298298
| `bake-file` | File | [Bake file definition](https://docs.docker.com/build/customize/bake/file-definition/) path |
299299

300+
Alternatively, each output is also exported as an environment variable:
301+
302+
* `DOCKER_METADATA_OUTPUT_VERSION`
303+
* `DOCKER_METADATA_OUTPUT_TAGS`
304+
* `DOCKER_METADATA_OUTPUT_LABELS`
305+
* `DOCKER_METADATA_OUTPUT_JSON`
306+
* `DOCKER_METADATA_OUTPUT_BAKE_FILE`
307+
308+
So it can be used with our [Docker Build Push action](https://github.com/docker/build-push-action/):
309+
310+
```yaml
311+
- uses: docker/build-push-action@v3
312+
with:
313+
build-args: |
314+
DOCKER_METADATA_OUTPUT_JSON
315+
```
316+
300317
### environment variables
301318

302319
| Name | Type | Description |

src/main.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async function run() {
4141
core.info(version.main || '');
4242
core.endGroup();
4343
}
44-
core.setOutput('version', version.main || '');
44+
setOutput('version', version.main || '');
4545

4646
// Docker tags
4747
const tags: Array<string> = meta.getTags();
@@ -54,7 +54,7 @@ async function run() {
5454
}
5555
core.endGroup();
5656
}
57-
core.setOutput('tags', tags.join(inputs.sepTags));
57+
setOutput('tags', tags.join(inputs.sepTags));
5858

5959
// Docker labels
6060
const labels: Array<string> = meta.getLabels();
@@ -63,24 +63,29 @@ async function run() {
6363
core.info(label);
6464
}
6565
core.endGroup();
66-
core.setOutput('labels', labels.join(inputs.sepLabels));
66+
setOutput('labels', labels.join(inputs.sepLabels));
6767

6868
// JSON
6969
const jsonOutput = meta.getJSON();
7070
core.startGroup(`JSON output`);
7171
core.info(JSON.stringify(jsonOutput, null, 2));
7272
core.endGroup();
73-
core.setOutput('json', jsonOutput);
73+
setOutput('json', JSON.stringify(jsonOutput));
7474

7575
// Bake file definition
7676
const bakeFile: string = meta.getBakeFile();
7777
core.startGroup(`Bake file definition`);
7878
core.info(fs.readFileSync(bakeFile, 'utf8'));
7979
core.endGroup();
80-
core.setOutput('bake-file', bakeFile);
80+
setOutput('bake-file', bakeFile);
8181
} catch (error) {
8282
core.setFailed(error.message);
8383
}
8484
}
8585

86+
function setOutput(name: string, value: string) {
87+
core.setOutput(name, value);
88+
core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value);
89+
}
90+
8691
run();

test/json.Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# syntax=docker/dockerfile:1
2+
FROM alpine
3+
RUN apk add --no-cache coreutils jq
4+
ARG BUILDINFO
5+
RUN printenv BUILDINFO
6+
RUN echo $BUILDINFO | jq

test/output.Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# syntax=docker/dockerfile:1
2+
FROM alpine
3+
RUN apk add --no-cache coreutils jq
4+
ARG DOCKER_METADATA_OUTPUT_VERSION
5+
ARG DOCKER_METADATA_OUTPUT_TAGS
6+
ARG DOCKER_METADATA_OUTPUT_LABELS
7+
ARG DOCKER_METADATA_OUTPUT_JSON
8+
RUN printenv DOCKER_METADATA_OUTPUT_VERSION
9+
RUN printenv DOCKER_METADATA_OUTPUT_TAGS
10+
RUN printenv DOCKER_METADATA_OUTPUT_LABELS
11+
RUN printenv DOCKER_METADATA_OUTPUT_JSON
12+
RUN echo $DOCKER_METADATA_OUTPUT_JSON | jq

0 commit comments

Comments
 (0)