Merge pull request #353 from crazy-max/split-bake-file

bake: split definition into two files
This commit is contained in:
CrazyMax 2023-11-27 00:50:21 -08:00 committed by GitHub
commit 2a4836ac76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 445 additions and 283 deletions

View file

@ -323,7 +323,8 @@ jobs:
with:
files: |
./test/docker-bake.hcl
${{ steps.docker_meta.outputs.bake-file }}
${{ steps.docker_meta.outputs.bake-file-tags }}
${{ steps.docker_meta.outputs.bake-file-labels }}
targets: |
release

View file

@ -230,8 +230,8 @@ jobs:
targets: build
```
Content of `${{ steps.meta.outputs.bake-file }}` file will look like this with
`refs/tags/v1.2.3` ref:
Content of `${{ steps.meta.outputs.bake-file }}` file, combining tags and
labels, will look like this with `refs/tags/v1.2.3` ref:
```json
{
@ -262,6 +262,22 @@ Content of `${{ steps.meta.outputs.bake-file }}` file will look like this with
}
```
You can also use the `bake-file-tags` and `bake-file-labels` outputs if you
just want to use tags and/or labels respectively. The following example is
similar to the previous one:
```yaml
-
name: Build
uses: docker/bake-action@v3
with:
files: |
./docker-bake.hcl
${{ steps.meta.outputs.bake-file-tags }}
${{ steps.meta.outputs.bake-file-labels }}
targets: build
```
## Customizing
### inputs
@ -276,28 +292,29 @@ The following inputs can be used as `step.with` keys:
> org.opencontainers.image.vendor=MyCompany
> ```
| Name | Type | Description |
|---------------------|--------|-------------------------------------------------------------------------------|
| `context` | String | Where to get context data. Allowed options are: `workflow` (default), `git`. |
| `images` | List | List of Docker images to use as base name for tags |
| `tags` | List | List of [tags](#tags-input) as key-value pair attributes |
| `flavor` | List | [Flavor](#flavor-input) to apply |
| `labels` | List | List of custom labels |
| `sep-tags` | String | Separator to use for tags output (default `\n`) |
| `sep-labels` | String | Separator to use for labels output (default `\n`) |
| `bake-target` | String | Bake target name (default `docker-metadata-action`) |
| Name | Type | Description |
|---------------|--------|-------------------------------------------------------------------------------|
| `context` | String | Where to get context data. Allowed options are: `workflow` (default), `git`. |
| `images` | List | List of Docker images to use as base name for tags |
| `tags` | List | List of [tags](#tags-input) as key-value pair attributes |
| `flavor` | List | [Flavor](#flavor-input) to apply |
| `labels` | List | List of custom labels |
| `sep-tags` | String | Separator to use for tags output (default `\n`) |
| `sep-labels` | String | Separator to use for labels output (default `\n`) |
| `bake-target` | String | Bake target name (default `docker-metadata-action`) |
### outputs
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` | File | [Bake file definition](https://docs.docker.com/build/bake/reference/) path |
| 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 |
Alternatively, each output is also exported as an environment variable:
@ -305,7 +322,8 @@ Alternatively, each output is also exported as an environment variable:
* `DOCKER_METADATA_OUTPUT_TAGS`
* `DOCKER_METADATA_OUTPUT_LABELS`
* `DOCKER_METADATA_OUTPUT_JSON`
* `DOCKER_METADATA_OUTPUT_BAKE_FILE`
* `DOCKER_METADATA_OUTPUT_BAKE_FILE_TAGS`
* `DOCKER_METADATA_OUTPUT_BAKE_FILE_LABELS`
So it can be used with our [Docker Build Push action](https://github.com/docker/build-push-action/):

View file

@ -3719,12 +3719,363 @@ describe('json', () => {
});
});
describe('bake', () => {
describe('bakeFile', () => {
// prettier-ignore
// eslint-disable-next-line jest/expect-expect
test.each([
[
'bake01',
'bakeFile01',
'event_push_dev.env',
{
images: ['user/app'],
tags: [
`type=ref,event=branch`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
],
labels: [
"invalid"
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"user/app:dev",
"user/app:my",
"user/app:custom",
"user/app:tags"
],
"args": {
"DOCKER_META_IMAGES": "user/app",
"DOCKER_META_VERSION": "dev",
}
}
}
},
{
"target": {
"docker-metadata-action": {
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "dev"
}
}
}
}
],
[
'bakeFile02',
'event_push_dev.env',
{
images: ['user/app'],
tags: [
`type=ref,event=branch`,
`type=raw,my`
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"user/app:dev",
"user/app:my",
],
"args": {
"DOCKER_META_IMAGES": "user/app",
"DOCKER_META_VERSION": "dev",
}
}
}
},
{
"target": {
"docker-metadata-action": {
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "dev"
}
}
}
}
],
[
'bakeFile03',
'event_tag_release1.env',
{
images: ['user/app'],
tags: [
`type=ref,event=tag`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
],
bakeTarget: "meta"
} as Inputs,
{
"target": {
"meta": {
"tags": [
"user/app:release1",
"user/app:my",
"user/app:custom",
"user/app:tags",
"user/app:latest"
],
"args": {
"DOCKER_META_IMAGES": "user/app",
"DOCKER_META_VERSION": "release1",
}
}
}
},
{
"target": {
"meta": {
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "release1"
}
}
}
}
],
[
'bakeFile04',
'event_tag_20200110-RC2.env',
{
images: ['user/app'],
tags: [
`type=match,pattern=\\d{8}`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
],
flavor: [
`latest=false`
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"user/app:20200110",
"user/app:my",
"user/app:custom",
"user/app:tags"
],
"args": {
"DOCKER_META_IMAGES": "user/app",
"DOCKER_META_VERSION": "20200110",
}
}
}
},
{
"target": {
"docker-metadata-action": {
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "20200110"
}
}
}
}
],
[
'bakeFile05',
'event_tag_v1.1.1.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=semver,pattern={{version}}`,
`type=semver,pattern={{major}}.{{minor}}`,
`type=semver,pattern={{major}}`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"org/app:1.1.1",
"org/app:1.1",
"org/app:1",
"org/app:my",
"org/app:custom",
"org/app:tags",
"org/app:latest",
"ghcr.io/user/app:1.1.1",
"ghcr.io/user/app:1.1",
"ghcr.io/user/app:1",
"ghcr.io/user/app:my",
"ghcr.io/user/app:custom",
"ghcr.io/user/app:tags",
"ghcr.io/user/app:latest"
],
"args": {
"DOCKER_META_IMAGES": "org/app,ghcr.io/user/app",
"DOCKER_META_VERSION": "1.1.1",
}
}
}
},
{
"target": {
"docker-metadata-action": {
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "1.1.1"
}
}
}
}
],
[
'bakeFile06',
'event_tag_v1.1.1.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"org/app:my",
"org/app:custom",
"org/app:tags",
"ghcr.io/user/app:my",
"ghcr.io/user/app:custom",
"ghcr.io/user/app:tags"
],
"args": {
"DOCKER_META_IMAGES": "org/app,ghcr.io/user/app",
"DOCKER_META_VERSION": "my",
}
}
}
},
{
"target": {
"docker-metadata-action": {
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "my"
}
}
}
}
],
[
'bakeFile07',
'event_tag_v1.1.1.env',
{
images: ['org/app'],
labels: [
"maintainer=CrazyMax",
"org.opencontainers.image.title=MyCustom=Title",
"org.opencontainers.image.description=Another description",
"org.opencontainers.image.vendor=MyCompany",
],
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"org/app:v1.1.1",
"org/app:latest"
],
"args": {
"DOCKER_META_IMAGES": "org/app",
"DOCKER_META_VERSION": "v1.1.1",
}
}
}
},
{
"target": {
"docker-metadata-action": {
"labels": {
"maintainer": "CrazyMax",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "Another description",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "MyCustom=Title",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.vendor": "MyCompany",
"org.opencontainers.image.version": "v1.1.1"
}
}
}
}
]
])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exBakeTags: unknown, exBakeLabels: unknown) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const bakeFileTags = meta.getBakeFile('tags');
expect(JSON.parse(fs.readFileSync(bakeFileTags, 'utf8'))).toEqual(exBakeTags);
const bakeFileLabels = meta.getBakeFile('labels');
expect(JSON.parse(fs.readFileSync(bakeFileLabels, 'utf8'))).toEqual(exBakeLabels);
});
});
describe('bakeFileTagsLabels', () => {
// prettier-ignore
// eslint-disable-next-line jest/expect-expect
test.each([
[
'bakeFileTagsLabels01',
'event_push_dev.env',
{
images: ['user/app'],
@ -3764,254 +4115,6 @@ describe('bake', () => {
}
}
}
],
[
'bake02',
'event_push_dev.env',
{
images: ['user/app'],
tags: [
`type=ref,event=branch`,
`type=raw,my`
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"user/app:dev",
"user/app:my",
],
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "dev"
},
"args": {
"DOCKER_META_IMAGES": "user/app",
"DOCKER_META_VERSION": "dev",
}
}
}
}
],
[
'bake03',
'event_tag_release1.env',
{
images: ['user/app'],
tags: [
`type=ref,event=tag`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
],
bakeTarget: "meta"
} as Inputs,
{
"target": {
"meta": {
"tags": [
"user/app:release1",
"user/app:my",
"user/app:custom",
"user/app:tags",
"user/app:latest"
],
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "release1"
},
"args": {
"DOCKER_META_IMAGES": "user/app",
"DOCKER_META_VERSION": "release1",
}
}
}
}
],
[
'bake04',
'event_tag_20200110-RC2.env',
{
images: ['user/app'],
tags: [
`type=match,pattern=\\d{8}`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
],
flavor: [
`latest=false`
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"user/app:20200110",
"user/app:my",
"user/app:custom",
"user/app:tags"
],
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "20200110"
},
"args": {
"DOCKER_META_IMAGES": "user/app",
"DOCKER_META_VERSION": "20200110",
}
}
}
}
],
[
'bake05',
'event_tag_v1.1.1.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=semver,pattern={{version}}`,
`type=semver,pattern={{major}}.{{minor}}`,
`type=semver,pattern={{major}}`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"org/app:1.1.1",
"org/app:1.1",
"org/app:1",
"org/app:my",
"org/app:custom",
"org/app:tags",
"org/app:latest",
"ghcr.io/user/app:1.1.1",
"ghcr.io/user/app:1.1",
"ghcr.io/user/app:1",
"ghcr.io/user/app:my",
"ghcr.io/user/app:custom",
"ghcr.io/user/app:tags",
"ghcr.io/user/app:latest"
],
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "1.1.1"
},
"args": {
"DOCKER_META_IMAGES": "org/app,ghcr.io/user/app",
"DOCKER_META_VERSION": "1.1.1",
}
}
}
}
],
[
'bake06',
'event_tag_v1.1.1.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
]
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"org/app:my",
"org/app:custom",
"org/app:tags",
"ghcr.io/user/app:my",
"ghcr.io/user/app:custom",
"ghcr.io/user/app:tags"
],
"labels": {
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "my"
},
"args": {
"DOCKER_META_IMAGES": "org/app,ghcr.io/user/app",
"DOCKER_META_VERSION": "my",
}
}
}
}
],
[
'bake07',
'event_tag_v1.1.1.env',
{
images: ['org/app'],
labels: [
"maintainer=CrazyMax",
"org.opencontainers.image.title=MyCustom=Title",
"org.opencontainers.image.description=Another description",
"org.opencontainers.image.vendor=MyCompany",
],
} as Inputs,
{
"target": {
"docker-metadata-action": {
"tags": [
"org/app:v1.1.1",
"org/app:latest"
],
"labels": {
"maintainer": "CrazyMax",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description": "Another description",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.title": "MyCustom=Title",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.vendor": "MyCompany",
"org.opencontainers.image.version": "v1.1.1"
},
"args": {
"DOCKER_META_IMAGES": "org/app",
"DOCKER_META_VERSION": "v1.1.1",
}
}
}
}
]
])('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)));
@ -4020,7 +4123,7 @@ describe('bake', () => {
const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const bakeFile = meta.getBakeFile();
const bakeFile = meta.getBakeFileTagsLabels();
expect(JSON.parse(fs.readFileSync(bakeFile, 'utf8'))).toEqual(exBakeDefinition);
});
});

View file

@ -44,10 +44,14 @@ outputs:
description: 'Generated Docker tags'
labels:
description: 'Generated Docker labels'
bake-file:
description: 'Bake definiton file'
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:
description: 'Bake definition file with tags and labels'
runs:
using: 'node20'

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

@ -81,11 +81,16 @@ actionsToolkit.run(
setOutput('json', JSON.stringify(jsonOutput));
});
// Bake file definition
const bakeFile: string = meta.getBakeFile();
await core.group(`Bake file definition`, async () => {
core.info(fs.readFileSync(bakeFile, 'utf8'));
setOutput('bake-file', bakeFile);
});
// Bake files
for (const kind of ['tags', 'labels']) {
const bakeFile: string = meta.getBakeFile(kind);
await core.group(`Bake file definition (${kind})`, async () => {
core.info(fs.readFileSync(bakeFile, 'utf8'));
setOutput(`bake-file-${kind}`, bakeFile);
});
}
// Bake file with tags and labels
setOutput(`bake-file`, meta.getBakeFileTagsLabels());
}
);

View file

@ -494,7 +494,33 @@ export class Meta {
};
}
public getBakeFile(): string {
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];
return res;
}, {})
});
default:
throw new Error(`Unknown bake file type: ${kind}`);
}
}
public getBakeFileTagsLabels(): string {
const bakeFile = path.join(ToolkitContext.tmpDir(), 'docker-metadata-action-bake.json');
fs.writeFileSync(
bakeFile,
@ -522,7 +548,12 @@ export class Meta {
2
)
);
return bakeFile;
}
private generateBakeFile(name: string, dt): string {
const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake-${name}.json`);
fs.writeFileSync(bakeFile, JSON.stringify({target: {[this.inputs.bakeTarget]: dt}}, null, 2));
return bakeFile;
}