Skip to content

Commit 7cf8e21

Browse files
committed
bake: split definition into two files
Allows to either include tags or labels or both definitions. Keep bake-file output for backward compatibility. Signed-off-by: CrazyMax <[email protected]>
1 parent 62339db commit 7cf8e21

File tree

6 files changed

+207
-56
lines changed

6 files changed

+207
-56
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ jobs:
323323
with:
324324
files: |
325325
./test/docker-bake.hcl
326-
${{ steps.docker_meta.outputs.bake-file }}
326+
${{ steps.docker_meta.outputs.bake-file-tags }}
327+
${{ steps.docker_meta.outputs.bake-file-labels }}
327328
targets: |
328329
release
329330

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,17 @@ jobs:
226226
with:
227227
files: |
228228
./docker-bake.hcl
229-
${{ steps.meta.outputs.bake-file }}
229+
${{ steps.meta.outputs.bake-file-tags }}
230+
${{ steps.meta.outputs.bake-file-labels }}
230231
targets: build
231232
```
232233

233-
Content of `${{ steps.meta.outputs.bake-file }}` file will look like this with
234-
`refs/tags/v1.2.3` ref:
234+
> **Note**
235+
>
236+
> You can also use the `bake-file` output that combines tags and labels.
237+
238+
Content of `${{ steps.meta.outputs.bake-file-tags }}` and `${{ steps.meta.outputs.bake-file-labels }}`
239+
files merged will look like this with `refs/tags/v1.2.3` ref:
235240

236241
```json
237242
{
@@ -291,21 +296,23 @@ The following inputs can be used as `step.with` keys:
291296

292297
The following outputs are available:
293298

294-
| Name | Type | Description |
295-
|-------------|--------|----------------------------------------------------------------------------|
296-
| `version` | String | Docker image version |
297-
| `tags` | String | Docker tags |
298-
| `labels` | String | Docker labels |
299-
| `json` | String | JSON output of tags and labels |
300-
| `bake-file` | File | [Bake file definition](https://docs.docker.com/build/bake/reference/) path |
299+
| Name | Type | Description |
300+
|--------------------|--------|----------------------------------------------------------------------------------------|
301+
| `version` | String | Docker image version |
302+
| `tags` | String | Docker tags |
303+
| `labels` | String | Docker labels |
304+
| `json` | String | JSON output of tags and labels |
305+
| `bake-file-tags` | File | [Bake file definition](https://docs.docker.com/build/bake/reference/) path with tags |
306+
| `bake-file-labels` | File | [Bake file definition](https://docs.docker.com/build/bake/reference/) path with labels |
301307

302308
Alternatively, each output is also exported as an environment variable:
303309

304310
* `DOCKER_METADATA_OUTPUT_VERSION`
305311
* `DOCKER_METADATA_OUTPUT_TAGS`
306312
* `DOCKER_METADATA_OUTPUT_LABELS`
307313
* `DOCKER_METADATA_OUTPUT_JSON`
308-
* `DOCKER_METADATA_OUTPUT_BAKE_FILE`
314+
* `DOCKER_METADATA_OUTPUT_BAKE_FILE_TAGS`
315+
* `DOCKER_METADATA_OUTPUT_BAKE_FILE_LABELS`
309316

310317
So it can be used with our [Docker Build Push action](https://github.com/docker/build-push-action/):
311318

__tests__/meta.test.ts

Lines changed: 138 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,12 +3719,12 @@ describe('json', () => {
37193719
});
37203720
});
37213721

3722-
describe('bake', () => {
3722+
describe('bakeFile', () => {
37233723
// prettier-ignore
37243724
// eslint-disable-next-line jest/expect-expect
37253725
test.each([
37263726
[
3727-
'bake01',
3727+
'bakeFile01',
37283728
'event_push_dev.env',
37293729
{
37303730
images: ['user/app'],
@@ -3747,6 +3747,16 @@ describe('bake', () => {
37473747
"user/app:custom",
37483748
"user/app:tags"
37493749
],
3750+
"args": {
3751+
"DOCKER_META_IMAGES": "user/app",
3752+
"DOCKER_META_VERSION": "dev",
3753+
}
3754+
}
3755+
}
3756+
},
3757+
{
3758+
"target": {
3759+
"docker-metadata-action": {
37503760
"labels": {
37513761
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
37523762
"org.opencontainers.image.description": "This your first repo!",
@@ -3756,17 +3766,13 @@ describe('bake', () => {
37563766
"org.opencontainers.image.title": "Hello-World",
37573767
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
37583768
"org.opencontainers.image.version": "dev"
3759-
},
3760-
"args": {
3761-
"DOCKER_META_IMAGES": "user/app",
3762-
"DOCKER_META_VERSION": "dev",
37633769
}
37643770
}
37653771
}
37663772
}
37673773
],
37683774
[
3769-
'bake02',
3775+
'bakeFile02',
37703776
'event_push_dev.env',
37713777
{
37723778
images: ['user/app'],
@@ -3782,6 +3788,16 @@ describe('bake', () => {
37823788
"user/app:dev",
37833789
"user/app:my",
37843790
],
3791+
"args": {
3792+
"DOCKER_META_IMAGES": "user/app",
3793+
"DOCKER_META_VERSION": "dev",
3794+
}
3795+
}
3796+
}
3797+
},
3798+
{
3799+
"target": {
3800+
"docker-metadata-action": {
37853801
"labels": {
37863802
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
37873803
"org.opencontainers.image.description": "This your first repo!",
@@ -3791,17 +3807,13 @@ describe('bake', () => {
37913807
"org.opencontainers.image.title": "Hello-World",
37923808
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
37933809
"org.opencontainers.image.version": "dev"
3794-
},
3795-
"args": {
3796-
"DOCKER_META_IMAGES": "user/app",
3797-
"DOCKER_META_VERSION": "dev",
37983810
}
37993811
}
38003812
}
38013813
}
38023814
],
38033815
[
3804-
'bake03',
3816+
'bakeFile03',
38053817
'event_tag_release1.env',
38063818
{
38073819
images: ['user/app'],
@@ -3823,6 +3835,16 @@ describe('bake', () => {
38233835
"user/app:tags",
38243836
"user/app:latest"
38253837
],
3838+
"args": {
3839+
"DOCKER_META_IMAGES": "user/app",
3840+
"DOCKER_META_VERSION": "release1",
3841+
}
3842+
}
3843+
}
3844+
},
3845+
{
3846+
"target": {
3847+
"meta": {
38263848
"labels": {
38273849
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
38283850
"org.opencontainers.image.description": "This your first repo!",
@@ -3832,17 +3854,13 @@ describe('bake', () => {
38323854
"org.opencontainers.image.title": "Hello-World",
38333855
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
38343856
"org.opencontainers.image.version": "release1"
3835-
},
3836-
"args": {
3837-
"DOCKER_META_IMAGES": "user/app",
3838-
"DOCKER_META_VERSION": "release1",
38393857
}
38403858
}
38413859
}
38423860
}
38433861
],
38443862
[
3845-
'bake04',
3863+
'bakeFile04',
38463864
'event_tag_20200110-RC2.env',
38473865
{
38483866
images: ['user/app'],
@@ -3865,6 +3883,16 @@ describe('bake', () => {
38653883
"user/app:custom",
38663884
"user/app:tags"
38673885
],
3886+
"args": {
3887+
"DOCKER_META_IMAGES": "user/app",
3888+
"DOCKER_META_VERSION": "20200110",
3889+
}
3890+
}
3891+
}
3892+
},
3893+
{
3894+
"target": {
3895+
"docker-metadata-action": {
38683896
"labels": {
38693897
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
38703898
"org.opencontainers.image.description": "This your first repo!",
@@ -3874,17 +3902,13 @@ describe('bake', () => {
38743902
"org.opencontainers.image.title": "Hello-World",
38753903
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
38763904
"org.opencontainers.image.version": "20200110"
3877-
},
3878-
"args": {
3879-
"DOCKER_META_IMAGES": "user/app",
3880-
"DOCKER_META_VERSION": "20200110",
38813905
}
38823906
}
38833907
}
38843908
}
38853909
],
38863910
[
3887-
'bake05',
3911+
'bakeFile05',
38883912
'event_tag_v1.1.1.env',
38893913
{
38903914
images: ['org/app', 'ghcr.io/user/app'],
@@ -3916,6 +3940,16 @@ describe('bake', () => {
39163940
"ghcr.io/user/app:tags",
39173941
"ghcr.io/user/app:latest"
39183942
],
3943+
"args": {
3944+
"DOCKER_META_IMAGES": "org/app,ghcr.io/user/app",
3945+
"DOCKER_META_VERSION": "1.1.1",
3946+
}
3947+
}
3948+
}
3949+
},
3950+
{
3951+
"target": {
3952+
"docker-metadata-action": {
39193953
"labels": {
39203954
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
39213955
"org.opencontainers.image.description": "This your first repo!",
@@ -3925,17 +3959,13 @@ describe('bake', () => {
39253959
"org.opencontainers.image.title": "Hello-World",
39263960
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
39273961
"org.opencontainers.image.version": "1.1.1"
3928-
},
3929-
"args": {
3930-
"DOCKER_META_IMAGES": "org/app,ghcr.io/user/app",
3931-
"DOCKER_META_VERSION": "1.1.1",
39323962
}
39333963
}
39343964
}
39353965
}
39363966
],
39373967
[
3938-
'bake06',
3968+
'bakeFile06',
39393969
'event_tag_v1.1.1.env',
39403970
{
39413971
images: ['org/app', 'ghcr.io/user/app'],
@@ -3956,6 +3986,16 @@ describe('bake', () => {
39563986
"ghcr.io/user/app:custom",
39573987
"ghcr.io/user/app:tags"
39583988
],
3989+
"args": {
3990+
"DOCKER_META_IMAGES": "org/app,ghcr.io/user/app",
3991+
"DOCKER_META_VERSION": "my",
3992+
}
3993+
}
3994+
}
3995+
},
3996+
{
3997+
"target": {
3998+
"docker-metadata-action": {
39593999
"labels": {
39604000
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
39614001
"org.opencontainers.image.description": "This your first repo!",
@@ -3965,17 +4005,13 @@ describe('bake', () => {
39654005
"org.opencontainers.image.title": "Hello-World",
39664006
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
39674007
"org.opencontainers.image.version": "my"
3968-
},
3969-
"args": {
3970-
"DOCKER_META_IMAGES": "org/app,ghcr.io/user/app",
3971-
"DOCKER_META_VERSION": "my",
39724008
}
39734009
}
39744010
}
39754011
}
39764012
],
39774013
[
3978-
'bake07',
4014+
'bakeFile07',
39794015
'event_tag_v1.1.1.env',
39804016
{
39814017
images: ['org/app'],
@@ -3993,6 +4029,16 @@ describe('bake', () => {
39934029
"org/app:v1.1.1",
39944030
"org/app:latest"
39954031
],
4032+
"args": {
4033+
"DOCKER_META_IMAGES": "org/app",
4034+
"DOCKER_META_VERSION": "v1.1.1",
4035+
}
4036+
}
4037+
}
4038+
},
4039+
{
4040+
"target": {
4041+
"docker-metadata-action": {
39964042
"labels": {
39974043
"maintainer": "CrazyMax",
39984044
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
@@ -4004,10 +4050,67 @@ describe('bake', () => {
40044050
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
40054051
"org.opencontainers.image.vendor": "MyCompany",
40064052
"org.opencontainers.image.version": "v1.1.1"
4053+
}
4054+
}
4055+
}
4056+
}
4057+
]
4058+
])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exBakeTags: unknown, exBakeLabels: unknown) => {
4059+
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
4060+
4061+
const toolkit = new Toolkit();
4062+
const repo = await toolkit.github.repoData();
4063+
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
4064+
4065+
const bakeFileTags = meta.getBakeFile('tags');
4066+
expect(JSON.parse(fs.readFileSync(bakeFileTags, 'utf8'))).toEqual(exBakeTags);
4067+
4068+
const bakeFileLabels = meta.getBakeFile('labels');
4069+
expect(JSON.parse(fs.readFileSync(bakeFileLabels, 'utf8'))).toEqual(exBakeLabels);
4070+
});
4071+
});
4072+
4073+
describe('bakeFileTagsLabels', () => {
4074+
// prettier-ignore
4075+
// eslint-disable-next-line jest/expect-expect
4076+
test.each([
4077+
[
4078+
'bakeFileTagsLabels01',
4079+
'event_push_dev.env',
4080+
{
4081+
images: ['user/app'],
4082+
tags: [
4083+
`type=ref,event=branch`,
4084+
`type=raw,my`,
4085+
`type=raw,custom`,
4086+
`type=raw,tags`
4087+
],
4088+
labels: [
4089+
"invalid"
4090+
]
4091+
} as Inputs,
4092+
{
4093+
"target": {
4094+
"docker-metadata-action": {
4095+
"tags": [
4096+
"user/app:dev",
4097+
"user/app:my",
4098+
"user/app:custom",
4099+
"user/app:tags"
4100+
],
4101+
"labels": {
4102+
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
4103+
"org.opencontainers.image.description": "This your first repo!",
4104+
"org.opencontainers.image.licenses": "MIT",
4105+
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
4106+
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
4107+
"org.opencontainers.image.title": "Hello-World",
4108+
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
4109+
"org.opencontainers.image.version": "dev"
40074110
},
40084111
"args": {
4009-
"DOCKER_META_IMAGES": "org/app",
4010-
"DOCKER_META_VERSION": "v1.1.1",
4112+
"DOCKER_META_IMAGES": "user/app",
4113+
"DOCKER_META_VERSION": "dev",
40114114
}
40124115
}
40134116
}
@@ -4020,7 +4123,7 @@ describe('bake', () => {
40204123
const repo = await toolkit.github.repoData();
40214124
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
40224125

4023-
const bakeFile = meta.getBakeFile();
4126+
const bakeFile = meta.getBakeFileTagsLabels();
40244127
expect(JSON.parse(fs.readFileSync(bakeFile, 'utf8'))).toEqual(exBakeDefinition);
40254128
});
40264129
});

0 commit comments

Comments
 (0)