From f0ad8701decba360e73ae230d5abe4c57fcedd36 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Mon, 27 Nov 2023 11:39:37 +0100 Subject: [PATCH] annotations support Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- .github/workflows/ci.yml | 44 +++++++ README.md | 92 ++++++++++++-- __tests__/meta.test.ts | 266 +++++++++++++++++++++++++++++++++++++-- action.yml | 4 + src/main.ts | 27 +++- src/meta.ts | 58 +++++---- 6 files changed, 444 insertions(+), 47 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 45f8383..aa505b0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -383,3 +383,47 @@ jobs: DOCKER_METADATA_OUTPUT_TAGS DOCKER_METADATA_OUTPUT_LABELS DOCKER_METADATA_OUTPUT_JSON + + bake-annotations: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v4 + - + name: Docker meta + id: docker_meta + uses: ./ + with: + images: | + ${{ env.DOCKER_IMAGE }} + ghcr.io/name/app + tags: | + type=schedule + type=ref,event=branch + type=ref,event=tag + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha + env: + DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index + - + name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + version: v0.12.0-rc1 + - + name: Build + uses: docker/bake-action@v4 + with: + files: | + ./test/docker-bake.hcl + ${{ steps.docker_meta.outputs.bake-file-tags }} + ${{ steps.docker_meta.outputs.bake-file-annotations }} + targets: | + release diff --git a/README.md b/README.md index a53272d..008d762 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ ___ * [Major version zero](#major-version-zero) * [JSON output object](#json-output-object) * [Overwrite labels](#overwrite-labels) + * [Annotations](#annotations) * [Contributing](#contributing) ## Usage @@ -307,23 +308,27 @@ The following inputs can be used as `step.with` keys: The following outputs are available: -| Name | Type | Description | -|--------------------|--------|-------------------------------------------------------------------------------------------------| -| `version` | String | Docker image version | -| `tags` | String | Docker tags | -| `labels` | String | Docker labels | -| `json` | String | JSON output of tags and labels | -| `bake-file-tags` | File | [Bake file definition](https://docs.docker.com/build/bake/reference/) path with tags | -| `bake-file-labels` | File | [Bake file definition](https://docs.docker.com/build/bake/reference/) path with labels | +| Name | Type | Description | +|-------------------------|--------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `version` | String | Docker image version | +| `tags` | String | Docker tags | +| `labels` | String | Docker labels | +| `annotations` | String | [Annotations](https://github.com/moby/buildkit/blob/master/docs/annotations.md) | +| `json` | String | JSON output of tags and labels | +| `bake-file-tags` | File | [Bake file definition](https://docs.docker.com/build/bake/reference/) path with tags | +| `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) | 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_ANNOTATIONS` * `DOCKER_METADATA_OUTPUT_JSON` * `DOCKER_METADATA_OUTPUT_BAKE_FILE_TAGS` * `DOCKER_METADATA_OUTPUT_BAKE_FILE_LABELS` +* `DOCKER_METADATA_OUTPUT_BAKE_FILE_ANNOTATIONS` So it can be used with our [Docker Build Push action](https://github.com/docker/build-push-action/): @@ -336,9 +341,10 @@ So it can be used with our [Docker Build Push action](https://github.com/docker/ ### environment variables -| Name | Type | Description | -|-------------------------------|------|------------------------------------------------------------------------------------------------------------| -| `DOCKER_METADATA_PR_HEAD_SHA` | Bool | If `true`, set associated head SHA instead of commit SHA that triggered the workflow on pull request event | +| Name | Type | Description | +|--------------------------------------|--------|------------------------------------------------------------------------------------------------------------| +| `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_ANNOTATIONS_LEVELS` | String | Comma separated list of annotations levels to set for annotations output separated (default `manifest`) | ## `context` input @@ -904,6 +910,70 @@ labels generated are not suitable, you can overwrite them like this: org.opencontainers.image.vendor=MyCompany ``` +### Annotations + +Since Buildx 0.12, it is possible to set annotations to your image through the +`--annotation` flag. + +With the [`build-push-action`](https://github.com/docker/build-push-action/), +you can set the `annotations` input with the value of the `annotations` output +of the `metadata-action`: + +```yaml + - + name: Docker meta + uses: docker/metadata-action@v5 + with: + images: name/app + - + name: Build and push + uses: docker/build-push-action@v5 + with: + tags: ${{ steps.meta.outputs.tags }} + annotations: ${{ steps.meta.outputs.annotations }} +``` + +The same can be done with the [`bake-action`](https://github.com/docker/bake-action/): + +```yaml + - + name: Docker meta + uses: docker/metadata-action@v5 + with: + images: name/app + - + name: Build + uses: docker/bake-action@v3 + with: + files: | + ./docker-bake.hcl + ${{ steps.meta.outputs.bake-file-tags }} + ${{ steps.meta.outputs.bake-file-annotations }} + targets: build +``` + +If you want to set specific level(s) for your annotations, you can use the +[`DOCKER_METADATA_ANNOTATIONS_LEVELS` environment variable](#environment-variables) +with a comma separated list of levels (defaults to `manifest`): + +```yaml + - + name: Docker meta + uses: docker/metadata-action@v5 + with: + images: name/app + env: + DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index + - + name: Build and push + uses: docker/build-push-action@v5 + with: + tags: ${{ steps.meta.outputs.tags }} + annotations: ${{ steps.meta.outputs.annotations }} +``` + +More information about annotations in the [BuildKit documentation](https://github.com/moby/buildkit/blob/master/docs/annotations.md). + ## Contributing Want to contribute? Awesome! You can find information about contributing to diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts index e5033a4..97431d0 100644 --- a/__tests__/meta.test.ts +++ b/__tests__/meta.test.ts @@ -3503,7 +3503,18 @@ describe('json', () => { "org.opencontainers.image.title": "Hello-World", "org.opencontainers.image.url": "https://github.com/octocat/Hello-World", "org.opencontainers.image.version": "dev" - } + }, + "annotations": [ + "manifest:foo=", + "manifest:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest:org.opencontainers.image.description=This your first repo!", + "manifest:org.opencontainers.image.licenses=MIT", + "manifest:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.title=Hello-World", + "manifest:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.version=dev" + ] } ], [ @@ -3530,7 +3541,17 @@ describe('json', () => { "org.opencontainers.image.title": "Hello-World", "org.opencontainers.image.url": "https://github.com/octocat/Hello-World", "org.opencontainers.image.version": "dev" - } + }, + "annotations": [ + "manifest:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest:org.opencontainers.image.description=This your first repo!", + "manifest:org.opencontainers.image.licenses=MIT", + "manifest:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.title=Hello-World", + "manifest:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.version=dev" + ] } ], [ @@ -3563,7 +3584,17 @@ describe('json', () => { "org.opencontainers.image.title": "Hello-World", "org.opencontainers.image.url": "https://github.com/octocat/Hello-World", "org.opencontainers.image.version": "release1" - } + }, + "annotations": [ + "manifest:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest:org.opencontainers.image.description=This your first repo!", + "manifest:org.opencontainers.image.licenses=MIT", + "manifest:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.title=Hello-World", + "manifest:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.version=release1" + ] } ], [ @@ -3597,7 +3628,17 @@ describe('json', () => { "org.opencontainers.image.title": "Hello-World", "org.opencontainers.image.url": "https://github.com/octocat/Hello-World", "org.opencontainers.image.version": "20200110" - } + }, + "annotations": [ + "manifest:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest:org.opencontainers.image.description=This your first repo!", + "manifest:org.opencontainers.image.licenses=MIT", + "manifest:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.title=Hello-World", + "manifest:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.version=20200110" + ] } ], [ @@ -3640,7 +3681,17 @@ describe('json', () => { "org.opencontainers.image.title": "Hello-World", "org.opencontainers.image.url": "https://github.com/octocat/Hello-World", "org.opencontainers.image.version": "1.1.1" - } + }, + "annotations": [ + "manifest:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest:org.opencontainers.image.description=This your first repo!", + "manifest:org.opencontainers.image.licenses=MIT", + "manifest:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.title=Hello-World", + "manifest:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.version=1.1.1" + ] } ], [ @@ -3672,7 +3723,17 @@ describe('json', () => { "org.opencontainers.image.title": "Hello-World", "org.opencontainers.image.url": "https://github.com/octocat/Hello-World", "org.opencontainers.image.version": "my" - } + }, + "annotations": [ + "manifest:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest:org.opencontainers.image.description=This your first repo!", + "manifest:org.opencontainers.image.licenses=MIT", + "manifest:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.title=Hello-World", + "manifest:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.version=my" + ] } ], [ @@ -3704,7 +3765,19 @@ describe('json', () => { "org.opencontainers.image.url": "https://github.com/octocat/Hello-World", "org.opencontainers.image.vendor": "MyCompany", "org.opencontainers.image.version": "v1.1.1" - } + }, + "annotations": [ + "manifest:maintainer=CrazyMax", + "manifest:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest:org.opencontainers.image.description=Another description", + "manifest:org.opencontainers.image.licenses=MIT", + "manifest:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.title=MyCustom=Title", + "manifest:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest:org.opencontainers.image.vendor=MyCompany", + "manifest:org.opencontainers.image.version=v1.1.1" + ] } ] ])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exJSON: unknown) => { @@ -3714,7 +3787,7 @@ describe('json', () => { const repo = await toolkit.github.repoData(); const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo); - const jsonOutput = meta.getJSON(); + const jsonOutput = meta.getJSON(['manifest']); expect(jsonOutput).toEqual(exJSON); }); }); @@ -3769,6 +3842,30 @@ describe('bakeFile', () => { } } } + }, + { + "target": { + "docker-metadata-action": { + "annotations": [ + "index:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "index:org.opencontainers.image.description=This your first repo!", + "index:org.opencontainers.image.licenses=MIT", + "index:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "index:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.title=Hello-World", + "index:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.version=dev", + "manifest-descriptor:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest-descriptor:org.opencontainers.image.description=This your first repo!", + "manifest-descriptor:org.opencontainers.image.licenses=MIT", + "manifest-descriptor:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest-descriptor:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.title=Hello-World", + "manifest-descriptor:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.version=dev" + ] + } + } } ], [ @@ -3810,6 +3907,30 @@ describe('bakeFile', () => { } } } + }, + { + "target": { + "docker-metadata-action": { + "annotations": [ + "index:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "index:org.opencontainers.image.description=This your first repo!", + "index:org.opencontainers.image.licenses=MIT", + "index:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "index:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.title=Hello-World", + "index:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.version=dev", + "manifest-descriptor:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest-descriptor:org.opencontainers.image.description=This your first repo!", + "manifest-descriptor:org.opencontainers.image.licenses=MIT", + "manifest-descriptor:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest-descriptor:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.title=Hello-World", + "manifest-descriptor:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.version=dev" + ] + } + } } ], [ @@ -3857,6 +3978,30 @@ describe('bakeFile', () => { } } } + }, + { + "target": { + "meta": { + "annotations": [ + "index:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "index:org.opencontainers.image.description=This your first repo!", + "index:org.opencontainers.image.licenses=MIT", + "index:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "index:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.title=Hello-World", + "index:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.version=release1", + "manifest-descriptor:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest-descriptor:org.opencontainers.image.description=This your first repo!", + "manifest-descriptor:org.opencontainers.image.licenses=MIT", + "manifest-descriptor:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest-descriptor:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.title=Hello-World", + "manifest-descriptor:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.version=release1" + ] + } + } } ], [ @@ -3905,6 +4050,30 @@ describe('bakeFile', () => { } } } + }, + { + "target": { + "docker-metadata-action": { + "annotations": [ + "index:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "index:org.opencontainers.image.description=This your first repo!", + "index:org.opencontainers.image.licenses=MIT", + "index:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "index:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.title=Hello-World", + "index:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.version=20200110", + "manifest-descriptor:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest-descriptor:org.opencontainers.image.description=This your first repo!", + "manifest-descriptor:org.opencontainers.image.licenses=MIT", + "manifest-descriptor:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest-descriptor:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.title=Hello-World", + "manifest-descriptor:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.version=20200110" + ] + } + } } ], [ @@ -3962,6 +4131,30 @@ describe('bakeFile', () => { } } } + }, + { + "target": { + "docker-metadata-action": { + "annotations": [ + "index:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "index:org.opencontainers.image.description=This your first repo!", + "index:org.opencontainers.image.licenses=MIT", + "index:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "index:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.title=Hello-World", + "index:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.version=1.1.1", + "manifest-descriptor:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest-descriptor:org.opencontainers.image.description=This your first repo!", + "manifest-descriptor:org.opencontainers.image.licenses=MIT", + "manifest-descriptor:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest-descriptor:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.title=Hello-World", + "manifest-descriptor:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.version=1.1.1" + ] + } + } } ], [ @@ -4008,6 +4201,30 @@ describe('bakeFile', () => { } } } + }, + { + "target": { + "docker-metadata-action": { + "annotations": [ + "index:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "index:org.opencontainers.image.description=This your first repo!", + "index:org.opencontainers.image.licenses=MIT", + "index:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "index:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.title=Hello-World", + "index:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.version=my", + "manifest-descriptor:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest-descriptor:org.opencontainers.image.description=This your first repo!", + "manifest-descriptor:org.opencontainers.image.licenses=MIT", + "manifest-descriptor:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest-descriptor:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.title=Hello-World", + "manifest-descriptor:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.version=my" + ] + } + } } ], [ @@ -4053,9 +4270,37 @@ describe('bakeFile', () => { } } } + }, + { + "target": { + "docker-metadata-action": { + "annotations": [ + "index:maintainer=CrazyMax", + "index:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "index:org.opencontainers.image.description=Another description", + "index:org.opencontainers.image.licenses=MIT", + "index:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "index:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.title=MyCustom=Title", + "index:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "index:org.opencontainers.image.vendor=MyCompany", + "index:org.opencontainers.image.version=v1.1.1", + "manifest-descriptor:maintainer=CrazyMax", + "manifest-descriptor:org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "manifest-descriptor:org.opencontainers.image.description=Another description", + "manifest-descriptor:org.opencontainers.image.licenses=MIT", + "manifest-descriptor:org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37", + "manifest-descriptor:org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.title=MyCustom=Title", + "manifest-descriptor:org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "manifest-descriptor:org.opencontainers.image.vendor=MyCompany", + "manifest-descriptor:org.opencontainers.image.version=v1.1.1" + ] + } + } } ] - ])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exBakeTags: unknown, exBakeLabels: unknown) => { + ])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exBakeTags: unknown, exBakeLabels: unknown, exBakeAnnotations: unknown) => { process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile))); const toolkit = new Toolkit(); @@ -4067,6 +4312,9 @@ describe('bakeFile', () => { const bakeFileLabels = meta.getBakeFile('labels'); expect(JSON.parse(fs.readFileSync(bakeFileLabels, 'utf8'))).toEqual(exBakeLabels); + + const bakeFileAnnotations = meta.getBakeFile('annotations:index,manifest-descriptor'); + expect(JSON.parse(fs.readFileSync(bakeFileAnnotations, 'utf8'))).toEqual(exBakeAnnotations); }); }); diff --git a/action.yml b/action.yml index 561853f..6bba9a8 100644 --- a/action.yml +++ b/action.yml @@ -44,12 +44,16 @@ outputs: description: 'Generated Docker tags' labels: description: 'Generated Docker labels' + annotations: + description: 'Generated annotations' json: description: 'JSON output of tags and labels' bake-file-tags: description: 'Bake definition file with tags' bake-file-labels: description: 'Bake definition file with labels' + bake-file-annotations: + description: 'Bake definiton file with annotations' bake-file: description: 'Bake definition file with tags and labels' diff --git a/src/main.ts b/src/main.ts index 426cc17..2711e5d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -74,19 +74,38 @@ actionsToolkit.run( setOutput('labels', labels.join(inputs.sepLabels)); }); + // Annotations + const alevels = process.env.DOCKER_METADATA_ANNOTATIONS_LEVELS || 'manifest'; + if (labels.length > 0) { + await core.group(`Annotations`, async () => { + const annotations: Array = []; + for (const level of alevels.split(',')) { + annotations.push( + ...labels.map(label => { + const v = `${level}:${label}`; + core.info(v); + return v; + }) + ); + } + setOutput(`annotations`, annotations.join(inputs.sepLabels)); + }); + } + // JSON - const jsonOutput = meta.getJSON(); + const jsonOutput = meta.getJSON(alevels.split(',')); await core.group(`JSON output`, async () => { core.info(JSON.stringify(jsonOutput, null, 2)); setOutput('json', JSON.stringify(jsonOutput)); }); // Bake files - for (const kind of ['tags', 'labels']) { + for (const kind of ['tags', 'labels', 'annotations:' + alevels]) { + const outputName = kind.split(':')[0]; const bakeFile: string = meta.getBakeFile(kind); - await core.group(`Bake file definition (${kind})`, async () => { + await core.group(`Bake file definition (${outputName})`, async () => { core.info(fs.readFileSync(bakeFile, 'utf8')); - setOutput(`bake-file-${kind}`, bakeFile); + setOutput(`bake-file-${outputName}`, bakeFile); }); } diff --git a/src/meta.ts b/src/meta.ts index 4b59a69..a09f72d 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -480,7 +480,11 @@ export class Meta { .map(([key, value]) => `${key}=${value}`); } - public getJSON(): unknown { + public getJSON(alevels: string[]): unknown { + const annotations: Array = []; + for (const level of alevels) { + annotations.push(...this.getLabels().map(label => `${level}:${label}`)); + } return { tags: this.getTags(), labels: this.getLabels().reduce((res, label) => { @@ -490,34 +494,42 @@ export class Meta { } res[matches[1]] = matches[2]; return res; - }, {}) + }, {}), + annotations: annotations }; } public getBakeFile(kind: string): string { - switch (kind) { - case 'tags': - return this.generateBakeFile(kind, { - tags: this.getTags(), - args: { - DOCKER_META_IMAGES: this.getImageNames().join(','), - DOCKER_META_VERSION: this.version.main - } - }); - case 'labels': - return this.generateBakeFile(kind, { - labels: this.getLabels().reduce((res, label) => { - const matches = label.match(/([^=]*)=(.*)/); - if (!matches) { - return res; - } - res[matches[1]] = matches[2]; + if (kind == 'tags') { + return this.generateBakeFile(kind, { + tags: this.getTags(), + args: { + DOCKER_META_IMAGES: this.getImageNames().join(','), + DOCKER_META_VERSION: this.version.main + } + }); + } else if (kind == 'labels') { + return this.generateBakeFile(kind, { + labels: this.getLabels().reduce((res, label) => { + const matches = label.match(/([^=]*)=(.*)/); + if (!matches) { return res; - }, {}) - }); - default: - throw new Error(`Unknown bake file type: ${kind}`); + } + res[matches[1]] = matches[2]; + return res; + }, {}) + }); + } else if (kind.startsWith('annotations:')) { + const name = kind.split(':')[0]; + const annotations: Array = []; + for (const level of kind.split(':')[1].split(',')) { + annotations.push(...this.getLabels().map(label => `${level}:${label}`)); + } + return this.generateBakeFile(name, { + annotations: annotations + }); } + throw new Error(`Unknown bake file type: ${kind}`); } public getBakeFileTagsLabels(): string {