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

Add metadata output #412

Merged
merged 1 commit into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 0 additions & 24 deletions .github/workflows/ci.yml
Expand Up @@ -70,9 +70,6 @@ jobs:
name: Inspect
run: |
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
-
name: Check digest
run: |
Expand Down Expand Up @@ -133,9 +130,6 @@ jobs:
name: Inspect
run: |
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
-
name: Check digest
run: |
Expand Down Expand Up @@ -191,9 +185,6 @@ jobs:
name: Inspect
run: |
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
-
name: Check digest
run: |
Expand Down Expand Up @@ -392,9 +383,6 @@ jobs:
name: Inspect
run: |
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
-
name: Check digest
run: |
Expand Down Expand Up @@ -447,9 +435,6 @@ jobs:
name: Inspect (1)
run: |
docker buildx imagetools inspect localhost:5000/name/app:latest
-
name: Image digest (1)
run: echo ${{ steps.docker_build.outputs.digest }}
-
name: Check digest (1)
run: |
Expand Down Expand Up @@ -480,9 +465,6 @@ jobs:
name: Inspect (2)
run: |
docker buildx imagetools inspect localhost:5000/name/app:latest
-
name: Image digest (2)
run: echo ${{ steps.docker_build2.outputs.digest }}
-
name: Check digest (2)
run: |
Expand Down Expand Up @@ -557,9 +539,6 @@ jobs:
name: Inspect
run: |
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
-
name: Check digest
run: |
Expand Down Expand Up @@ -622,9 +601,6 @@ jobs:
name: Inspect
run: |
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
-
name: Check digest
run: |
Expand Down
10 changes: 4 additions & 6 deletions README.md
Expand Up @@ -100,9 +100,6 @@ jobs:
with:
push: true
tags: user/app:latest
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
```

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)
Expand Down Expand Up @@ -223,9 +220,10 @@ Following inputs can be used as `step.with` keys

Following outputs are available

| Name | Type | Description |
|---------------|---------|---------------------------------------|
| `digest` | String | Image content-addressable identifier also called a digest |
| Name | Type | Description |
|-------------------|---------|---------------------------------------|
| `digest` | String | Image content-addressable identifier also called a digest |
| `metadata` | JSON | Build result metadata |

## Troubleshooting

Expand Down
15 changes: 15 additions & 0 deletions __tests__/buildx.test.ts
Expand Up @@ -8,6 +8,10 @@ import * as context from '../src/context';

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

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

describe('getMetadata', () => {
it('matches', async () => {
const metadataFile = await buildx.getMetadataFile();
console.log(`metadataFile: ${metadataFile}`);
await fs.writeFileSync(metadataFile, metadata);
const expected = await buildx.getMetadata();
console.log(`metadata: ${expected}`);
expect(expected).toEqual(metadata);
});
});

describe('isLocalOrTarExporter', () => {
// prettier-ignore
test.each([
Expand Down
25 changes: 24 additions & 1 deletion __tests__/context.test.ts
Expand Up @@ -425,7 +425,30 @@ ccc`],
'--output', 'type=local,dest=./release-out',
'.'
]
]
],
[
'0.6.0',
new Map<string, string>([
['context', '.'],
['tag', 'localhost:5000/name/app:latest'],
['file', './test/Dockerfile'],
['network', 'host'],
['load', 'false'],
['no-cache', 'false'],
['push', 'true'],
['pull', 'false']
]),
[
'buildx',
'build',
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
'--metadata-file', '/tmp/.docker-build-push-jest/metadata-file',
'--file', './test/Dockerfile',
'--network', 'host',
'--push',
'.'
]
],
])(
'given %p with %p as inputs, returns %p',
async (buildxVersion: string, inputs: Map<string, any>, expected: Array<string>) => {
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Expand Up @@ -79,6 +79,8 @@ inputs:
outputs:
digest:
description: 'Image content-addressable identifier also called a digest'
metadata:
description: 'Build result metadata'

runs:
using: 'node12'
Expand Down
40 changes: 32 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/buildx.ts
Expand Up @@ -18,6 +18,18 @@ export async function getImageID(): Promise<string | undefined> {
return fs.readFileSync(iidFile, {encoding: 'utf-8'});
}

export async function getMetadataFile(): Promise<string> {
return path.join(context.tmpDir(), 'metadata-file').split(path.sep).join(path.posix.sep);
}

export async function getMetadata(): Promise<string | undefined> {
const metadataFile = await getMetadataFile();
if (!fs.existsSync(metadataFile)) {
return undefined;
}
return fs.readFileSync(metadataFile, {encoding: 'utf-8'});
}

export async function getSecretString(kvp: string): Promise<string> {
return getSecret(kvp, false);
}
Expand Down
4 changes: 3 additions & 1 deletion src/context.ts
Expand Up @@ -2,7 +2,6 @@ import csvparse from 'csv-parse/lib/sync';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as tmp from 'tmp';

import * as core from '@actions/core';
Expand Down Expand Up @@ -122,6 +121,9 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio
if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) {
args.push('--iidfile', await buildx.getImageIDFile());
}
if (buildx.satisfies(buildxVersion, '>=0.6.0')) {
args.push('--metadata-file', await buildx.getMetadataFile());
}
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
args.push('--cache-from', cacheFrom);
});
Expand Down
19 changes: 12 additions & 7 deletions src/main.ts
Expand Up @@ -33,13 +33,18 @@ async function run(): Promise<void> {
}
});

const imageID = await buildx.getImageID();
if (imageID) {
core.startGroup(`Extracting digest`);
core.info(`${imageID}`);
context.setOutput('digest', imageID);
core.endGroup();
}
await core.group(`Setting outputs`, async () => {
const imageID = await buildx.getImageID();
const metadata = await buildx.getMetadata();
if (imageID) {
core.info(`digest=${imageID}`);
context.setOutput('digest', imageID);
}
if (metadata) {
core.info(`metadata=${metadata}`);
context.setOutput('metadata', metadata);
}
});
} catch (error) {
core.setFailed(error.message);
}
Expand Down