diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 71ba1ebc..3fe08b94 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -23,7 +23,8 @@ # X.Y.Z and X.Y; it carries the gateway release line as a # label, and the BFF inside has it compiled in # -# The third is the Helm chart, packaged at the same version. +# The third is the Helm chart, packaged at the same version. Last, the notes' +# Artifacts section gets the image's digest (record-artifacts). # # docs/releasing.md walks through the whole pipeline. name: Release @@ -238,3 +239,30 @@ jobs: helm package deploy/helm/console --version "$VERSION" \ --app-version "$VERSION" --destination "$RUNNER_TEMP" helm push "$RUNNER_TEMP/console-${VERSION}.tgz" "$registry" + + # Names the released image by digest in the release's notes. cut-release.mjs + # wrote them before the image was tagged, so their Artifacts section names it + # by tag only. The digest is read from quay.io, where the image is public, so + # this job needs no robot and is not in the `release` environment; it holds + # only the permission to edit the release. + record-artifacts: + needs: [publish, tag-image, publish-chart] + if: needs.publish.outputs.version != '' + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 + with: + ref: ${{ needs.publish.outputs.sha }} + persist-credentials: false + - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3 + - name: Name the image by digest in the release notes + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + VERSION: ${{ needs.publish.outputs.version }} + run: | + set -euo pipefail + digest="$(docker buildx imagetools inspect "quay.io/ai-openshell/console:${VERSION}" --format '{{json .Manifest}}' | jq -r '.digest')" + node scripts/release/record-artifacts.mjs --tag "v${VERSION}" --digest "$digest" diff --git a/docs/releasing.md b/docs/releasing.md index bb5c38dd..1f937b9b 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -136,6 +136,7 @@ publish.yml started by a person, on main or release/X.Y │ └── a vX.Y.Z tag is on this commit ──► tag-image ──► image :X.Y.Z, :X.Y (same digest) └──► publish-chart ──► chart X.Y.Z + └──► record-artifacts ──► notes name the image digest ``` - **The image is built once.** `scripts/retag-image.sh` adds `latest`, `X.Y.Z` and `X.Y` later, by pointing the tag at the digest of `sha-<7>`, so a released image is byte for byte the one CI built for the commit that passed. @@ -145,10 +146,11 @@ publish.yml started by a person, on main or release/X.Y ## The release notes -[`scripts/release/release-notes.mjs`](../scripts/release/release-notes.mjs) writes two parts; a dry run prints them as a real run would publish them: +[`scripts/release/release-notes.mjs`](../scripts/release/release-notes.mjs) writes three parts; a dry run prints them as a real run would publish them: - **The changes**: the title of every commit since the previous release, as merged, oldest first. A commit whose type or scope is `ci` is left out. The previous release is the nearest one in the released commit's history, so the first release of a new line lists the changes since the last release of the line before. With no earlier release, a fixed sentence says so. - **Supported OpenShell gateways**, described below. +- **Artifacts**: the image `quay.io/ai-openshell/console:X.Y.Z` and the chart `oci://quay.io/ai-openshell/charts/console` at version `X.Y.Z`, with a `helm install` line. The notes are written before the image is tagged, so the last job, `record-artifacts`, adds the image's digest to them afterwards ([`record-artifacts.mjs`](../scripts/release/record-artifacts.mjs)). It replaces only that section, and running it again changes nothing. To add the section to a release cut before it existed, run it by hand: `node scripts/release/record-artifacts.mjs --tag vX.Y.Z --digest "$(docker buildx imagetools inspect quay.io/ai-openshell/console:X.Y.Z --format '{{json .Manifest}}' | jq -r .digest)"` (`--dry-run` prints the notes and edits nothing). ## What every release declares diff --git a/scripts/release/publish-workflow.test.mjs b/scripts/release/publish-workflow.test.mjs index 91a467ab..5a1228fb 100644 --- a/scripts/release/publish-workflow.test.mjs +++ b/scripts/release/publish-workflow.test.mjs @@ -214,13 +214,25 @@ test('a dry run publishes nothing, even for a commit that is already released', const released = steps.find((step) => step.startsWith('name: Was a release cut at this commit?\n')); assert.ok(released.includes("\n if: steps.plan.outputs.dry_run != 'true'\n"), released); // The jobs after it run only on what that step reports. - for (const job of ['tag-image', 'publish-chart']) { + for (const job of ['tag-image', 'publish-chart', 'record-artifacts']) { const body = workflow.slice(workflow.indexOf(`\n ${job}:\n`)); assert.ok(body.slice(0, 200).includes("if: needs.publish.outputs.version != ''"), job); } assert.ok(workflow.includes('version: ${{ steps.released.outputs.version }}')); }); +// The notes name the image by digest only after it exists: the job runs after +// both publishing jobs, holds no registry robot, and only edits the release. +test('the notes get the image digest after the image and the chart are published', () => { + const body = workflow.slice(workflow.indexOf('\n record-artifacts:\n')); + assert.ok(body.includes('needs: [publish, tag-image, publish-chart]'), body); + assert.ok(!body.includes('environment:'), 'record-artifacts needs no release environment'); + assert.ok(!body.includes('quay-login'), 'record-artifacts signs in to no registry'); + assert.match(body, /permissions:\n {6}contents: write\n/); + assert.ok(body.includes('docker buildx imagetools inspect "quay.io/ai-openshell/console:${VERSION}"'), body); + assert.ok(body.includes('node scripts/release/record-artifacts.mjs --tag "v${VERSION}" --digest "$digest"'), body); +}); + // --- handing it to cut-release.mjs ------------------------------------------- test('the release step runs cut-release.mjs for the branch, as a dry run only when told', { skip: !haveBash }, (t) => { diff --git a/scripts/release/record-artifacts.mjs b/scripts/release/record-artifacts.mjs new file mode 100644 index 00000000..60e5821c --- /dev/null +++ b/scripts/release/record-artifacts.mjs @@ -0,0 +1,52 @@ +// Name the released image by digest in the notes of its GitHub release. +// +// node scripts/release/record-artifacts.mjs --tag v0.1.6 --digest sha256:… +// +// cut-release.mjs writes the notes before the image is tagged, so their +// Artifacts section names the image by tag only. publish.yml runs this once +// tag-image and publish-chart are done, with the digest the release tag +// resolves to. It also backfills a release cut before the section existed: +// run it by hand with that release's tag and digest. +// +// Needs `gh` signed in with permission to edit the repository's releases +// (GH_TOKEN, GH_REPO in a workflow). +import { execFileSync } from "node:child_process"; +import { parseArgs } from "node:util"; + +import { withImageDigest } from "./release-notes.mjs"; + +const { values } = parseArgs({ + options: { + tag: { type: "string" }, + digest: { type: "string" }, + "dry-run": { type: "boolean", default: false }, + }, +}); + +if (!/^v\d+\.\d+\.\d+$/.test(values.tag ?? "")) { + console.error( + `usage: record-artifacts.mjs --tag vX.Y.Z --digest sha256:… [--dry-run] (got tag ${values.tag})`, + ); + process.exit(2); +} + +const gh = (...args) => execFileSync("gh", args, { encoding: "utf8" }); +const body = JSON.parse( + gh("release", "view", values.tag, "--json", "body"), +).body; +const notes = withImageDigest(body, { + version: values.tag.slice(1), + imageDigest: values.digest, +}); + +if (values["dry-run"]) { + process.stdout.write(notes); +} else if (notes === body) { + console.log(`${values.tag}: the notes already name the image by digest.`); +} else { + execFileSync("gh", ["release", "edit", values.tag, "--notes-file", "-"], { + input: notes, + stdio: ["pipe", "inherit", "inherit"], + }); + console.log(`${values.tag}: the notes now name ${values.digest}.`); +} diff --git a/scripts/release/release-notes.mjs b/scripts/release/release-notes.mjs index 7c73bcd9..faedda11 100644 --- a/scripts/release/release-notes.mjs +++ b/scripts/release/release-notes.mjs @@ -1,6 +1,6 @@ // What the notes of a release say. // -// Two parts, in this order: +// Three parts, in this order: // // the changes the title of every commit since the previous release, as it // was merged. With a squash merge that is the pull request's @@ -9,6 +9,11 @@ // the gateways a "Supported OpenShell gateways" section: the gateway // release line the release is for, the gateway release it is // built on and tested against, and the SDK. +// the artifacts an "Artifacts" section: the image and the chart the release +// publishes, with a helm install line. The notes are written +// before either exists, so they name the image by its tag; +// once the image is tagged, publish.yml's record-artifacts +// job adds its digest (record-artifacts.mjs, withImageDigest). // // cut-release.mjs reads the repository and calls releaseNotes(). The line // comes from scripts/gateway-range.mjs at release time, so the claim is the @@ -150,6 +155,58 @@ export function supportedGatewaysNotes({ declared, previous, previousTag, readme return lines.join('\n'); } +export const IMAGE = 'quay.io/ai-openshell/console'; +export const CHART = 'oci://quay.io/ai-openshell/charts/console'; +const ARTIFACTS_HEADING = '### Artifacts'; + +/** + * Where a release is published: its image and its chart. + * + * @param {object} input + * @param {string} input.version X.Y.Z, without the v + * @param {string | null} [input.imageDigest] sha256:…, once the image is tagged + */ +export function artifactsNotes({ version, imageDigest = null }) { + const image = imageDigest ? `\`${IMAGE}:${version}@${imageDigest}\`` : `\`${IMAGE}:${version}\``; + return [ + ARTIFACTS_HEADING, + '', + `- Image: ${image}${imageDigest ? '' : ' (the digest is added here once the image is tagged)'}`, + `- Helm chart: \`${CHART}\`, version \`${version}\``, + '', + '```sh', + `helm install console ${CHART} --version ${version}`, + '```', + ].join('\n'); +} + +/** + * A release body with its Artifacts section naming the image by digest. + * + * The section the notes were written with is replaced; a body without one + * (a release cut before the section existed) gets it at the end. Everything + * else in the body is left as it is, so a hand edit to the notes survives. + */ +export function withImageDigest(body, { version, imageDigest }) { + if (!/^sha256:[0-9a-f]{64}$/.test(imageDigest ?? '')) { + throw new Error(`not an image digest: ${imageDigest}`); + } + const section = artifactsNotes({ version, imageDigest }); + const lines = (body ?? '').replace(/\s+$/, '').split('\n'); + const start = lines.findIndex((line) => line.trim() === ARTIFACTS_HEADING); + if (start === -1) { + const text = lines.join('\n'); + return `${text ? `${text}\n\n` : ''}${section}\n`; + } + let end = lines.findIndex((line, i) => i > start && /^#{1,3} /.test(line)); + if (end === -1) { + end = lines.length; + } + const before = lines.slice(0, start).join('\n').replace(/\s+$/, ''); + const after = lines.slice(end).join('\n'); + return `${before ? `${before}\n\n` : ''}${section}\n${after ? `\n${after}\n` : ''}`; +} + /** * The whole body of a release. * @@ -174,5 +231,7 @@ export function releaseNotes({ tag, previousTag, commits, declared, previous, re readmeUrl: repoUrl ? `${repoUrl}/blob/${tag}/README.md#compatibility` : null, }), '', + artifactsNotes({ version: tag.replace(/^v/, '') }), + '', ].join('\n'); } diff --git a/scripts/release/release.test.mjs b/scripts/release/release.test.mjs index 036bc44b..d58098e5 100644 --- a/scripts/release/release.test.mjs +++ b/scripts/release/release.test.mjs @@ -16,7 +16,17 @@ import { delimiter, dirname, join } from 'node:path'; import { test } from 'node:test'; import { fileURLToPath } from 'node:url'; -import { changesNotes, isCiCommit, lineDeclaredBy, releaseNotes, supportedGatewaysNotes } from './release-notes.mjs'; +import { + CHART, + IMAGE as RELEASE_IMAGE, + artifactsNotes, + changesNotes, + isCiCommit, + lineDeclaredBy, + releaseNotes, + supportedGatewaysNotes, + withImageDigest, +} from './release-notes.mjs'; import { releaseAt } from './released-version.mjs'; const repoRoot = join(dirname(fileURLToPath(import.meta.url)), '..', '..'); @@ -167,6 +177,50 @@ test('the notes of a release are its changes, then the gateways it supports', () assert.ok(first.includes('see Compatibility in the README'), first); }); +test('the notes end with where the release is published: the image by tag and the chart', () => { + const notes = releaseNotes({ tag: 'v0.1.6', previousTag: 'v0.1.5', commits, declared, previous, repoUrl: REPO_URL }); + const gateways = notes.indexOf('### Supported OpenShell gateways'); + const artifacts = notes.indexOf('### Artifacts'); + assert.ok(artifacts > gateways, notes); + assert.ok(notes.includes('- Image: `quay.io/ai-openshell/console:0.1.6` (the digest is added'), notes); + assert.ok(notes.includes('- Helm chart: `oci://quay.io/ai-openshell/charts/console`, version `0.1.6`'), notes); + assert.ok(notes.includes('helm install console oci://quay.io/ai-openshell/charts/console --version 0.1.6'), notes); + assert.equal(RELEASE_IMAGE, 'quay.io/ai-openshell/console'); + assert.equal(CHART, 'oci://quay.io/ai-openshell/charts/console'); +}); + +const IMAGE_DIGEST = `sha256:${'ab'.repeat(32)}`; + +test('once the image is tagged, the Artifacts section names it by digest and nothing else changes', () => { + const notes = releaseNotes({ tag: 'v0.1.6', previousTag: 'v0.1.5', commits, declared, previous, repoUrl: REPO_URL }); + const recorded = withImageDigest(notes, { version: '0.1.6', imageDigest: IMAGE_DIGEST }); + assert.ok(recorded.includes(`- Image: \`quay.io/ai-openshell/console:0.1.6@${IMAGE_DIGEST}\``), recorded); + assert.ok(!recorded.includes('the digest is added'), recorded); + assert.equal(recorded.slice(0, notes.indexOf('### Artifacts')), notes.slice(0, notes.indexOf('### Artifacts'))); + // Running it again, as a re-run of the job would, changes nothing. + assert.equal(withImageDigest(recorded, { version: '0.1.6', imageDigest: IMAGE_DIGEST }), recorded); +}); + +test('a release cut before the Artifacts section existed gets it at the end', () => { + const old = '### Changes since v0.1.3\n\n- fix: y\n\n### Supported OpenShell gateways\n\n**0.1.x**\n'; + const recorded = withImageDigest(old, { version: '0.1.4', imageDigest: IMAGE_DIGEST }); + assert.ok(recorded.startsWith(old.trimEnd() + '\n\n### Artifacts\n'), recorded); + assert.ok(recorded.endsWith('```\n'), recorded); +}); + +test('a section after Artifacts, added by hand, is kept', () => { + const body = `${artifactsNotes({ version: '0.1.6' })}\n\n### Known issues\n\n- none\n`; + const recorded = withImageDigest(body, { version: '0.1.6', imageDigest: IMAGE_DIGEST }); + assert.ok(recorded.endsWith('### Known issues\n\n- none\n'), recorded); + assert.ok(recorded.includes(`@${IMAGE_DIGEST}`), recorded); +}); + +test('anything but a sha256 digest is refused, so a failed lookup cannot write "null" into the notes', () => { + for (const digest of [null, 'null', '', 'sha256:short', `md5:${'ab'.repeat(32)}`]) { + assert.throws(() => withImageDigest('x', { version: '0.1.6', imageDigest: digest }), /not an image digest/, String(digest)); + } +}); + test('the first release of a new line says the line changed, next to the changes since the old one', () => { const notes = releaseNotes({ tag: 'v0.2.0',