Skip to content

Commit

Permalink
Provide outputs as env vars
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jan 13, 2023
1 parent 05d22bf commit 6729545
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
15 changes: 10 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = meta.getTags();
Expand All @@ -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<string> = meta.getLabels();
Expand All @@ -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();
6 changes: 6 additions & 0 deletions test/json.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/output.Dockerfile
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6729545

Please sign in to comment.