Repository navigation
441 lines (414 loc) · 19.8 KB
/
Copy pathrelease.yml
File metadata and controls
441 lines (414 loc) · 19.8 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Release: builds and pushes the host-shell container image to GHCR.
#
# Mirrors the build:image job in .gitlab-ci.yml (docker build of this repo's
# Dockerfile, push to a registry), with GHCR in place of the in-cluster GitLab
# registry.
#
# hugmesh-release-channels: beta+stable
# The release channel comes from the TAG. scripts/release-version.sh checks the
# version file and the ref shape; scripts/release-channel.sh decides every case
# below (both are tested by the version job before anything is decided):
#
# push to main -> :main and :sha-<12>. No GitHub Release.
# push to release/X.Y -> :sha-<12> only (the same main-style run the
# release script checks before it tags). The
# version must be X.Y.*. No GitHub Release.
# push of tag vX.Y.Z-beta.N -> :X.Y.Z-beta.N, :beta and :sha-<12>, plus a
# GitHub prerelease (never Latest). The commit
# must be on main.
# push of tag vX.Y.Z (stable) -> :X.Y.Z, :X.Y, :latest and :sha-<12>, plus a
# GitHub release marked Latest. The commit must
# be on release/X.Y. A patch on an older line (a
# newer stable vA.B.C is published) moves :X.Y
# but not :latest, and its GitHub release is not
# marked Latest.
# workflow_dispatch on main, a release/X.Y branch or a v* tag
# -> same as the push; for a tag, an image or
# GitHub release that already exists is left
# alone (the image steps are skipped and only a
# missing GitHub Release is created)
# anything else -> refused by the version job (this includes
# release/<anything that is not X.Y> and any
# pre-release other than -beta.N)
#
# The version source is package.json `.version` (package-lock.json must match).
# A tag must equal "v" + that version. :X.Y.Z is never overwritten; the floating
# tags :beta, :X.Y and :latest move. Every tag a run pushes points at one
# digest, and that digest is signed and attested.
#
# The version job only refuses early. "Re-run failed jobs" reuses its outputs,
# however old they are, and a newer stable can publish while a run is in
# flight, so whether :X.Y and :latest move is decided again by the same script
# right before the push and right before gh release create. No later job reads
# the version job's answer, so a re-run cannot move a floating tag to an older
# line. A window of seconds remains between that check and the registry write
# (a registry has no compare-and-swap); if a concurrent run wins it, the digest
# check below fails that run.
#
# This workflow has no pull_request trigger and must never gain one. The runs-on
# line sends a job to vars.RUNS_ON_PUBLISH (the self-hosted runner, a JSON array
# such as ["self-hosted","Linux","X64"]) only for a non-PR run on main or a v* tag;
# every other run (release/X.Y branches included), and every run when
# RUNS_ON_PUBLISH is unset, goes to hosted ubuntu-latest. Deleting
# RUNS_ON_PUBLISH in this repo is the off switch. The expression is not a
# security boundary against fork PRs (a fork's PR runs the fork's copy of the
# workflow); the runner group settings are.
name: Release
on:
push:
branches: [main, 'release/**']
tags: ['v*']
workflow_dispatch: {}
permissions:
contents: read
concurrency:
group: release-${{ github.ref }}
cancel-in-progress: false
env:
REGISTRY: ghcr.io
IMAGE_NAME: ghcr.io/hugmesh/hugmesh-host-shell
jobs:
version:
runs-on: ${{ fromJSON(github.event_name != 'pull_request' && github.event_name != 'pull_request_target' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref, 'refs/tags/studio-v')) && vars.RUNS_ON_PUBLISH || '"ubuntu-latest"') }}
outputs:
version: ${{ steps.guard.outputs.version }}
kind: ${{ steps.v.outputs.kind }}
channel: ${{ steps.v.outputs.channel }}
line: ${{ steps.v.outputs.line }}
sha12: ${{ steps.v.outputs.sha12 }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Self-test the release version guard
run: bash scripts/release-version.test.sh
- name: Self-test the release channel rules
run: bash scripts/test-release-channel.sh
- name: Version guard
id: guard
run: bash scripts/release-version.sh
# Channel, branch rules (a beta tag's commit on main, a stable tag's on
# release/X.Y) and image tags: scripts/release-channel.sh. This answer
# only refuses early; the image and github-release jobs decide again.
- name: Release channel
id: v
env:
VERSION: ${{ steps.guard.outputs.version }}
run: |
set -euo pipefail
out="$(VERSION_FILE=package.json bash scripts/release-channel.sh)"
printf '%s\n' "$out" >> "$GITHUB_OUTPUT"
echo "$out"
image:
needs: version
# Signed publish job: always a GitHub-hosted runner. Nothing built on a
# self-hosted runner (the operator's own host) may be published, so this
# job deliberately does not use RUNS_ON_PUBLISH.
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
attestations: write
env:
VERSION: ${{ needs.version.outputs.version }}
KIND: ${{ needs.version.outputs.kind }}
SHA12: ${{ needs.version.outputs.sha12 }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Only the immutable :X.Y.Z (or :X.Y.Z-beta.N) is checked here. The
# floating tags :beta, :X.Y and :latest are meant to move to a newer
# release, so they are not.
- name: Refuse to overwrite a published version
id: exists
run: |
set -uo pipefail
if [ "$KIND" != release ]; then
echo "skip=false" >> "$GITHUB_OUTPUT"
exit 0
fi
ref="$IMAGE_NAME:$VERSION"
if out="$(docker buildx imagetools inspect "$ref" 2>&1)"; then
if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then
echo "::notice::$ref already exists; this re-run skips the image and only completes the release"
echo "skip=true" >> "$GITHUB_OUTPUT"
exit 0
fi
case "$VERSION" in
*-*) next="raise the beta number" ;;
*) next="cut the next patch release from release/${VERSION%.*}" ;;
esac
echo "::error::$ref already exists; a published version is never overwritten: $next instead."
exit 1
fi
case "$out" in
*": not found"*)
echo "skip=false" >> "$GITHUB_OUTPUT"
;;
*)
# Fail closed: an auth or network error must not look like "absent".
# A package that has never been pushed also lands here; publish
# main first (it creates the package), then re-run this tag.
echo "::error::could not tell whether $ref exists: $out"
exit 1
;;
esac
# Builds now and pushes nothing, so the step that pushes finds every layer
# in the builder's cache and takes seconds. The tag check below would
# otherwise be as old as the whole build by the time the tags are written.
- name: Build the image (no push)
if: steps.exists.outputs.skip != 'true'
uses: docker/build-push-action@v6
with:
context: .
push: false
- name: Compute tags and labels
id: meta
if: steps.exists.outputs.skip != 'true'
run: |
set -euo pipefail
# scripts/release-channel.sh decides the tag list here, right before
# the push, not in the version job: beta -> :X.Y.Z-beta.N + :beta;
# stable -> :X.Y.Z plus :X.Y and :latest when nothing newer is
# published on its line / on any line; every run adds :sha-<12>.
out="$(VERSION_FILE=package.json bash scripts/release-channel.sh)"
echo "$out"
tags="$(printf '%s\n' "$out" | sed -n '/^image_tags<<EOF_IMAGE_TAGS$/,/^EOF_IMAGE_TAGS$/p' | sed '1d;$d')"
move_latest="$(printf '%s\n' "$out" | sed -n 's/^move_latest=//p')"
if [ -z "$tags" ]; then
echo "::error::scripts/release-channel.sh computed no image tags"
exit 1
fi
while IFS= read -r t; do
case "$t" in
"$IMAGE_NAME":?*) ;;
*) echo "::error::unexpected image tag '$t'"; exit 1 ;;
esac
done <<< "$tags"
if [ "$KIND" = release ]; then
label_version="$VERSION"
else
label_version="$GITHUB_REF_NAME"
fi
{
echo "move_latest=$move_latest"
echo "tags<<EOF_TAGS"
echo "$tags"
echo "EOF_TAGS"
echo "labels<<EOF_LABELS"
echo "org.opencontainers.image.source=https://github.com/$GITHUB_REPOSITORY"
echo "org.opencontainers.image.version=$label_version"
echo "org.opencontainers.image.revision=$GITHUB_SHA"
echo "EOF_LABELS"
} >> "$GITHUB_OUTPUT"
echo "Tags:"
echo "$tags"
- name: Build and push image
id: build
if: steps.exists.outputs.skip != 'true'
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# --- signed-images-20261021 ---
# Everything below signs what the step `build` just pushed, by DIGEST, from
# this GitHub-hosted job: a cosign keyless signature (Sigstore, GitHub OIDC),
# a SLSA build-provenance attestation and an SPDX SBOM attestation, both
# pushed next to the image. A run that skipped the build (existing release
# tag) skips all of it.
- name: Resolve the pushed image name and digest
id: signref
if: steps.build.outcome == 'success'
env:
METADATA: ${{ steps.build.outputs.metadata }}
DIGEST: ${{ steps.build.outputs.digest }}
EXPECTED_TAGS: ${{ steps.meta.outputs.tags }}
MOVE_LATEST: ${{ steps.meta.outputs.move_latest }}
run: |
set -euo pipefail
python3 - <<'PY'
import json, os, re, sys
meta = json.loads(os.environ.get("METADATA") or "{}")
digest = os.environ.get("DIGEST", "")
if not re.fullmatch(r"sha256:[0-9a-f]{64}", digest):
sys.exit(f"::error::build step gave no usable digest: {digest!r}")
names = [n.strip() for n in meta.get("image.name", "").split(",") if n.strip()]
repos, tags = set(), []
for n in names:
head, _, last = n.rpartition("/")
if ":" in last:
last, tag = last.split(":", 1)
tags.append(tag)
repos.add(f"{head}/{last}")
if len(repos) != 1:
sys.exit(f"::error::expected one pushed repository, got {sorted(repos)}")
expected = sorted(t.rpartition(":")[2] for t in os.environ.get("EXPECTED_TAGS", "").split() if t)
if sorted(tags) != expected:
sys.exit(f"::error::pushed tags {sorted(tags)} are not the computed tags {expected}")
# :latest only for a stable version that owns it (MOVE_LATEST from the
# check right before the push). The "-" fallback makes a missing or EMPTY VERSION fail
# closed (a job output that was never set arrives as "", not unset).
if "latest" in tags and ("-" in (os.environ.get("VERSION") or "-") or os.environ.get("MOVE_LATEST") != "true"):
sys.exit("::error::a :latest tag was pushed; beta images and older-line patches must never be tagged latest")
repo = repos.pop()
if repo != repo.lower() or not repo.startswith("ghcr.io/hugmesh/"):
sys.exit(f"::error::unexpected image repository {repo!r}")
with open(os.environ["GITHUB_OUTPUT"], "a") as out:
out.write(f"repo={repo}\ndigest={digest}\n")
print(f"signing {repo}@{digest} (tags: {', '.join(tags)})")
PY
- name: Install cosign
if: steps.signref.outcome == 'success'
uses: sigstore/cosign-installer@v3
- name: Sign the image digest (cosign keyless, GitHub OIDC)
if: steps.signref.outcome == 'success'
env:
REF: ${{ steps.signref.outputs.repo }}@${{ steps.signref.outputs.digest }}
run: cosign sign --yes "$REF"
- name: Attest build provenance (pushed to the registry)
if: steps.signref.outcome == 'success'
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ steps.signref.outputs.repo }}
subject-digest: ${{ steps.signref.outputs.digest }}
push-to-registry: true
- name: Generate an SPDX SBOM of the pushed digest
if: steps.signref.outcome == 'success'
uses: anchore/sbom-action@v0
with:
image: ${{ steps.signref.outputs.repo }}@${{ steps.signref.outputs.digest }}
format: spdx-json
output-file: ${{ runner.temp }}/sbom.spdx.json
upload-artifact: false
upload-release-assets: false
- name: Attest the SBOM (pushed to the registry)
if: steps.signref.outcome == 'success'
uses: actions/attest-sbom@v2
with:
subject-name: ${{ steps.signref.outputs.repo }}
subject-digest: ${{ steps.signref.outputs.digest }}
sbom-path: ${{ runner.temp }}/sbom.spdx.json
push-to-registry: true
# cosign and the attestations bind to the digest, so they cover every tag
# that resolves to it. Prove each pushed tag does (a floating tag moved
# by a concurrent run would not).
- name: Check every pushed tag resolves to the signed digest
if: steps.signref.outcome == 'success'
env:
TAGS: ${{ steps.meta.outputs.tags }}
DIGEST: ${{ steps.signref.outputs.digest }}
run: |
set -euo pipefail
while IFS= read -r ref; do
[ -n "$ref" ] || continue
# A digest is the sha256 of the manifest's exact bytes, which --raw
# prints. Do not use --format '{{.Manifest.Digest}}': buildx answers
# any format that starts with {{.Manifest with its full listing.
if ! raw_sha="$(docker buildx imagetools inspect "$ref" --raw | sha256sum)"; then
echo "::error::could not read $ref from the registry"
exit 1
fi
got="sha256:${raw_sha%% *}"
if [ "$got" != "$DIGEST" ]; then
echo "::error::$ref resolves to $got, not the signed digest $DIGEST"
exit 1
fi
echo "$ref -> $DIGEST (signed)"
done <<< "$TAGS"
- name: Write the verify commands to the job summary
if: steps.signref.outcome == 'success'
env:
REPO_IMAGE: ${{ steps.signref.outputs.repo }}
DIGEST: ${{ steps.signref.outputs.digest }}
run: |
{
echo "### Signed image"
echo '```'
echo "$REPO_IMAGE@$DIGEST"
echo "cosign verify $REPO_IMAGE@$DIGEST --certificate-oidc-issuer https://token.actions.githubusercontent.com --certificate-identity-regexp '^https://github.com/$GITHUB_REPOSITORY/.github/workflows/release.yml@refs/(heads/main|heads/release/[0-9]+[.][0-9]+|tags/v.*)\$'"
echo "gh attestation verify oci://$REPO_IMAGE@$DIGEST --repo $GITHUB_REPOSITORY"
echo "gh attestation verify oci://$REPO_IMAGE@$DIGEST --repo $GITHUB_REPOSITORY --predicate-type https://spdx.dev/Document/v2.3"
echo '```'
} >> "$GITHUB_STEP_SUMMARY"
github-release:
needs: [version, image]
if: needs.version.outputs.kind == 'release'
runs-on: ${{ fromJSON(github.event_name != 'pull_request' && github.event_name != 'pull_request_target' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref, 'refs/tags/studio-v')) && vars.RUNS_ON_PUBLISH || '"ubuntu-latest"') }}
permissions:
contents: write
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
VERSION: ${{ needs.version.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# beta -> prerelease, never Latest. stable -> a full release, marked
# Latest only when nothing newer is published on any line (gh would
# otherwise pick Latest by date and mislabel an older-line patch). That is
# decided here, right before the release is created: a newer stable may
# have published since the image job, or days ago if this is a re-run.
- name: Create the GitHub release
run: |
set -euo pipefail
if gh release view "$GITHUB_REF_NAME" >/dev/null 2>&1; then
echo "::notice::release $GITHUB_REF_NAME already exists; nothing to do"
exit 0
fi
args=(--verify-tag --title "$GITHUB_REF_NAME"
--notes "Container image: $IMAGE_NAME:$VERSION")
case "$VERSION" in
*-*) args+=(--prerelease --latest=false) ;;
*)
out="$(VERSION_FILE=package.json bash scripts/release-channel.sh)"
echo "$out"
move_latest="$(printf '%s\n' "$out" | sed -n 's/^move_latest=//p')"
if [ "$move_latest" = true ]; then
args+=(--latest)
else
args+=(--latest=false)
fi
;;
esac
gh release create "$GITHUB_REF_NAME" "${args[@]}"
# --- signed-images-20261021 ---
# After the GitHub Release of a v<version> tag run, ask hugmesh.github.io to
# rebuild its downloads data (contract: .wf-scripts results downloads/
# DISPATCH-CONTRACT.md). The payload is informational; the site re-reads
# GitHub itself. Needs the SITE_DISPATCH_TOKEN secret (a token allowed to
# write contents on hugmesh/hugmesh.github.io); without it this job prints a
# notice and succeeds. Never runs on a push to main.
notify-site:
needs: [github-release]
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
permissions: {}
steps:
- name: Notify hugmesh.github.io (repository_dispatch releases-updated)
env:
SITE_DISPATCH_TOKEN: ${{ secrets.SITE_DISPATCH_TOKEN }}
run: |
set -euo pipefail
if [ -z "${SITE_DISPATCH_TOKEN:-}" ]; then
echo "::notice::SITE_DISPATCH_TOKEN is not set; skipping the hugmesh.github.io releases-updated notification"
exit 0
fi
payload=$(python3 -c 'import json, os; print(json.dumps({"event_type": "releases-updated", "client_payload": {"source": os.environ["GITHUB_REPOSITORY"], "kind": "image", "version": os.environ["GITHUB_REF_NAME"][1:], "ref": os.environ["GITHUB_REF"], "sha": os.environ["GITHUB_SHA"]}}))')
curl -fsS -X POST \
-H "Authorization: Bearer $SITE_DISPATCH_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/hugmesh/hugmesh.github.io/dispatches \
-d "$payload"
echo "sent releases-updated for $GITHUB_REPOSITORY $GITHUB_REF_NAME"