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

labelPrefix flavor for annotations support #351

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ___
* [`{{date '<format>' tz='<timezone>'}}`](#date-format-tztimezone)
* [Major version zero](#major-version-zero)
* [JSON output object](#json-output-object)
* [Labels prefix](#labels-prefix)
* [Overwrite labels](#overwrite-labels)
* [Contributing](#contributing)

Expand Down Expand Up @@ -368,13 +369,16 @@ flavor: |
latest=auto
prefix=
suffix=
labelPrefix=
```

* `latest=<auto|true|false>`: Handle [latest tag](#latest-tag) (default `auto`)
* `prefix=<string>,onlatest=<true|false>`: A global prefix for each generated
tag and optionally for `latest`
* `suffix=<string>,onlatest=<true|false>`: A global suffix for each generated
tag and optionally for `latest`
* `labelPrefix=<string>`: A global prefix to apply to each label. Can be
useful to [set annotations at manifest or index level](#labels-prefix).

## `tags` input

Expand Down Expand Up @@ -867,6 +871,31 @@ that you can reuse them further in your workflow using the [`fromJSON` function]
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
```

### Labels prefix

Since Buildx 0.12, it is possible to set annotations to your image through the
`--annotation` flag. When using the metadata-action you can set the `manifest:`
or `index:` prefix to each label using the `labelPrefix` attribute in the
`flavor` input. When used with the [`build-push-action`](https://github.com/docker/build-push-action/)
and the `annotations` input, it will either set annotations at the manifest or
index level:

```yaml
-
name: Docker meta
uses: docker/metadata-action@v5
with:
images: name/app
flavor: |
labelPrefix=index:
-
name: Build and push
uses: docker/build-push-action@v5
with:
tags: ${{ steps.meta.outputs.tags }}
annotations: ${{ steps.meta.outputs.labels }}
```

### Overwrite labels

If some [OCI Image Format Specification](https://github.com/opencontainers/image-spec/blob/master/annotations.md)
Expand Down
24 changes: 24 additions & 0 deletions __tests__/flavor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('transform', () => {
prefixLatest: false,
suffix: "",
suffixLatest: false,
labelPrefix: "",
} as Flavor,
false
],
Expand All @@ -50,6 +51,7 @@ describe('transform', () => {
prefixLatest: false,
suffix: "",
suffixLatest: false,
labelPrefix: "",
} as Flavor,
false
],
Expand All @@ -63,6 +65,7 @@ describe('transform', () => {
prefixLatest: false,
suffix: "",
suffixLatest: false,
labelPrefix: "",
} as Flavor,
false
],
Expand All @@ -83,6 +86,7 @@ describe('transform', () => {
prefixLatest: false,
suffix: "",
suffixLatest: false,
labelPrefix: "",
} as Flavor,
false
],
Expand All @@ -96,6 +100,7 @@ describe('transform', () => {
prefixLatest: false,
suffix: "-alpine",
suffixLatest: false,
labelPrefix: "",
} as Flavor,
false
],
Expand All @@ -111,6 +116,7 @@ describe('transform', () => {
prefixLatest: false,
suffix: "-alpine",
suffixLatest: false,
labelPrefix: "",
} as Flavor,
false
],
Expand All @@ -124,6 +130,7 @@ describe('transform', () => {
prefixLatest: true,
suffix: "",
suffixLatest: false,
labelPrefix: "",
} as Flavor,
false
],
Expand All @@ -137,6 +144,7 @@ describe('transform', () => {
prefixLatest: false,
suffix: "-alpine",
suffixLatest: true,
labelPrefix: "",
} as Flavor,
false
],
Expand All @@ -151,6 +159,7 @@ describe('transform', () => {
prefixLatest: true,
suffix: "-alpine",
suffixLatest: true,
labelPrefix: "",
} as Flavor,
false
],
Expand All @@ -164,6 +173,21 @@ describe('transform', () => {
prefixLatest: false,
suffix: "",
suffixLatest: false,
labelPrefix: "",
} as Flavor,
false
],
[
[
`labelPrefix=manifest:`,
],
{
latest: "auto",
prefix: "",
prefixLatest: false,
suffix: "",
suffixLatest: false,
labelPrefix: "manifest:",
} as Flavor,
false
]
Expand Down
97 changes: 96 additions & 1 deletion __tests__/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3463,6 +3463,37 @@ describe('raw', () => {
"org.opencontainers.image.version=foo"
]
],
[
'raw11',
'event_push_dev.env',
{
images: ['user/app'],
tags: [
`type=raw,foo`
],
flavor: [
`labelPrefix=index:`,
]
} as Inputs,
{
main: 'foo',
partial: [],
latest: false
} as Version,
[
'user/app:foo'
],
[
"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=foo"
]
]
])('given %p wth %p event', tagsLabelsTest);
});

Expand Down Expand Up @@ -3706,7 +3737,35 @@ describe('json', () => {
"org.opencontainers.image.version": "v1.1.1"
}
}
]
],
[
'json08',
'event_push_dev.env',
{
images: ['user/app'],
tags: [
`type=raw,foo`
],
flavor: [
"labelPrefix=manifest:",
]
} as Inputs,
{
"tags": [
"user/app:foo"
],
"labels": {
"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": "foo"
}
}
],
])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exJSON: unknown) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));

Expand Down Expand Up @@ -4012,6 +4071,42 @@ describe('bake', () => {
}
}
}
],
[
'bake08',
'event_push_dev.env',
{
images: ['user/app'],
tags: [
`type=raw,foo`
],
flavor: [
"labelPrefix=index:",
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"user/app:foo"
],
"labels": {
"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": "foo"
},
"args": {
"DOCKER_META_IMAGES": "user/app",
"DOCKER_META_VERSION": "foo",
}
}
}
}
]
])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exBakeDefinition: unknown) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
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.

9 changes: 8 additions & 1 deletion src/flavor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Flavor {
prefixLatest: boolean;
suffix: string;
suffixLatest: boolean;
labelPrefix: string;
}

export function Transform(inputs: string[]): Flavor {
Expand All @@ -15,7 +16,8 @@ export function Transform(inputs: string[]): Flavor {
prefix: '',
prefixLatest: false,
suffix: '',
suffixLatest: false
suffixLatest: false,
labelPrefix: ''
};

for (const input of inputs) {
Expand Down Expand Up @@ -68,6 +70,10 @@ export function Transform(inputs: string[]): Flavor {
}
break;
}
case 'labelprefix': {
flavor.labelPrefix = value;
break;
}
default: {
throw new Error(`Unknown flavor entry: ${input}`);
}
Expand All @@ -81,6 +87,7 @@ export function Transform(inputs: string[]): Flavor {
core.info(`prefixLatest=${flavor.prefixLatest}`);
core.info(`suffix=${flavor.suffix}`);
core.info(`suffixLatest=${flavor.suffixLatest}`);
core.info(`labelPrefix=${flavor.labelPrefix}`);
core.endGroup();

return flavor;
Expand Down
5 changes: 4 additions & 1 deletion src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ export class Meta {
}

public getLabels(): Array<string> {
const labels: Array<string> = [
let labels: Array<string> = [
`org.opencontainers.image.title=${this.repo.name || ''}`,
`org.opencontainers.image.description=${this.repo.description || ''}`,
`org.opencontainers.image.url=${this.repo.html_url || ''}`,
Expand All @@ -466,6 +466,9 @@ export class Meta {
`org.opencontainers.image.licenses=${this.repo.license?.spdx_id || ''}`
];
labels.push(...this.inputs.labels);
if (this.flavor.labelPrefix.length > 0) {
labels = labels.map(label => this.flavor.labelPrefix + label);
}

return Array.from(
new Map<string, string>(
Expand Down