Skip to content

Commit 1ecdfcc

Browse files
committed
feat(docker)!: add digest to images uri
1 parent 0273b0e commit 1ecdfcc

2 files changed

Lines changed: 52 additions & 79 deletions

File tree

.github/workflows/__test-action-docker-build-image.yml

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,12 @@ jobs:
6565
"hoverkraft-tech/ci-github-container/application-test",
6666
`"repository" output is not valid`
6767
);
68-
assert.equal(
69-
builtImage.digests.length,
70-
1,
71-
`"digests" output is not valid`
72-
);
7368
assert.match(
74-
builtImage.digests[0],
75-
/^ghcr\.io\/hoverkraft-tech\/ci-github-container\/application-test@sha256:[a-f0-9]{64}$/,
76-
`"digests" output is not valid`
69+
builtImage.digest,
70+
/^sha256:[a-f0-9]{64}$/,
71+
`"digest" output is not valid`
7772
);
73+
assert.equal(builtImage.image, `ghcr.io/hoverkraft-tech/ci-github-container/application-test@${builtImage.digest}`, `"image" output is not valid`);
7874
7975
// Annotations
8076
assert.match(
@@ -123,21 +119,6 @@ jobs:
123119
assert.equal(builtImage.tags[0], prShaTag, `"tags" output is not valid`);
124120
assert.equal(builtImage.tags[1], prTag, `"tags" output is not valid`);
125121
126-
assert.equal(
127-
builtImage.images.length,
128-
2,
129-
`"images" output is not valid`
130-
);
131-
assert.equal(
132-
builtImage.images[0],
133-
`ghcr.io/hoverkraft-tech/ci-github-container/application-test:${prShaTag}`,
134-
`"images" output is not valid`
135-
);
136-
assert.equal(
137-
builtImage.images[1],
138-
`ghcr.io/hoverkraft-tech/ci-github-container/application-test:${prTag}`,
139-
`"images" output is not valid`
140-
);
141122
assert.equal(
142123
builtImage.annotations["org.opencontainers.image.version"],
143124
prTag,
@@ -151,19 +132,6 @@ jobs:
151132
assert.equal(builtImage.tags[0], refTag, `"tags" output is not valid`);
152133
assert.equal(builtImage.tags[1], "latest", `"tags" output is not valid`);
153134
154-
assert.equal(builtImage.images.length, 2, `"images" output is not valid`);
155-
assert.equal(
156-
builtImage.images[0],
157-
`ghcr.io/hoverkraft-tech/ci-github-container/application-test:${refTag}`,
158-
`"images" output is not valid`
159-
);
160-
161-
assert.equal(
162-
builtImage.images[1],
163-
`ghcr.io/hoverkraft-tech/ci-github-container/application-test:latest`,
164-
`"images" output is not valid`
165-
);
166-
167135
assert.equal(
168136
builtImage.annotations["org.opencontainers.image.version"],
169137
refTag,
@@ -177,20 +145,18 @@ jobs:
177145
username: ${{ github.repository_owner }}
178146
password: ${{ github.token }}
179147

180-
- name: Assert - Check docker digests
148+
- name: Assert - Check docker digest
181149
run: |
182-
DIGESTS=$(echo '${{ steps.build-image.outputs.built-image }}' | jq -r '.digests[]')
183-
for DIGEST in $DIGESTS; do
184-
if ! docker pull "$DIGEST"; then
185-
echo "Failed to pull $DIGEST"
186-
exit 1
187-
fi
188-
189-
if ! docker manifest inspect "$DIGEST"; then
190-
echo "Failed to inspect $DIGEST"
191-
exit 1
192-
fi
193-
done
150+
DIGEST=$(echo '${{ steps.build-image.outputs.built-image }}' | jq -r '.digest')
151+
if ! docker pull "$DIGEST"; then
152+
echo "Failed to pull $DIGEST"
153+
exit 1
154+
fi
155+
156+
if ! docker manifest inspect "$DIGEST"; then
157+
echo "Failed to inspect $DIGEST"
158+
exit 1
159+
fi
194160
195161
tests-with-given-tag:
196162
name: Test for "docker/build-image" action with given tag
@@ -249,19 +215,17 @@ jobs:
249215
username: ${{ github.repository_owner }}
250216
password: ${{ github.token }}
251217

252-
- name: Assert - Check docker digests
218+
- name: Assert - Check docker image
253219
run: |
254-
DIGESTS=$(echo '${{ steps.build-image.outputs.built-image }}' | jq -r '.digests[]')
255-
for DIGEST in $DIGESTS; do
256-
if ! docker pull "$DIGEST"; then
257-
echo "Failed to pull $DIGEST"
258-
exit 1
259-
fi
260-
261-
if ! docker manifest inspect "$DIGEST"; then
262-
echo "Failed to inspect $DIGEST"
263-
exit 1
264-
fi
265-
done
220+
IMAGE=$(echo '${{ steps.build-image.outputs.built-image }}' | jq -r '.image')
221+
if ! docker pull "$IMAGE"; then
222+
echo "Failed to pull $IMAGE"
223+
exit 1
224+
fi
225+
226+
if ! docker manifest inspect "$IMAGE"; then
227+
echo "Failed to inspect $IMAGE"
228+
exit 1
229+
fi
266230
267231
# jscpd:ignore-end

actions/docker/build-image/action.yml

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
---
2-
name: "Build image"
3-
description: "Action to build an image with Docker for a specific platform"
4-
author: Hoverkraft
2+
name: "Docker - Build image"
3+
description: |
4+
Action to build and push a "raw" image with Docker for a specific platform.
5+
This action uses the Docker Buildx plugin to build the image.
6+
It supports caching.
7+
It returns the image digest uri, tags, and annotations, but does not handle it itself.
8+
author: hoverkraft
59
branding:
610
icon: package
7-
color: gray-dark
11+
color: blue
812

913
outputs:
1014
built-image:
@@ -14,17 +18,12 @@ outputs:
1418
"name": "application",
1519
"registry": "ghcr.io",
1620
"repository": "my-org/my-repo/application",
21+
"digest": "sha256:d31aa93410434ac9dcfc9179cac2cb1fd4d7c27f11527addc40299c7c675f49d",
22+
"image": "ghcr.io/my-org/my-repo/application@sha256:d31aa93410434ac9dcfc9179cac2cb1fd4d7c27f11527addc40299c7c675f49d",
1723
"tags": [
1824
"pr-63-5222075",
1925
"pr-63"
2026
],
21-
"images": [
22-
"ghcr.io/my-org/my-repo/application:pr-63-5222075",
23-
"ghcr.io/my-org/my-repo/application:pr-63"
24-
],
25-
"digests": [
26-
"ghcr.io/my-org/my-repo/application@sha256:d31aa93410434ac9dcfc9179cac2cb1fd4d7c27f11527addc40299c7c675f49d"
27-
],
2827
"annotations": {
2928
"org.opencontainers.image.created": "2021-09-30T14:00:00Z",
3029
"org.opencontainers.image.description": "Application image"
@@ -257,8 +256,7 @@ runs:
257256
}
258257
259258
if (builtMetadata["containerimage.digest"] === undefined) {
260-
core.setFailed('Given "metadata"."containerimage.digest" output is undefined.');
261-
return;
259+
return core.setFailed('Given "metadata"."containerimage.digest" output is undefined.');
262260
}
263261
264262
const name = `${{ inputs.image }}`;
@@ -273,15 +271,26 @@ runs:
273271
.map(tag => tag.replace(/[^\/]+\/[^:]+:(.+)/,'$1').trim())
274272
.filter(tag => tag !== "");
275273
276-
const images = tags.map(tag => `${image}:${tag}`);
277274
const digests = builtMetadata["containerimage.digest"]
278275
.split(",")
279276
.map(digest => {
280277
const cleanedDigest = digest.trim();
281-
return cleanedDigest !== "" ? `${image}@${cleanedDigest}` : null;
278+
return cleanedDigest !== "" ? cleanedDigest : null;
282279
})
283280
.filter(digest => digest !== null);
284281
282+
const uniqueDigests = [...new Set(digests)];
283+
if (uniqueDigests.length === 0) {
284+
return core.setFailed('No valid digests found in "containerimage.digest" output.');
285+
}
286+
287+
if( uniqueDigests.length > 1 ) {
288+
return core.setFailed(`Multiple digests found: ${uniqueDigests.join(", ")}.`);
289+
}
290+
291+
const digest = uniqueDigests[0];
292+
const image = `${image}@${digest}`;
293+
285294
const annotations = `${{ steps.metadata.outputs.annotations }}`
286295
.split("\n")
287296
.map(annotation => {
@@ -303,8 +312,8 @@ runs:
303312
annotations,
304313
registry,
305314
repository,
306-
images,
307-
digests,
315+
image,
316+
digest
308317
};
309318
310319
core.setOutput("built-image", JSON.stringify(builtImage));

0 commit comments

Comments
 (0)