Merge pull request #497 from omus/cv/output-env

Support disabling `DOCKER_METADATA_OUTPUT_*` environment variables
This commit is contained in:
CrazyMax 2025-02-26 14:37:22 +01:00 committed by GitHub
commit 95438bc6e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 40 additions and 4 deletions

View File

@ -472,6 +472,32 @@ jobs:
DOCKER_METADATA_OUTPUT_ANNOTATIONS DOCKER_METADATA_OUTPUT_ANNOTATIONS
DOCKER_METADATA_OUTPUT_JSON DOCKER_METADATA_OUTPUT_JSON
no-output-env:
runs-on: ubuntu-latest
env:
DOCKER_METADATA_SET_OUTPUT_ENV: false
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
-
name: No output environment variables set
shell: bash
run: |
[[ "$(printenv | grep "^DOCKER_METADATA_OUTPUT_" | wc -l)" -eq 0 ]] || exit 1
bake-annotations: bake-annotations:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View File

@ -319,7 +319,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-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) | | `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 `DOCKER_METADATA_SET_OUTPUT_ENV` is `true`:
* `DOCKER_METADATA_OUTPUT_VERSION` * `DOCKER_METADATA_OUTPUT_VERSION`
* `DOCKER_METADATA_OUTPUT_TAGS` * `DOCKER_METADATA_OUTPUT_TAGS`
@ -346,6 +346,7 @@ So it can be used with our [Docker Build Push action](https://github.com/docker/
| `DOCKER_METADATA_PR_HEAD_SHA` | Bool | If `true`, set associated head SHA instead of commit SHA that triggered the workflow on pull request event | | `DOCKER_METADATA_PR_HEAD_SHA` | Bool | If `true`, set associated head SHA instead of commit SHA that triggered the workflow on pull request event |
| `DOCKER_METADATA_SHORT_SHA_LENGTH` | Number | Specifies the length of the [short commit SHA](#typesha) to ensure uniqueness. Default is `7`, but can be increased for larger repositories. | | `DOCKER_METADATA_SHORT_SHA_LENGTH` | Number | Specifies the length of the [short commit SHA](#typesha) to ensure uniqueness. Default is `7`, but can be increased for larger repositories. |
| `DOCKER_METADATA_ANNOTATIONS_LEVELS` | String | Comma separated list of annotations levels to set for annotations output separated (default `manifest`) | | `DOCKER_METADATA_ANNOTATIONS_LEVELS` | String | Comma separated list of annotations levels to set for annotations output separated (default `manifest`) |
| `DOCKER_METADATA_SET_OUTPUT_ENV` | Bool | If `true`, sets each output as an environment variable (default `true`) |
## `context` input ## `context` input

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as actionsToolkit from '@docker/actions-toolkit'; import * as actionsToolkit from '@docker/actions-toolkit';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit'; import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
import {Util} from '@docker/actions-toolkit/lib/util';
import {getContext, getInputs, Inputs} from './context'; import {getContext, getInputs, Inputs} from './context';
import {Meta, Version} from './meta'; import {Meta, Version} from './meta';
@ -13,6 +14,7 @@ actionsToolkit.run(
const toolkit = new Toolkit({githubToken: inputs.githubToken}); const toolkit = new Toolkit({githubToken: inputs.githubToken});
const context = await getContext(inputs.context, toolkit); const context = await getContext(inputs.context, toolkit);
const repo = await toolkit.github.repoData(); const repo = await toolkit.github.repoData();
const setOutput = outputEnvEnabled() ? setOutputAndEnv : core.setOutput;
await core.group(`Context info`, async () => { await core.group(`Context info`, async () => {
core.info(`eventName: ${context.eventName}`); core.info(`eventName: ${context.eventName}`);
@ -105,7 +107,14 @@ actionsToolkit.run(
} }
); );
function setOutput(name: string, value: string) { function setOutputAndEnv(name: string, value: string) {
core.setOutput(name, value); core.setOutput(name, value);
core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value); core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value);
} }
function outputEnvEnabled(): boolean {
if (process.env.DOCKER_METADATA_SET_OUTPUT_ENV) {
return Util.parseBool(process.env.DOCKER_METADATA_SET_OUTPUT_ENV);
}
return true;
}