Repository navigation
399 lines (390 loc) · 19 KB
/
Copy pathimage.yml
File metadata and controls
399 lines (390 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Container image verification and publication (issue #227): builds an existing
# release tag for linux/amd64 and linux/arm64 and can publish it to
# ghcr.io/coder/openstorage.
#
# Manual only, on purpose. There is no push, tag, release, or workflow_call
# trigger, so neither a merge nor a release ever publishes an image.
# Each platform is built once, natively, by the stage job, which pushes it by
# digest with no tag. The verify jobs pull those digests and run every check
# and the Compose quickstart smoke on them. publish=true (dispatched from main)
# then waits for a reviewer to approve the ghcr-publish environment and gives
# the checked digests exactly one immutable version tag (vX.Y.Z -> :X.Y.Z)
# without building again, so the tag holds the bytes that were checked. It
# refuses a tag that already exists, records the index digest, and the
# verify-published jobs re-run the checks on the pulled tag.
# A dispatch with the default publish=false stages and checks the digests and
# pushes no tag. Only the stage and publish jobs can write packages. Every image
# they pull (base, Go builder, BuildKit, SBOM scanner) is pinned by digest.
# Runbook: docs/how-to/release.md#4-verify-and-publish-the-container-image.
name: image
on:
workflow_dispatch:
inputs:
tag:
description: "Existing release tag to build (e.g. v0.17.0)"
required: true
type: string
publish:
description: "Tag ghcr.io/coder/openstorage:X.Y.Z (false: stage and check only, no tag)"
required: true
type: boolean
default: false
permissions: {}
# One image run at a time, never cancelled midway: the existence check and the
# push in the publish job must not race another run for the same tag.
concurrency:
group: image
cancel-in-progress: false
env:
IMAGE: ghcr.io/coder/openstorage
jobs:
# Resolve the tag to a commit once. Every later job builds that commit, so a
# tag moved during the run cannot change what gets checked or published.
resolve:
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read
outputs:
tag: ${{ steps.resolve.outputs.tag }}
commit: ${{ steps.resolve.outputs.commit }}
version: ${{ steps.resolve.outputs.version }}
date: ${{ steps.resolve.outputs.date }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false
- name: Require a published release tag on main
id: resolve
env:
TAG: ${{ inputs.tag }}
PUBLISH: ${{ inputs.publish }}
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
run: |
# Fail loudly instead of letting the publish job skip silently.
[ "$PUBLISH" != true ] || [ "$GITHUB_REF" = refs/heads/main ] || { echo "::error::publish=true requires a dispatch from main"; exit 1; }
[[ $TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "::error::tag must look like v1.2.3, got: $TAG"; exit 1; }
commit=$(git rev-parse --verify "refs/tags/$TAG^{commit}")
git merge-base --is-ancestor "$commit" origin/main || { echo "::error::$TAG is not on main"; exit 1; }
# Every job builds the tag's own Dockerfile. Tags cut before it took the
# stamp build args (v0.16.0 and earlier) cannot pass the version check.
git show "$commit:Dockerfile" | grep -qE '^ARG VERSION([= ]|$)' ||
{ echo "::error::$TAG predates version-stamped images; publish a release cut after the image workflow landed"; exit 1; }
[ "$(gh release view "$TAG" --json isDraft --jq .isDraft)" = false ] || { echo "::error::$TAG has no published release"; exit 1; }
# The commit time, not the build time, so every job stamps the same value.
date=$(TZ=UTC0 git show -s --date=format-local:%Y-%m-%dT%H:%M:%SZ --format=%cd "$commit")
{
echo "tag=$TAG"
echo "commit=$commit"
echo "version=${TAG#v}"
echo "date=$date"
} >> "$GITHUB_OUTPUT"
echo "$TAG -> $commit ($date)"
# Build each platform once, natively, and push it by digest with no tag. Nothing
# in this job runs the image. The verify jobs check these digests, and publish
# gives exactly these digests the version tag.
stage:
needs: resolve
strategy:
fail-fast: false
matrix:
include:
- arch: amd64
runner: ubuntu-latest
- arch: arm64
runner: ubuntu-24.04-arm
name: stage (${{ matrix.arch }})
runs-on: ${{ matrix.runner }}
timeout-minutes: 20
permissions:
contents: read
packages: write
env:
ARCH: ${{ matrix.arch }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ needs.resolve.outputs.commit }}
persist-credentials: false
# The BuildKit image holds the push credential, so pin it by digest. A
# native runner needs no QEMU.
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
with:
driver-opts: image=moby/buildkit:v0.35.0@sha256:aa632b921ef07411d2cb77292cd34213d4f7b384001d6fceef1e1f25bf9752bb
- uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Build and push the platform by digest
id: build
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
env:
# This job holds a write token and uploads nothing but the digest file:
# no build summary and no build-record artifact.
DOCKER_BUILD_SUMMARY: false
DOCKER_BUILD_RECORD_UPLOAD: false
with:
# The checked-out tree, not the action's default Git context of the
# dispatching ref (main).
context: .
# The action passes this token to BuildKit as a secret for a Git context.
# Empty, it receives no token whatever the context is.
github-token: ''
platforms: linux/${{ matrix.arch }}
outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=true
build-args: |
VERSION=${{ needs.resolve.outputs.tag }}
COMMIT=${{ needs.resolve.outputs.commit }}
BUILD_DATE=${{ needs.resolve.outputs.date }}
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.revision=${{ needs.resolve.outputs.commit }}
org.opencontainers.image.version=${{ needs.resolve.outputs.version }}
org.opencontainers.image.created=${{ needs.resolve.outputs.date }}
org.opencontainers.image.title=openstorage
org.opencontainers.image.description=Self-hosted, API-first Git storage
org.opencontainers.image.licenses=Apache-2.0
provenance: mode=max
# The default scanner is a mutable tag (stable-1); pin it like the rest.
sbom: generator=docker/buildkit-syft-scanner:1.12.1@sha256:873a7458c3c8ac8e32c7b796c10e1600b07b963d2aa000a72a2d97aaaa2d6315
- name: Record the digest
env:
DIGEST: ${{ steps.build.outputs.digest }}
run: |
[[ $DIGEST =~ ^sha256:[a-f0-9]{64}$ ]]
mkdir digest
echo "$DIGEST" > "digest/$ARCH"
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: digest-${{ matrix.arch }}
path: digest/${{ matrix.arch }}
if-no-files-found: error
# An approval can wait 30 days, and publish needs this file after it.
retention-days: 35
# Pull the staged digest of each platform and run every check on it, the Compose
# quickstart smoke included. The token is read-only.
verify:
needs: [resolve, stage]
strategy:
fail-fast: false
matrix:
include:
- arch: amd64
runner: ubuntu-latest
- arch: arm64
runner: ubuntu-24.04-arm
name: verify (${{ matrix.arch }})
runs-on: ${{ matrix.runner }}
timeout-minutes: 20
permissions:
contents: read
packages: read
env:
ARCH: ${{ matrix.arch }}
TAG: ${{ needs.resolve.outputs.tag }}
COMMIT: ${{ needs.resolve.outputs.commit }}
BUILD_DATE: ${{ needs.resolve.outputs.date }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ needs.resolve.outputs.commit }}
persist-credentials: false
- uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: digest-${{ matrix.arch }}
path: digest
- name: Pull the staged digest
run: |
DIGEST=$(cat "digest/$ARCH")
[[ $DIGEST =~ ^sha256:[a-f0-9]{64}$ ]]
docker pull --platform "linux/$ARCH" "$IMAGE@$DIGEST"
[ "$(docker image inspect --format '{{index .Config.Labels "org.opencontainers.image.revision"}}' "$IMAGE@$DIGEST")" = "$COMMIT" ]
echo "OPENSTORAGE_IMAGE=$(docker image inspect --format '{{.Id}}' "$IMAGE@$DIGEST")" >> "$GITHUB_ENV"
- name: Check platform, version stamp, and runtime tools
run: |
[ "$(docker image inspect --format '{{.Os}}/{{.Architecture}}' "$OPENSTORAGE_IMAGE")" = "linux/$ARCH" ]
out=$(docker run --rm "$OPENSTORAGE_IMAGE" --version)
echo "$out"
grep -F "openstorage $TAG (commit $COMMIT, built $BUILD_DATE," <<<"$out"
docker run --rm --entrypoint /bin/sh "$OPENSTORAGE_IMAGE" -c 'command -v git && command -v gpg && command -v ssh-keygen'
- name: Check the words of the image's Git patch refusals
# internal/api reads Git's messages to pick a status, and the image's Git
# is Alpine's, so a reworded message must fail here. Tags cut before the
# script landed carry no cases and skip the check.
run: |
if [ ! -f scripts/check-apply-messages.sh ]; then
echo "::notice::$TAG has no scripts/check-apply-messages.sh, so its image's Git is not checked for patch messages"
exit 0
fi
docker run --rm --entrypoint /bin/sh \
-v "$PWD/scripts/check-apply-messages.sh:/check-apply-messages.sh:ro" \
-v "$PWD/internal/api/testdata/applymsg:/cases:ro" \
"$OPENSTORAGE_IMAGE" /check-apply-messages.sh /cases
- name: Smoke the Compose quickstart
run: bash scripts/smoke-compose-quickstart.sh
# Give the checked digests the version tag after a reviewer approves. Nothing is
# built here: the tag is an index of the digests that stage pushed and verify
# checked, so it holds those bytes.
publish:
needs: [resolve, verify]
# Dispatch input `publish` defaults to false. Requiring main stops a branch
# copy of this file from publishing.
if: inputs.publish && github.ref == 'refs/heads/main'
# Required reviewers approve the push, and deployments only from main.
environment: ghcr-publish
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
contents: read
packages: write
outputs:
digest: ${{ steps.promote.outputs.digest }}
env:
TAG: ${{ needs.resolve.outputs.tag }}
VERSION: ${{ needs.resolve.outputs.version }}
COMMIT: ${{ needs.resolve.outputs.commit }}
DATE: ${{ needs.resolve.outputs.date }}
steps:
- uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: digest-*
path: digest
merge-multiple: true
- name: Refuse to replace an existing version tag
env:
ACTOR: ${{ github.actor }}
GH_TOKEN: ${{ github.token }}
run: |
# Credentials go through stdin, never argv.
token=$(printf 'user = "%s:%s"\n' "$ACTOR" "$GH_TOKEN" | curl -fsS -K - \
"https://ghcr.io/token?service=ghcr.io&scope=repository:${IMAGE#ghcr.io/}:pull" | jq -r .token)
echo "::add-mask::$token"
code=$(curl -sS -o /dev/null -w '%{http_code}' -I -H "Authorization: Bearer $token" \
-H 'Accept: application/vnd.oci.image.index.v1+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.docker.distribution.manifest.v2+json' \
"https://${IMAGE%%/*}/v2/${IMAGE#ghcr.io/}/manifests/$VERSION")
# 404 is the only proof of absence. 200 means published already; any
# other status is unknown, so stop rather than risk an overwrite.
[ "$code" = 404 ] || { echo "::error::$IMAGE:$VERSION exists or cannot be checked (HTTP $code). Version tags are immutable."; exit 1; }
- name: Give the checked digests the version tag
id: promote
run: |
amd64=$(cat digest/amd64)
arm64=$(cat digest/arm64)
[[ $amd64 =~ ^sha256:[a-f0-9]{64}$ && $arm64 =~ ^sha256:[a-f0-9]{64}$ && $amd64 != "$arm64" ]]
docker buildx version
create=(--tag "$IMAGE:$VERSION"
--annotation "index:org.opencontainers.image.source=https://github.com/$GITHUB_REPOSITORY"
--annotation "index:org.opencontainers.image.revision=$COMMIT"
--annotation "index:org.opencontainers.image.version=$VERSION"
--annotation "index:org.opencontainers.image.description=Self-hosted, API-first Git storage"
"$IMAGE@$amd64" "$IMAGE@$arm64")
# Look at the index before it exists. A version tag is never replaced, so the
# tag must not be created unless it will hold exactly the manifests of the two
# staged indexes (the images and their attestations).
staged=$(for d in "$amd64" "$arm64"; do
docker buildx imagetools inspect "$IMAGE@$d" --format '{{json .Manifest}}' | jq -r '.manifests[].digest'
done | sort)
planned=$(docker buildx imagetools create --dry-run "${create[@]}" | jq -r '.manifests[].digest' | sort)
[[ -n $staged && $staged = "$planned" ]]
docker buildx imagetools create "${create[@]}"
# Parse JSON: some Buildx releases ignore a field template like
# {{.Manifest.Digest}} and print the default table (docker/buildx#4006).
digest=$(docker buildx imagetools inspect "$IMAGE:$VERSION" --format '{{json .Manifest}}' | jq -r .digest)
[[ $digest =~ ^sha256:[a-f0-9]{64}$ ]]
echo "digest=$digest" >> "$GITHUB_OUTPUT"
- name: Check that the index holds exactly the checked digests
env:
DIGEST: ${{ steps.promote.outputs.digest }}
run: |
platforms=$(docker buildx imagetools inspect "$IMAGE@$DIGEST" --format '{{json .Manifest}}' |
jq -r '[.manifests[].platform | select(.os != "unknown") | .os + "/" + .architecture] | sort | join(",")')
[ "$platforms" = linux/amd64,linux/arm64 ]
# Every manifest of the two staged indexes (image and attestations) must be
# in the new index, byte for byte, and the index must hold nothing else.
staged=$(for a in amd64 arm64; do
docker buildx imagetools inspect "$IMAGE@$(cat "digest/$a")" --format '{{json .Manifest}}' | jq -r '.manifests[].digest'
done | sort)
published=$(docker buildx imagetools inspect "$IMAGE@$DIGEST" --format '{{json .Manifest}}' | jq -r '.manifests[].digest' | sort)
[[ -n $staged && $staged = "$published" ]]
{
echo "### Published $IMAGE:$VERSION"
echo
echo "- Index digest: \`$DIGEST\`"
echo "- Platforms: $platforms"
echo "- Staged digests: \`$(cat digest/amd64)\`, \`$(cat digest/arm64)\`"
echo "- Source: $TAG ($COMMIT)"
echo "- Pin: \`$IMAGE:$VERSION@$DIGEST\`"
} >> "$GITHUB_STEP_SUMMARY"
# The same checks again, natively, on the bytes now in the registry.
verify-published:
needs: [resolve, publish]
strategy:
fail-fast: false
matrix:
include:
- arch: amd64
runner: ubuntu-latest
- arch: arm64
runner: ubuntu-24.04-arm
name: verify-published (${{ matrix.arch }})
runs-on: ${{ matrix.runner }}
timeout-minutes: 20
permissions:
contents: read
packages: read
env:
ARCH: ${{ matrix.arch }}
TAG: ${{ needs.resolve.outputs.tag }}
COMMIT: ${{ needs.resolve.outputs.commit }}
BUILD_DATE: ${{ needs.resolve.outputs.date }}
DIGEST: ${{ needs.publish.outputs.digest }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ needs.resolve.outputs.commit }}
persist-credentials: false
- uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Pull the published digest
run: |
docker pull --platform "linux/$ARCH" "$IMAGE@$DIGEST"
[ "$(docker image inspect --format '{{index .Config.Labels "org.opencontainers.image.revision"}}' "$IMAGE@$DIGEST")" = "$COMMIT" ]
echo "OPENSTORAGE_IMAGE=$(docker image inspect --format '{{.Id}}' "$IMAGE@$DIGEST")" >> "$GITHUB_ENV"
- name: Check platform, version stamp, and runtime tools
run: |
[ "$(docker image inspect --format '{{.Os}}/{{.Architecture}}' "$OPENSTORAGE_IMAGE")" = "linux/$ARCH" ]
out=$(docker run --rm "$OPENSTORAGE_IMAGE" --version)
echo "$out"
grep -F "openstorage $TAG (commit $COMMIT, built $BUILD_DATE," <<<"$out"
docker run --rm --entrypoint /bin/sh "$OPENSTORAGE_IMAGE" -c 'command -v git && command -v gpg && command -v ssh-keygen'
- name: Check the words of the image's Git patch refusals
# internal/api reads Git's messages to pick a status, and the image's Git
# is Alpine's, so a reworded message must fail here. Tags cut before the
# script landed carry no cases and skip the check.
run: |
if [ ! -f scripts/check-apply-messages.sh ]; then
echo "::notice::$TAG has no scripts/check-apply-messages.sh, so its image's Git is not checked for patch messages"
exit 0
fi
docker run --rm --entrypoint /bin/sh \
-v "$PWD/scripts/check-apply-messages.sh:/check-apply-messages.sh:ro" \
-v "$PWD/internal/api/testdata/applymsg:/cases:ro" \
"$OPENSTORAGE_IMAGE" /check-apply-messages.sh /cases
- name: Smoke the Compose quickstart
run: bash scripts/smoke-compose-quickstart.sh