diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index f065f4ff..a744d801 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -6,8 +6,12 @@ name: release # push to main → :latest + : (freshly-built stable APK bundled) # push tag vYYYY.MM.DD.HHMM → :latest (fresh APK bundled, and attached to the Release) # workflow_dispatch → manual trigger (same rules based on the ref) +# workflow_dispatch, ghcr_release → ghcr.io/fabled-sword/minstrel: + :latest, +# built from the public export (ghcr-release), +# and nothing on the private registry # -# That is the whole tag map. +# That is the whole tag map. The rest of this header is about the private +# registry; the public release image is described at ghcr-release. # # : on main is the ROLLBACK UNIT — every production commit addressable # without a release ceremony. It is minted only on main, where rollback is @@ -111,12 +115,21 @@ on: description: Fail the go lane on purpose, to check that nothing publishes on red type: boolean default: false + ghcr_release: + description: Build the public release image from the exported tree for GHCR (ghcr-release), instead of the private channel image + type: boolean + default: false + ghcr_dry_run: + description: With ghcr_release, build and check but push nothing. Untick only for the real release, run on main + type: boolean + default: true # A rapid re-push to main supersedes the in-flight build: the later commit # wins. Tags are immutable and unique, so no tag build can be superseded by -# another run on the same ref. +# another run on the same ref. The event name is in the group so a dispatch +# and a push on the same ref never cancel each other. concurrency: - group: ${{ github.workflow }}-${{ github.ref }} + group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }} cancel-in-progress: true jobs: @@ -682,7 +695,8 @@ jobs: && needs.web.result == 'success' && needs.android.result == 'success' && needs.govulncheck.result == 'success' - && needs.android-release.result == 'success' }} + && needs.android-release.result == 'success' + && github.event.inputs.ghcr_release != 'true' }} runs-on: go-ci container: image: git.fabledsword.com/bvandeusen/ci-go:1.26 @@ -830,6 +844,183 @@ jobs: --build-arg MINSTREL_CHANNEL="${{ steps.tags.outputs.channel }}" \ --push ${{ steps.tags.outputs.args }} . + # The public release image, process #7881's release step 4. + # + # Runs only on a dispatch with ghcr_release ticked, never on a push. It + # builds from `git archive HEAD`, the same tree the public repo's release + # commit holds, so nothing export-ignored (.gitea/, ci/) reaches the + # image's build context, and the image's labels name the public repo and + # the public commit rather than this workbench. + # + # Tags: : (rule 149's name, v is the public git tag) and + # :latest. : is pushed once and never re-pushed; a re-run only + # moves :latest. The private registry gets nothing from this job. + # + # ghcr_dry_run (the default) builds and checks everything and pushes + # nothing, so the job can be proved on dev before a release exists. A real + # run must be on main and needs the public tag to exist already: the + # exported tree is compared with the tag's tree and refused on a mismatch. + ghcr-release: + name: Publish the release image to GHCR (dispatch only) + needs: [go, integration, web, android, govulncheck, android-release] + if: >- + ${{ + !cancelled() + && github.event_name == 'workflow_dispatch' + && github.event.inputs.ghcr_release == 'true' + && needs.go.result == 'success' + && needs.integration.result == 'success' + && needs.web.result == 'success' + && needs.android.result == 'success' + && needs.govulncheck.result == 'success' + && needs.android-release.result == 'success' }} + runs-on: go-ci + container: + image: git.fabledsword.com/bvandeusen/ci-go:1.26 + + env: + PUBLIC_IMAGE: ghcr.io/fabled-sword/minstrel + PUBLIC_REPO: https://github.com/Fabled-Sword/minstrel + # Anything but an explicit false is a dry run. + DRY_RUN: ${{ github.event.inputs.ghcr_dry_run != 'false' }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + # Full history: ci/version.sh derives the name from commit times. + fetch-depth: 0 + + - name: Name the release + id: name + shell: bash + run: | + set -euo pipefail + VERSION="$(ci/version.sh HEAD | sed -n 's/^name=//p')" + if [ -z "${VERSION}" ]; then + echo "::error::could not derive a release name from ci/version.sh" + exit 1 + fi + if [ "${DRY_RUN}" != "true" ] && [ "${GITHUB_REF}" != "refs/heads/main" ]; then + echo "::error::a real GHCR release runs on main; this is ${GITHUB_REF}. Dispatch on main, or leave ghcr_dry_run ticked." + exit 1 + fi + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + echo "tag=v${VERSION}" >> "$GITHUB_OUTPUT" + echo "::notice::Release ${VERSION} (public tag v${VERSION}), dry run: ${DRY_RUN}" + + - name: Export the public tree and match it to the public tag + id: export + shell: bash + env: + TAG: ${{ steps.name.outputs.tag }} + run: | + set -euo pipefail + EXPORT="${RUNNER_TEMP}/export" + rm -rf "${EXPORT}" + mkdir -p "${EXPORT}" + git archive HEAD | tar -x -C "${EXPORT}" + for private in .gitea ci ci-requirements.md; do + if [ -e "${EXPORT}/${private}" ]; then + echo "::error::the export carries ${private}; .gitattributes export-ignore no longer covers it" + exit 1 + fi + done + + # The tree hash of exactly what was exported. -f because some + # tracked files are also ignored (android/.idea, the generated token + # CSS): the public commit has to be staged with -f too, or it drops + # them and this match refuses the release. + cd "${EXPORT}" + git init -q + git add -A -f + TREE="$(git write-tree)" + echo "::notice::export tree ${TREE}" + + if git fetch -q --depth 1 "${PUBLIC_REPO}" "refs/tags/${TAG}:refs/tags/${TAG}" 2>/dev/null; then + PUBLIC_TREE="$(git rev-parse "${TAG}^{tree}")" + REVISION="$(git rev-parse "${TAG}^{commit}")" + if [ "${PUBLIC_TREE}" != "${TREE}" ]; then + echo "::error::${PUBLIC_REPO} tag ${TAG} holds tree ${PUBLIC_TREE}, but this commit exports ${TREE}. The public release commit is not this export." + exit 1 + fi + echo "::notice::public ${TAG} is commit ${REVISION}, and its tree matches the export" + elif [ "${DRY_RUN}" = "true" ]; then + REVISION=unreleased + echo "::notice::${TAG} is not on ${PUBLIC_REPO} yet; a dry run labels the image revision=unreleased" + else + echo "::error::${TAG} is not on ${PUBLIC_REPO}. Push the public release commit and its tag first (#8209), then run this." + exit 1 + fi + rm -rf .git + echo "dir=${EXPORT}" >> "$GITHUB_OUTPUT" + echo "revision=${REVISION}" >> "$GITHUB_OUTPUT" + + - name: Download signed APK artifact + uses: actions/download-artifact@v8 + with: + name: minstrel-apk + path: client/ + + - name: Stage the APK and its version sidecar into the export + shell: bash + env: + APK_VERSION_NAME: ${{ needs.android-release.outputs.version_name }} + APK_VERSION_CODE: ${{ needs.android-release.outputs.version_code }} + APK_CHANNEL: ${{ needs.android-release.outputs.channel }} + EXPORT: ${{ steps.export.outputs.dir }} + run: | + set -euxo pipefail + mv client/app-release.apk "${EXPORT}/client/minstrel.apk" + printf '{"name":"%s","code":%s,"channel":"%s"}\n' \ + "${APK_VERSION_NAME}" "${APK_VERSION_CODE}" "${APK_CHANNEL}" \ + > "${EXPORT}/client/minstrel.apk.version" + cat "${EXPORT}/client/minstrel.apk.version" + + - name: Build the release image from the export + shell: bash + env: + VERSION: ${{ steps.name.outputs.version }} + REVISION: ${{ steps.export.outputs.revision }} + CHANNEL: ${{ needs.android-release.outputs.channel }} + EXPORT: ${{ steps.export.outputs.dir }} + # --provenance=false and no SBOM: an attestation would carry the + # workbench's build details into a public registry. + run: | + set -euo pipefail + docker build --pull --provenance=false \ + --build-arg MINSTREL_VERSION="${VERSION}" \ + --build-arg MINSTREL_CHANNEL="${CHANNEL}" \ + --label org.opencontainers.image.source="${PUBLIC_REPO}" \ + --label org.opencontainers.image.revision="${REVISION}" \ + --label org.opencontainers.image.version="${VERSION}" \ + -t "${PUBLIC_IMAGE}:${VERSION}" -t "${PUBLIC_IMAGE}:latest" \ + "${EXPORT}" + docker image inspect --format '{{json .Config.Labels}}' "${PUBLIC_IMAGE}:${VERSION}" + + - name: Push to GHCR + shell: bash + env: + VERSION: ${{ steps.name.outputs.version }} + GHCR_USER: ${{ vars.GHCR_USER }} + GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }} + run: | + set -euo pipefail + if [ "${DRY_RUN}" = "true" ]; then + echo "::notice::dry run: ${PUBLIC_IMAGE}:${VERSION} and :latest built, nothing pushed" + exit 0 + fi + if [ -z "${GHCR_USER}" ]; then echo "::error::the Actions variable GHCR_USER is empty"; exit 1; fi + if [ -z "${GHCR_TOKEN}" ]; then echo "::error::the Actions secret GHCR_TOKEN is empty"; exit 1; fi + printf '%s' "${GHCR_TOKEN}" | docker login ghcr.io -u "${GHCR_USER}" --password-stdin + if docker manifest inspect "${PUBLIC_IMAGE}:${VERSION}" > /dev/null 2>&1; then + echo "::notice::${PUBLIC_IMAGE}:${VERSION} is already published and is never re-pushed; moving :latest only" + else + docker push "${PUBLIC_IMAGE}:${VERSION}" + fi + docker push "${PUBLIC_IMAGE}:latest" + docker image inspect --format '{{json .RepoDigests}}' "${PUBLIC_IMAGE}:${VERSION}" + # Verifies a tag release actually ended up complete, and names the specific # thing that's missing if not. # diff --git a/internal/server/release_gate_test.go b/internal/server/release_gate_test.go index c62b1a17..5c60a641 100644 --- a/internal/server/release_gate_test.go +++ b/internal/server/release_gate_test.go @@ -24,9 +24,10 @@ import ( // require its success by name. var gateLanes = []string{"go", "integration", "web", "android", "govulncheck"} -// gatePublishers push something other people can pull: image tags, and the -// APK + sidecar attached to a Release. -var gatePublishers = []string{"image-release", "release-assets"} +// gatePublishers push something other people can pull: image tags (the +// private channels, and the public release image on GHCR), and the APK + +// sidecar attached to a Release. +var gatePublishers = []string{"image-release", "release-assets", "ghcr-release"} // gateOthers are the remaining jobs and why they sit outside the gate: // android-release only builds a workflow artifact (nothing outside the run can @@ -118,3 +119,48 @@ func TestReleaseGate_PublishersNeedEveryLaneToSucceed(t *testing.T) { } } } + +// The public release image has four properties a later edit could quietly +// lose, each of which still builds and pushes a valid image: +// - it runs only on a dispatch that asks for it, never on a push to dev; +// - a GHCR dispatch moves no private channel; +// - it builds from the `git archive` export, so export-ignored files never +// reach its build context; +// - it carries no provenance attestation, and its source label names the +// public repo rather than this workbench. +func TestGHCRRelease_OnlyOnRequestFromTheExportAndNamingThePublicRepo(t *testing.T) { + jobs := releaseJobs(t) + ghcr := strings.Join(strings.Fields(jobs["ghcr-release"].If), " ") + for _, want := range []string{"github.event_name == 'workflow_dispatch'", "github.event.inputs.ghcr_release == 'true'"} { + if !strings.Contains(ghcr, want) { + t.Errorf("ghcr-release's if lacks %s, so it could run on a push\n if: %s", want, ghcr) + } + } + private := strings.Join(strings.Fields(jobs["image-release"].If), " ") + if !strings.Contains(private, "github.event.inputs.ghcr_release != 'true'") { + t.Errorf("image-release does not stand aside on a GHCR dispatch, so a release would also move a private channel\n if: %s", private) + } + + yaml := releaseYAML(t) + i := strings.Index(yaml, "\n ghcr-release:") + if i < 0 { + t.Fatal("no ghcr-release job") + } + job := yaml[i:] + if j := jobBoundary.FindStringIndex(job[1:]); j != nil { + job = job[:j[0]+1] + } + export := stepBody(t, job, "Export the public tree and match it to the public tag") + if !strings.Contains(export, "git archive HEAD") { + t.Errorf("the export step does not use git archive, so export-ignored files could reach the image\n%s", export) + } + build := stepBody(t, job, "Build the release image from the export") + for _, want := range []string{"--provenance=false", `"${EXPORT}"`, "org.opencontainers.image.source=\"${PUBLIC_REPO}\""} { + if !strings.Contains(build, want) { + t.Errorf("the release image build lacks %s\n%s", want, build) + } + } + if !strings.Contains(job, "PUBLIC_REPO: https://github.com/Fabled-Sword/minstrel") { + t.Error("ghcr-release's PUBLIC_REPO is not the public GitHub repo, so the image's source label points elsewhere") + } +}