Skip to content

Commit c0b121f

Browse files
committed
Add metadata output
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
1 parent ad1d2e9 commit c0b121f

File tree

9 files changed

+104
-47
lines changed

9 files changed

+104
-47
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ jobs:
7070
name: Inspect
7171
run: |
7272
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
73-
-
74-
name: Image digest
75-
run: echo ${{ steps.docker_build.outputs.digest }}
7673
-
7774
name: Check digest
7875
run: |
@@ -133,9 +130,6 @@ jobs:
133130
name: Inspect
134131
run: |
135132
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
136-
-
137-
name: Image digest
138-
run: echo ${{ steps.docker_build.outputs.digest }}
139133
-
140134
name: Check digest
141135
run: |
@@ -191,9 +185,6 @@ jobs:
191185
name: Inspect
192186
run: |
193187
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
194-
-
195-
name: Image digest
196-
run: echo ${{ steps.docker_build.outputs.digest }}
197188
-
198189
name: Check digest
199190
run: |
@@ -392,9 +383,6 @@ jobs:
392383
name: Inspect
393384
run: |
394385
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
395-
-
396-
name: Image digest
397-
run: echo ${{ steps.docker_build.outputs.digest }}
398386
-
399387
name: Check digest
400388
run: |
@@ -447,9 +435,6 @@ jobs:
447435
name: Inspect (1)
448436
run: |
449437
docker buildx imagetools inspect localhost:5000/name/app:latest
450-
-
451-
name: Image digest (1)
452-
run: echo ${{ steps.docker_build.outputs.digest }}
453438
-
454439
name: Check digest (1)
455440
run: |
@@ -480,9 +465,6 @@ jobs:
480465
name: Inspect (2)
481466
run: |
482467
docker buildx imagetools inspect localhost:5000/name/app:latest
483-
-
484-
name: Image digest (2)
485-
run: echo ${{ steps.docker_build2.outputs.digest }}
486468
-
487469
name: Check digest (2)
488470
run: |
@@ -557,9 +539,6 @@ jobs:
557539
name: Inspect
558540
run: |
559541
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
560-
-
561-
name: Image digest
562-
run: echo ${{ steps.docker_build.outputs.digest }}
563542
-
564543
name: Check digest
565544
run: |
@@ -622,9 +601,6 @@ jobs:
622601
name: Inspect
623602
run: |
624603
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
625-
-
626-
name: Image digest
627-
run: echo ${{ steps.docker_build.outputs.digest }}
628604
-
629605
name: Check digest
630606
run: |

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ jobs:
100100
with:
101101
push: true
102102
tags: user/app:latest
103-
-
104-
name: Image digest
105-
run: echo ${{ steps.docker_build.outputs.digest }}
106103
```
107104
108105
Building from the current repository automatically uses the [GitHub Token](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token)
@@ -223,9 +220,10 @@ Following inputs can be used as `step.with` keys
223220

224221
Following outputs are available
225222

226-
| Name | Type | Description |
227-
|---------------|---------|---------------------------------------|
228-
| `digest` | String | Image content-addressable identifier also called a digest |
223+
| Name | Type | Description |
224+
|-------------------|---------|---------------------------------------|
225+
| `digest` | String | Image content-addressable identifier also called a digest |
226+
| `metadata` | JSON | Build result metadata |
229227

230228
## Troubleshooting
231229

__tests__/buildx.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import * as context from '../src/context';
88

99
const tmpNameSync = path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep);
1010
const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
11+
const metadata = `{
12+
"containerimage.config.digest": "sha256:059b68a595b22564a1cbc167af369349fdc2ecc1f7bc092c2235cbf601a795fd",
13+
"containerimage.digest": "sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c"
14+
}`;
1115

1216
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
1317
const tmpDir = path.join('/tmp/.docker-build-push-jest').split(path.sep).join(path.posix.sep);
@@ -32,6 +36,17 @@ describe('getImageID', () => {
3236
});
3337
});
3438

39+
describe('getMetadata', () => {
40+
it('matches', async () => {
41+
const metadataFile = await buildx.getMetadataFile();
42+
console.log(`metadataFile: ${metadataFile}`);
43+
await fs.writeFileSync(metadataFile, metadata);
44+
const expected = await buildx.getMetadata();
45+
console.log(`metadata: ${expected}`);
46+
expect(expected).toEqual(metadata);
47+
});
48+
});
49+
3550
describe('isLocalOrTarExporter', () => {
3651
// prettier-ignore
3752
test.each([

__tests__/context.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,30 @@ ccc`],
425425
'--output', 'type=local,dest=./release-out',
426426
'.'
427427
]
428-
]
428+
],
429+
[
430+
'0.6.0',
431+
new Map<string, string>([
432+
['context', '.'],
433+
['tag', 'localhost:5000/name/app:latest'],
434+
['file', './test/Dockerfile'],
435+
['network', 'host'],
436+
['load', 'false'],
437+
['no-cache', 'false'],
438+
['push', 'true'],
439+
['pull', 'false']
440+
]),
441+
[
442+
'buildx',
443+
'build',
444+
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
445+
'--metadata-file', '/tmp/.docker-build-push-jest/metadata-file',
446+
'--file', './test/Dockerfile',
447+
'--network', 'host',
448+
'--push',
449+
'.'
450+
]
451+
],
429452
])(
430453
'given %p with %p as inputs, returns %p',
431454
async (buildxVersion: string, inputs: Map<string, any>, expected: Array<string>) => {

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ inputs:
7979
outputs:
8080
digest:
8181
description: 'Image content-addressable identifier also called a digest'
82+
metadata:
83+
description: 'Build result metadata'
8284

8385
runs:
8486
using: 'node12'

dist/index.js

Lines changed: 32 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/buildx.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ export async function getImageID(): Promise<string | undefined> {
1818
return fs.readFileSync(iidFile, {encoding: 'utf-8'});
1919
}
2020

21+
export async function getMetadataFile(): Promise<string> {
22+
return path.join(context.tmpDir(), 'metadata-file').split(path.sep).join(path.posix.sep);
23+
}
24+
25+
export async function getMetadata(): Promise<string | undefined> {
26+
const metadataFile = await getMetadataFile();
27+
if (!fs.existsSync(metadataFile)) {
28+
return undefined;
29+
}
30+
return fs.readFileSync(metadataFile, {encoding: 'utf-8'});
31+
}
32+
2133
export async function getSecretString(kvp: string): Promise<string> {
2234
return getSecret(kvp, false);
2335
}

src/context.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import csvparse from 'csv-parse/lib/sync';
22
import * as fs from 'fs';
33
import * as os from 'os';
44
import * as path from 'path';
5-
import * as semver from 'semver';
65
import * as tmp from 'tmp';
76

87
import * as core from '@actions/core';
@@ -122,6 +121,9 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio
122121
if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) {
123122
args.push('--iidfile', await buildx.getImageIDFile());
124123
}
124+
if (buildx.satisfies(buildxVersion, '>=0.6.0')) {
125+
args.push('--metadata-file', await buildx.getMetadataFile());
126+
}
125127
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
126128
args.push('--cache-from', cacheFrom);
127129
});

src/main.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ async function run(): Promise<void> {
3333
}
3434
});
3535

36-
const imageID = await buildx.getImageID();
37-
if (imageID) {
38-
core.startGroup(`Extracting digest`);
39-
core.info(`${imageID}`);
40-
context.setOutput('digest', imageID);
41-
core.endGroup();
42-
}
36+
await core.group(`Setting outputs`, async () => {
37+
const imageID = await buildx.getImageID();
38+
const metadata = await buildx.getMetadata();
39+
if (imageID) {
40+
core.info(`digest=${imageID}`);
41+
context.setOutput('digest', imageID);
42+
}
43+
if (metadata) {
44+
core.info(`metadata=${metadata}`);
45+
context.setOutput('metadata', metadata);
46+
}
47+
});
4348
} catch (error) {
4449
core.setFailed(error.message);
4550
}

0 commit comments

Comments
 (0)