Skip to content

Commit b0b7751

Browse files
authored
Merge pull request #412 from crazy-max/metadata-file
Add `metadata` output
2 parents 09d66c2 + c0b121f commit b0b7751

File tree

8 files changed

+104
-20
lines changed

8 files changed

+104
-20
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ Following inputs can be used as `step.with` keys
213213

214214
Following outputs are available
215215

216-
| Name | Type | Description |
217-
|---------------|---------|---------------------------------------|
218-
| `digest` | String | Image content-addressable identifier also called a digest |
216+
| Name | Type | Description |
217+
|-------------------|---------|---------------------------------------|
218+
| `digest` | String | Image content-addressable identifier also called a digest |
219+
| `metadata` | JSON | Build result metadata |
219220

220221
## Troubleshooting
221222

__tests__/buildx.test.ts

+15
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

+24-1
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

+2
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

+32-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/buildx.ts

+12
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

+3-1
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

+12-7
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)