Skip to content

Commit

Permalink
add imageid output and use metadata to set digest output
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Mar 6, 2022
1 parent fe02965 commit af70eb7
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 16 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -499,6 +499,71 @@ jobs:
if: always()
uses: crazy-max/ghaction-dump-context@v1

digest:
runs-on: ubuntu-latest
env:
DOCKER_IMAGE: localhost:5000/name/app
strategy:
fail-fast: false
matrix:
include:
- driver: docker
load: false
push: true
- driver: docker
load: true
push: false
- driver: docker-container
load: false
push: true
- driver: docker-container
load: true
push: false
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
version: https://github.com/crazy-max/buildx.git#moby-imgdgst
driver: ${{ matrix.driver }}
driver-opts: |
network=host
image=moby/buildkit:master
-
name: Build
id: docker_build
uses: ./
with:
context: ./test
load: ${{ matrix.load }}
push: ${{ matrix.push }}
tags: ${{ env.DOCKER_IMAGE }}:latest
platforms: ${{ matrix.platforms }}
-
name: Docker images
run: |
docker image ls --no-trunc
-
name: Check manifest
if: ${{ matrix.push }}
run: |
set -x
docker buildx imagetools inspect ${{ env.DOCKER_IMAGE }}@${{ steps.docker_build.outputs.digest }} --format '{{json .}}'
-
name: Inspect image
if: ${{ matrix.load }}
run: |
set -x
docker image inspect ${{ steps.docker_build.outputs.imageid }}
registry-cache:
runs-on: ubuntu-latest
services:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -241,7 +241,8 @@ Following outputs are available
| Name | Type | Description |
|-------------------|---------|---------------------------------------|
| `digest` | String | Image content-addressable identifier also called a digest |
| `imageid` | String | Image ID |
| `digest` | String | Image digest |
| `metadata` | JSON | Build result metadata |
## Troubleshooting
Expand Down
17 changes: 13 additions & 4 deletions __tests__/buildx.test.ts
Expand Up @@ -7,7 +7,7 @@ import * as buildx from '../src/buildx';
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 imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
const metadata = `{
"containerimage.config.digest": "sha256:059b68a595b22564a1cbc167af369349fdc2ecc1f7bc092c2235cbf601a795fd",
"containerimage.digest": "sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c"
Expand All @@ -28,9 +28,9 @@ jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
describe('getImageID', () => {
it('matches', async () => {
const imageIDFile = await buildx.getImageIDFile();
await fs.writeFileSync(imageIDFile, digest);
const imageID = await buildx.getImageID();
expect(imageID).toEqual(digest);
await fs.writeFileSync(imageIDFile, imageID);
const expected = await buildx.getImageID();
expect(expected).toEqual(imageID);
});
});

Expand All @@ -43,6 +43,15 @@ describe('getMetadata', () => {
});
});

describe('getDigest', () => {
it('matches', async () => {
const metadataFile = await buildx.getMetadataFile();
await fs.writeFileSync(metadataFile, metadata);
const expected = await buildx.getDigest(metadata);
expect(expected).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
});
});

describe('isLocalOrTarExporter', () => {
// prettier-ignore
test.each([
Expand Down
4 changes: 3 additions & 1 deletion action.yml
Expand Up @@ -89,8 +89,10 @@ inputs:
required: false

outputs:
imageid:
description: 'Image ID'
digest:
description: 'Image content-addressable identifier also called a digest'
description: 'Image digest'
metadata:
description: 'Build result metadata'

Expand Down
30 changes: 25 additions & 5 deletions dist/index.js

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

11 changes: 11 additions & 0 deletions src/buildx.ts
Expand Up @@ -34,6 +34,17 @@ export async function getMetadata(): Promise<string | undefined> {
return content;
}

export async function getDigest(metadata: string | undefined): Promise<string | undefined> {
if (metadata === undefined) {
return undefined;
}
const metadataJSON = JSON.parse(metadata);
if (metadataJSON['containerimage.digest']) {
return metadataJSON['containerimage.digest'];
}
return undefined;
}

export async function getSecretString(kvp: string): Promise<string> {
return getSecret(kvp, false);
}
Expand Down
17 changes: 12 additions & 5 deletions src/main.ts
Expand Up @@ -34,16 +34,23 @@ async function run(): Promise<void> {
});

const imageID = await buildx.getImageID();
const metadata = await buildx.getMetadata();
const digest = await buildx.getDigest(metadata);

if (imageID) {
await core.group(`Digest output`, async () => {
await core.group(`ImageID`, async () => {
core.info(imageID);
context.setOutput('digest', imageID);
context.setOutput('imageid', imageID);
});
}
if (digest) {
await core.group(`Digest`, async () => {
core.info(digest);
context.setOutput('digest', digest);
});
}

const metadata = await buildx.getMetadata();
if (metadata) {
await core.group(`Metadata output`, async () => {
await core.group(`Metadata`, async () => {
core.info(metadata);
context.setOutput('metadata', metadata);
});
Expand Down

0 comments on commit af70eb7

Please sign in to comment.