Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide outputs as env vars #257

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
```
Comment on lines +308 to +315
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find docs in build-push-action that describe the syntax for this - we should probably add that on merging this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's a core implementation where you can use --build-arg flag without a value: https://docs.docker.com/engine/reference/commandline/build/#-set-build-time-variables---build-arg

You may also use the --build-arg flag without a value, in which case the value
from the local environment will be propagated into the Docker container being
built:

$ export HTTP_PROXY=http://10.20.30.2:1234
$ docker build --build-arg HTTP_PROXY .


### environment variables

| Name | Type | Description |
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

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