From 6729545cde76a1fb0fc1133c68fa5af1181f7e68 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 13 Jan 2023 10:45:14 +0100 Subject: [PATCH] Provide outputs as env vars Signed-off-by: CrazyMax --- .github/workflows/ci.yml | 39 +++++++++++++++++++++++++++++++++++++++ README.md | 17 +++++++++++++++++ src/main.ts | 15 ++++++++++----- test/json.Dockerfile | 6 ++++++ test/output.Dockerfile | 12 ++++++++++++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 test/json.Dockerfile create mode 100644 test/output.Dockerfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42b300e..3ab6ce6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,6 +37,9 @@ jobs: type=ref,event=tag type=ref,event=pr type=sha + - + name: Print envs + run: env|sort tag-schedule: runs-on: ubuntu-latest @@ -224,6 +227,14 @@ jobs: echo "version=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}" echo "revision=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}" echo "created=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}" + - + name: JSON build arg + uses: docker/build-push-action@v3 + with: + context: ./test + file: ./test/json.Dockerfile + build-args: | + BUILDINFO=${{ steps.meta.outputs.json }} docker-push: runs-on: ubuntu-latest @@ -350,3 +361,31 @@ jobs: with: script: | console.log(`${{ steps.meta.outputs.tags }}`); + + output-env: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v3 + - + name: Docker meta + id: meta + uses: ./ + with: + images: | + ${{ env.DOCKER_IMAGE }} + ghcr.io/name/app + labels: | + maintainer=CrazyMax + - + name: Build + uses: docker/build-push-action@v3 + with: + context: ./test + file: ./test/output.Dockerfile + build-args: | + DOCKER_METADATA_OUTPUT_VERSION + DOCKER_METADATA_OUTPUT_TAGS + DOCKER_METADATA_OUTPUT_LABELS + DOCKER_METADATA_OUTPUT_JSON diff --git a/README.md b/README.md index 808b82a..4d0eb63 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,23 @@ Following outputs are available | `json` | String | JSON output of tags and labels | | `bake-file` | File | [Bake file definition](https://docs.docker.com/build/customize/bake/file-definition/) path | +Alternatively, each output is also exported as an environment variable: + +* `DOCKER_METADATA_OUTPUT_VERSION` +* `DOCKER_METADATA_OUTPUT_TAGS` +* `DOCKER_METADATA_OUTPUT_LABELS` +* `DOCKER_METADATA_OUTPUT_JSON` +* `DOCKER_METADATA_OUTPUT_BAKE_FILE` + +So it can be used with our [Docker Build Push action](https://github.com/docker/build-push-action/): + +```yaml +- uses: docker/build-push-action@v3 + with: + build-args: | + DOCKER_METADATA_OUTPUT_JSON +``` + ### environment variables | Name | Type | Description | diff --git a/src/main.ts b/src/main.ts index cf28cb8..c2b70e3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -41,7 +41,7 @@ async function run() { core.info(version.main || ''); core.endGroup(); } - core.setOutput('version', version.main || ''); + setOutput('version', version.main || ''); // Docker tags const tags: Array = meta.getTags(); @@ -54,7 +54,7 @@ async function run() { } core.endGroup(); } - core.setOutput('tags', tags.join(inputs.sepTags)); + setOutput('tags', tags.join(inputs.sepTags)); // Docker labels const labels: Array = meta.getLabels(); @@ -63,24 +63,29 @@ async function run() { core.info(label); } core.endGroup(); - core.setOutput('labels', labels.join(inputs.sepLabels)); + setOutput('labels', labels.join(inputs.sepLabels)); // JSON const jsonOutput = meta.getJSON(); core.startGroup(`JSON output`); core.info(JSON.stringify(jsonOutput, null, 2)); core.endGroup(); - core.setOutput('json', jsonOutput); + setOutput('json', JSON.stringify(jsonOutput)); // Bake file definition const bakeFile: string = meta.getBakeFile(); core.startGroup(`Bake file definition`); core.info(fs.readFileSync(bakeFile, 'utf8')); core.endGroup(); - core.setOutput('bake-file', bakeFile); + setOutput('bake-file', bakeFile); } catch (error) { core.setFailed(error.message); } } +function setOutput(name: string, value: string) { + core.setOutput(name, value); + core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value); +} + run(); diff --git a/test/json.Dockerfile b/test/json.Dockerfile new file mode 100644 index 0000000..8eb4e75 --- /dev/null +++ b/test/json.Dockerfile @@ -0,0 +1,6 @@ +# syntax=docker/dockerfile:1 +FROM alpine +RUN apk add --no-cache coreutils jq +ARG BUILDINFO +RUN printenv BUILDINFO +RUN echo $BUILDINFO | jq diff --git a/test/output.Dockerfile b/test/output.Dockerfile new file mode 100644 index 0000000..532b7bb --- /dev/null +++ b/test/output.Dockerfile @@ -0,0 +1,12 @@ +# syntax=docker/dockerfile:1 +FROM alpine +RUN apk add --no-cache coreutils jq +ARG DOCKER_METADATA_OUTPUT_VERSION +ARG DOCKER_METADATA_OUTPUT_TAGS +ARG DOCKER_METADATA_OUTPUT_LABELS +ARG DOCKER_METADATA_OUTPUT_JSON +RUN printenv DOCKER_METADATA_OUTPUT_VERSION +RUN printenv DOCKER_METADATA_OUTPUT_TAGS +RUN printenv DOCKER_METADATA_OUTPUT_LABELS +RUN printenv DOCKER_METADATA_OUTPUT_JSON +RUN echo $DOCKER_METADATA_OUTPUT_JSON | jq