From fb76883e29a74d651948516da4d0b648000bb116 Mon Sep 17 00:00:00 2001 From: waja Date: Mon, 16 Nov 2026 00:03:44 +0100 Subject: [PATCH 1/5] Run release validation and publication in one workflow --- .github/workflows/docker.image.yml | 914 +++++++++++++++++++++++++++++ 1 file changed, 914 insertions(+) diff --git a/.github/workflows/docker.image.yml b/.github/workflows/docker.image.yml index 3970d3d..9ec7f5b 100644 --- a/.github/workflows/docker.image.yml +++ b/.github/workflows/docker.image.yml @@ -33,6 +33,16 @@ on: required: false type: boolean default: false + create_release_tag: + description: "Create or move the release tag after all validation jobs pass" + required: false + type: boolean + default: false + expected_tag_ref_sha: + description: "Expected existing tag ref SHA for an atomic replacement" + required: false + type: string + default: "" push: branches: - $default-branch @@ -101,6 +111,907 @@ jobs: path: .workflow-tools fetch-depth: 0 + - name: Create or verify release tag after all validation jobs pass + if: ${{ github.event_name == 'workflow_dispatch' && inputs.create_release_tag }} + env: + GH_TOKEN: ${{ github.token }} + TAG_PUSH_TOKEN: ${{ github.token }} + RELEASE_TAG: ${{ inputs.image_version }} + SOURCE_REF_INPUT: ${{ inputs.source_ref }} + REPLACEMENT_INPUT: ${{ inputs.replacement }} + EXPECTED_TAG_REF_SHA: ${{ inputs.expected_tag_ref_sha }} + REPOSITORY: ${{ github.repository }} + run: | + set -euo pipefail + tag_pattern='^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}-[0-9]{1,3} env: + BUILD_VERSION_INPUT: ${{ inputs.image_version }} + SOURCE_REF_INPUT: ${{ inputs.source_ref }} + HEAD_REF_INPUT: ${{ github.head_ref }} + REF_NAME_INPUT: ${{ github.ref_name }} + GITHUB_EVENT_NAME: ${{ github.event_name }} + run: | + set -euo pipefail + + if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then + vcs_branch="$HEAD_REF_INPUT" + else + vcs_branch="$(bash .workflow-tools/.github/scripts/resolve-vcs-branch.sh)" + fi + + { + echo "build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" + echo "build_version=${BUILD_VERSION_INPUT:-$(git describe --tags --always)}" + echo "vcs_ref=$(git rev-parse --short HEAD)" + echo "vcs_url=$(git config --get remote.origin.url)" + echo "vcs_branch=$vcs_branch" + } >> "$GITHUB_OUTPUT" + + - name: Perform ShiftLeft Scan + uses: ShiftLeftSecurity/scan-action@v1.3.0 + env: + WORKSPACE: "" + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SCAN_AUTO_BUILD: true + with: + output: reports + # Scan auto-detects the languages in your project. To override uncomment the below variable and set the type + # type: credscan,java + # type: python + - name: Upload report + uses: github/codeql-action/upload-sarif@v4 + with: + sarif_file: reports + test-build: + needs: + - super-linter + - shiftleft + outputs: + tested_sha: ${{ steps.build_metadata.outputs.vcs_sha }} + build_version: ${{ steps.build_metadata.outputs.build_version }} + vcs_ref: ${{ steps.build_metadata.outputs.vcs_ref }} + vcs_url: ${{ steps.build_metadata.outputs.vcs_url }} + vcs_branch: ${{ steps.build_metadata.outputs.vcs_branch }} + strategy: + fail-fast: false + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + ref: ${{ inputs.source_ref || ((github.event_name == 'pull_request' || github.event_name == 'push') && github.sha) || github.ref }} + fetch-depth: 0 + - name: Checkout workflow tools + uses: actions/checkout@v7 + with: + ref: ${{ github.event.repository.default_branch }} + path: .workflow-tools + fetch-depth: 0 + + - name: Prepare build commit metadata + id: build_metadata + env: + BUILD_VERSION_INPUT: ${{ inputs.image_version }} + SOURCE_REF_INPUT: ${{ inputs.source_ref }} + HEAD_REF_INPUT: ${{ github.head_ref }} + REF_NAME_INPUT: ${{ github.ref_name }} + GITHUB_EVENT_NAME: ${{ github.event_name }} + run: | + set -euo pipefail + + if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then + vcs_branch="$HEAD_REF_INPUT" + else + vcs_branch="$(bash .workflow-tools/.github/scripts/resolve-vcs-branch.sh)" + fi + + { + echo "vcs_sha=$(git rev-parse HEAD)" + echo "vcs_ref=$(git rev-parse --short HEAD)" + echo "build_version=${BUILD_VERSION_INPUT:-$(git describe --tags --always)}" + echo "vcs_url=$(git config --get remote.origin.url)" + echo "vcs_branch=$vcs_branch" + } >> "$GITHUB_OUTPUT" + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v4 + with: + driver-opts: network=host + - name: Build single-platform test image + id: docker_build + uses: docker/build-push-action@v7 + with: + builder: ${{ steps.buildx.outputs.name }} + push: false + outputs: type=docker,dest=/tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar + tags: localhost:5000/foobar/${{ env.IMAGE_NAME }} + cache-from: type=gha,scope=speedtest + cache-to: type=gha,mode=max,scope=speedtest + context: . + file: ./Dockerfile + platforms: linux/amd64 + build-args: | + BUILD_DATE=${{ needs.shiftleft.outputs.build_date }} + BUILD_VERSION=${{ steps.build_metadata.outputs.build_version }} + VCS_REF=${{ steps.build_metadata.outputs.vcs_ref }} + VCS_URL=${{ steps.build_metadata.outputs.vcs_url }} + VCS_BRANCH=${{ steps.build_metadata.outputs.vcs_branch }} + - name: Upload single-platform test image + uses: actions/upload-artifact@v7 + with: + name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} + path: /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar + if-no-files-found: error + compression-level: 0 + test: + name: Run test + needs: + - test-build + strategy: + fail-fast: false + runs-on: ubuntu-latest + services: + registry: + image: registry:3 + ports: + - 5000:5000 + steps: + - name: Download single-platform test image + uses: actions/download-artifact@v8 + with: + name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} + path: /tmp + - name: Load image into local registry + run: | + docker load --input /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar + docker push localhost:5000/foobar/${{ env.IMAGE_NAME }} + - name: Inspect the Docker image + run: | + docker buildx imagetools inspect localhost:5000/foobar/${{ env.IMAGE_NAME }} + - name: Test the Docker image + run: | + CONTAINER_OUTPUT=$(docker run --rm localhost:5000/foobar/${{ env.IMAGE_NAME }} -h) + echo "Container output: $CONTAINER_OUTPUT" + echo "Container output (quoted): '$CONTAINER_OUTPUT'" + + TEST_STRING=$(echo "${CONTAINER_OUTPUT}" | head -1 | cut -d' ' -f2 | tr -d '\r') + echo "Extracted test string: '$TEST_STRING'" + + if ! [ "${TEST_STRING}" = "speedtest-cli" ]; then + echo "ERROR: Expected 'speedtest-cli' but got '$TEST_STRING'" + exit 1 + fi + release-build: + name: Build multi-platform release candidate + needs: + - test-build + - test + - dockle + - trivy + - anchore + - shiftleft + runs-on: ubuntu-latest + steps: + - name: Checkout tested source + uses: actions/checkout@v7 + with: + ref: ${{ needs.test-build.outputs.tested_sha }} + fetch-depth: 0 + - name: Verify tested source + env: + EXPECTED_SHA: ${{ needs.test-build.outputs.tested_sha }} + run: | + set -euo pipefail + test -n "$EXPECTED_SHA" + test "$(git rev-parse HEAD)" = "$EXPECTED_SHA" + - name: Set up QEMU + uses: docker/setup-qemu-action@v4 + with: + platforms: all + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v4 + with: + driver-opts: network=host + - name: Build multi-platform OCI release candidate + uses: docker/build-push-action@v7 + with: + builder: ${{ steps.buildx.outputs.name }} + push: false + outputs: type=oci,name=localhost:5000/foobar/${{ env.IMAGE_NAME }}:latest,dest=/tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.oci.tar + tags: localhost:5000/foobar/${{ env.IMAGE_NAME }}:latest + cache-from: type=gha,scope=speedtest + cache-to: type=gha,mode=max,scope=speedtest + context: . + file: ./Dockerfile + platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64 + build-args: | + BUILD_DATE=${{ needs.shiftleft.outputs.build_date }} + BUILD_VERSION=${{ needs.test-build.outputs.build_version }} + VCS_REF=${{ needs.test-build.outputs.vcs_ref }} + VCS_URL=${{ needs.test-build.outputs.vcs_url }} + VCS_BRANCH=${{ needs.test-build.outputs.vcs_branch }} + - name: Upload multi-platform OCI image + uses: actions/upload-artifact@v7 + with: + name: multiarch-${{ github.run_id }} + path: /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.oci.tar + if-no-files-found: error + compression-level: 0 + + platform-smoke: + name: Smoke test ${{ matrix.platform }} + needs: + - release-build + strategy: + fail-fast: false + matrix: + include: + - platform: linux/386 + arch: 386 + variant: "" + - platform: linux/amd64 + arch: amd64 + variant: "" + - platform: linux/arm/v6 + arch: arm + variant: v6 + - platform: linux/arm/v7 + arch: arm + variant: v7 + - platform: linux/arm64 + arch: arm64 + variant: "" + runs-on: ubuntu-latest + services: + registry: + image: registry:3 + ports: + - 5000:5000 + steps: + - name: Checkout workflow scripts + uses: actions/checkout@v7 + - name: Download multi-platform OCI image + uses: actions/download-artifact@v8 + with: + name: multiarch-${{ github.run_id }} + path: /tmp + - name: Install Skopeo + run: | + sudo apt-get update + sudo apt-get install --yes skopeo + - name: Set up QEMU + uses: docker/setup-qemu-action@v4 + with: + platforms: all + - name: Select platform image from tested OCI index + env: + ARCHIVE: /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.oci.tar + ARCH: ${{ matrix.arch }} + VARIANT: ${{ matrix.variant }} + PLATFORM_TAG: ${{ matrix.arch }}-${{ matrix.variant || 'default' }} + run: | + set -euo pipefail + args=(--override-os linux --override-arch "$ARCH") + if [[ -n "$VARIANT" ]]; then + args+=(--override-variant "$VARIANT") + fi + skopeo copy --dest-tls-verify=false "${args[@]}" \ + "oci-archive:${ARCHIVE}:latest" \ + "docker://localhost:5000/foobar/${IMAGE_NAME}:${PLATFORM_TAG}" + - name: Run platform smoke test + env: + PLATFORM: ${{ matrix.platform }} + PLATFORM_TAG: ${{ matrix.arch }}-${{ matrix.variant || 'default' }} + run: | + set -euo pipefail + docker pull --platform "$PLATFORM" "localhost:5000/foobar/${IMAGE_NAME}:$PLATFORM_TAG" + output=$(docker run --rm --platform "$PLATFORM" \ + "localhost:5000/foobar/${IMAGE_NAME}:$PLATFORM_TAG" -h) + first_line=$(printf '%s\n' "$output" | head -1) + if [[ "$first_line" != *"speedtest-cli"* ]]; then + echo "::error::Unexpected output for $PLATFORM: $output" + exit 1 + fi + + dockle: + name: Run Dockle tests + needs: + - test-build + strategy: + fail-fast: false + runs-on: ubuntu-latest + services: + registry: + image: registry:3 + ports: + - 5000:5000 + steps: + - name: Download single-platform test image + uses: actions/download-artifact@v8 + with: + name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} + path: /tmp + - name: Load image into local registry + run: | + docker load --input /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar + docker push localhost:5000/foobar/${{ env.IMAGE_NAME }} + - name: Run dockle container image linter + uses: goodwithtech/dockle-action@v0.4.15 + with: + image: "registry:5000/foobar/${{ env.IMAGE_NAME }}" + insecure: true + format: "sarif" + exit-code: "1" + exit-level: "warn" + ignore: "CIS-DI-0001,CIS-DI-0010,DKL-DI-0006" + output: sarif-reports + - name: Upload Reports + uses: github/codeql-action/upload-sarif@v4 + with: + sarif_file: "sarif-reports" + trivy: + name: Run Trivy tests + needs: + - test-build + strategy: + fail-fast: false + runs-on: ubuntu-latest + services: + registry: + image: registry:3 + ports: + - 5000:5000 + steps: + - name: Download single-platform test image + uses: actions/download-artifact@v8 + with: + name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} + path: /tmp + - name: Load image into local registry + run: | + docker load --input /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar + docker push localhost:5000/foobar/${{ env.IMAGE_NAME }} + - name: Run Trivy vulnerability scanner + uses: aquasecurity/trivy-action@v0.36.0 + with: + image-ref: localhost:5000/foobar/${{ env.IMAGE_NAME }} + format: "template" + template: "@/contrib/sarif.tpl" + output: "trivy-results.sarif" + severity: "CRITICAL,HIGH" + - name: Upload Reports + uses: github/codeql-action/upload-sarif@v4 + with: + sarif_file: "trivy-results.sarif" + anchore: + name: Run Anchore tests + needs: + - test-build + strategy: + fail-fast: false + runs-on: ubuntu-latest + services: + registry: + image: registry:3 + ports: + - 5000:5000 + steps: + - name: Download single-platform test image + uses: actions/download-artifact@v8 + with: + name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} + path: /tmp + - name: Load image into local registry + run: | + docker load --input /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar + docker push localhost:5000/foobar/${{ env.IMAGE_NAME }} + - name: Run the Anchore scan action itself with GitHub Advanced Security code scanning integration enabled + uses: anchore/scan-action@v7.4.2 + with: + image: localhost:5000/foobar/${{ env.IMAGE_NAME }} + acs-report-enable: true + severity-cutoff: critical + fail-build: false + output-file: "./results.sarif" + - name: Move reports + run: mkdir -p sarif-reports && cp ./results.sarif ./sarif-reports/ + - name: Upload Reports + uses: github/codeql-action/upload-sarif@v4 + with: + sarif_file: "sarif-reports" + release-docker: + name: Release Docker images + permissions: + contents: write + packages: write + needs: + - test-build + - test + - release-build + - platform-smoke + - dockle + - trivy + - anchore + strategy: + fail-fast: false + runs-on: ubuntu-latest + steps: + - name: Checkout tested source + uses: actions/checkout@v7 + with: + ref: ${{ needs.test-build.outputs.tested_sha }} + fetch-depth: 0 + + - name: Verify tested source + env: + EXPECTED_SHA: ${{ needs.test-build.outputs.tested_sha }} + run: | + set -euo pipefail + test -n "$EXPECTED_SHA" + test "$(git rev-parse HEAD)" = "$EXPECTED_SHA" + + - name: Checkout workflow tools + uses: actions/checkout@v7 + with: + ref: ${{ github.event.repository.default_branch }} + path: .workflow-tools + fetch-depth: 0 + + - name: Prepare image metadata + id: metadata + env: + BUILD_VERSION_INPUT: ${{ inputs.image_version }} + SOURCE_REF_INPUT: ${{ inputs.source_ref }} + HEAD_REF_INPUT: ${{ github.head_ref }} + REF_NAME_INPUT: ${{ github.ref_name }} + GITHUB_EVENT_NAME: ${{ github.event_name }} + run: | + set -euo pipefail + + if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then + vcs_branch="$HEAD_REF_INPUT" + else + vcs_branch="$(bash .workflow-tools/.github/scripts/resolve-vcs-branch.sh)" + fi + + { + echo "build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" + echo "build_version=${BUILD_VERSION_INPUT:-$(git describe --tags --always)}" + echo "vcs_ref=$(git rev-parse --short HEAD)" + echo "vcs_url=$(git config --get remote.origin.url)" + echo "vcs_branch=$vcs_branch" + } >> "$GITHUB_OUTPUT" + + - name: Prepare + id: prep + if: ${{ github.event_name != 'pull_request' }} + env: + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SOURCE_REF_INPUT: ${{ inputs.source_ref }} + IMAGE_VERSION_INPUT: ${{ inputs.image_version }} + PUBLISH_INPUT: ${{ inputs.publish }} + REPLACEMENT_INPUT: ${{ inputs.replacement }} + TEST_ID_INPUT: ${{ inputs.test_id }} + CURRENT_RUN_ID: ${{ github.run_id }} + EVENT_NAME_INPUT: ${{ github.event_name }} + EVENT_REF_INPUT: ${{ github.ref }} + EVENT_BEFORE_INPUT: ${{ github.event.before }} + QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} + QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }} + run: | + USER="${GITHUB_REPOSITORY_OWNER}" + case "$REPLACEMENT_INPUT" in + true|false|"") ;; + *) echo "::error::Invalid replacement input."; exit 1 ;; + esac + replacement_effective=false + suppress_latest=false + if [[ "$REPLACEMENT_INPUT" == "true" ]]; then + replacement_effective=true + suppress_latest=true + fi + + # Direct tag pushes may create a new release, but replacements must + # go through release.yml so the explicit opt-in and tests are enforced. + if [[ "$EVENT_NAME_INPUT" == "push" && "$EVENT_REF_INPUT" == refs/tags/* ]]; then + if [[ "$EVENT_BEFORE_INPUT" != "0000000000000000000000000000000000000000" ]]; then + echo "::error::Updating an existing release tag by push is not allowed." + echo "Use the Release workflow with allow_tag_replacement=true." + exit 1 + fi + fi + IMAGE_NAME="$(echo "$GITHUB_REPOSITORY" | awk -F '/' '{print $2}' | sed s/docker-//)" + DOCKER_IMAGE="${USER}/${IMAGE_NAME}" + REALEASE_TAG=false + VERSION=edge + # running on a tag + if [[ $GITHUB_REF == refs/tags/* ]]; then + VERSION="${GITHUB_REF#refs/tags/}" + fi + # running on a branch + if [[ "$GITHUB_REF" == refs/heads/* ]]; then + VERSION="${GITHUB_REF#refs/heads/}" + fi + # running on schedule + # shellcheck disable=SC2050 + if [ "${{ github.event_name }}" = "schedule" ]; then + VERSION=nightly + fi + # running on master branch + if [ "$VERSION" = "master" ]; then + VERSION=latest + fi + # Release automation can run the workflow from the default branch + # while explicitly checking out the tested merge commit. + if [[ -n "$IMAGE_VERSION_INPUT" ]]; then + VERSION="$IMAGE_VERSION_INPUT" + fi + + # Validate direct new-tag pushes against the actual tag target and + # master history. Existing-tag updates are rejected above. + if [[ "$EVENT_NAME_INPUT" == "push" && "$EVENT_REF_INPUT" == refs/tags/* ]]; then + if [[ ! "$VERSION" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$ ]]; then + echo "::error::Invalid release tag $VERSION." + exit 1 + fi + source_sha=$(git rev-parse HEAD) + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + while [[ "$tag_type" == "tag" ]]; do + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + done + if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then + echo "::error::Pushed tag $VERSION does not resolve to checked-out commit $source_sha." + exit 1 + fi + relation=$(gh api "repos/$GITHUB_REPOSITORY/compare/$source_sha...master" --jq '.status') + if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then + echo "::error::Pushed tag $VERSION is not in master history." + exit 1 + fi + fi + # The replacement bypass is restricted to the validated release + # workflow and re-checks the tag/source relationship independently. + if [[ "$REPLACEMENT_INPUT" == "true" ]]; then + release_tag_pattern='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$' + if [[ "$EVENT_NAME_INPUT" != "workflow_dispatch" || + ! "$VERSION" =~ $release_tag_pattern || + -z "$SOURCE_REF_INPUT" ]]; then + echo "::error::Replacement requires workflow_dispatch, a valid release tag, and an explicit source_ref." + exit 1 + fi + + source_sha=$(git rev-parse HEAD) + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + while [[ "$tag_type" == "tag" ]]; do + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + done + if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then + echo "::error::Replacement tag $VERSION must point to tested source $source_sha." + exit 1 + fi + + relation=$(gh api "repos/$GITHUB_REPOSITORY/compare/$source_sha...master" --jq '.status') + if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then + echo "::error::Replacement source is not in master history." + exit 1 + fi + fi + TAGS="${DOCKER_IMAGE}:${VERSION}" + release_tag_pattern='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$' + if [[ "$VERSION" =~ $release_tag_pattern ]]; then + REALEASE_TAG=true + fi + + if [[ "$EVENT_NAME_INPUT" == "workflow_dispatch" && "$PUBLISH_INPUT" != "false" ]]; then + source_sha=$(git rev-parse HEAD) + if [[ "$TEST_ID_INPUT" == release-publish-* || "$VERSION" =~ $release_tag_pattern ]]; then + expected_prefix="release-publish-$VERSION-$source_sha-run-" + if [[ ! "$VERSION" =~ $release_tag_pattern || "$TEST_ID_INPUT" != "$expected_prefix"* ]]; then + echo "::error::Release publication test_id must match the actual image version and checked-out source SHA ($expected_prefix)." + exit 1 + fi + fi + fi + + # Check whether this source is still the current master before any + # publication can update latest, including direct master builds. + if [[ "$PUBLISH_INPUT" != "false" && + ( "$VERSION" == "latest" || "$VERSION" =~ $release_tag_pattern ) ]]; then + source_sha=$(git rev-parse HEAD) + master_sha=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/master" --jq '.object.sha') + if [[ "$source_sha" != "$master_sha" ]]; then + suppress_latest=true + echo "::notice::Source is not current master; leaving latest unchanged." + fi + fi + + # Publication history and tag checks apply only to actual release + # publications. Test-only dispatches must not require an existing tag. + if [[ "$PUBLISH_INPUT" != "false" && "$VERSION" =~ $release_tag_pattern ]]; then + # Treat every prior publication attempt as potentially having pushed + # an image, even if the workflow later failed or was cancelled. + # Same-SHA retries are safe; a different SHA requires explicit approval. + if [[ "$EVENT_NAME_INPUT" == "push" && "$EVENT_REF_INPUT" == refs/tags/* ]]; then + bash .workflow-tools/.github/scripts/check-release-image-history.sh \ + "repos/$GITHUB_REPOSITORY" "$VERSION" "$source_sha" \ + "$replacement_effective" "$CURRENT_RUN_ID" "true" + fi + + if [[ "$EVENT_NAME_INPUT" == "workflow_dispatch" ]]; then + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + while [[ "$tag_type" == "tag" ]]; do + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + done + if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then + echo "::error::Release tag $VERSION does not resolve to tested source $source_sha." + exit 1 + fi + + relation=$(gh api "repos/$GITHUB_REPOSITORY/compare/$source_sha...master" --jq '.status') + if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then + echo "::error::Release source is not in master history." + exit 1 + fi + + bash .workflow-tools/.github/scripts/check-release-image-history.sh \ + "repos/$GITHUB_REPOSITORY" "$VERSION" "$source_sha" "$replacement_effective" "$CURRENT_RUN_ID" + fi + fi + + # A stale master build must not push a direct :latest tag at all. + # Release-version tags remain publishable; only their latest promotion is suppressed. + if [[ "$VERSION" == "latest" && "$suppress_latest" == "true" ]]; then + TAGS="" + fi + TAGS_INT="$TAGS" + TAGS="" + if [[ -n "$TAGS_INT" ]]; then + if [ "$DOCKERHUB_USERNAME" != '' ] && [ "$DOCKERHUB_TOKEN" != '' ]; then + TAGS="$TAGS_INT" + fi + if [ "$GITHUB_TOKEN" != '' ]; then + if [[ -n "$TAGS" ]]; then + TAGS="$TAGS,ghcr.io/$TAGS_INT" + else + TAGS="ghcr.io/$TAGS_INT" + fi + fi + if [ "$QUAY_USERNAME" != '' ] && [ "$QUAY_TOKEN" != '' ]; then + if [[ -n "$TAGS" ]]; then + TAGS="$TAGS,quay.io/$TAGS_INT" + else + TAGS="quay.io/$TAGS_INT" + fi + fi + fi + latest_candidate=false + if [[ "$PUBLISH_INPUT" != "false" && "$REALEASE_TAG" == "true" && + "$suppress_latest" != "true" && -n "$TAGS" ]]; then + latest_candidate=true + fi + { + echo "tags=${TAGS}" + echo "latest_candidate=$latest_candidate" + } >> "$GITHUB_OUTPUT" + + - name: Download validated multi-platform OCI image + uses: actions/download-artifact@v8 + with: + name: multiarch-${{ github.run_id }} + path: /tmp + + - name: Install Skopeo + run: | + sudo apt-get update + sudo apt-get install --yes skopeo + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v4 + + - name: Login to ghcr + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + if: ${{ github.event_name != 'pull_request' && env.GITHUB_TOKEN != '' }} + uses: docker/login-action@v4 + with: + registry: ghcr.io + username: ${USER} + password: ${{ env.GITHUB_TOKEN }} + + - name: Login to DockerHub Container Registry + env: + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + if: ${{ github.event_name != 'pull_request' && env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }} + uses: docker/login-action@v4 + with: + username: ${{ env.DOCKERHUB_USERNAME }} + password: ${{ env.DOCKERHUB_TOKEN }} + + - name: Login to Quay Container Registry + env: + QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} + QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }} + if: ${{ github.event_name != 'pull_request' && env.QUAY_USERNAME != '' && env.QUAY_TOKEN != '' }} + uses: docker/login-action@v4 + with: + registry: quay.io + username: ${{ env.QUAY_USERNAME }} + password: ${{ env.QUAY_TOKEN }} + + # Keep this step name stable: release-history scripts use it to detect publication attempts. + - name: Build and push + id: docker_build + if: ${{ github.event_name != 'pull_request' && (github.event_name != 'workflow_dispatch' || inputs.publish) && steps.prep.outputs.tags != '' }} + env: + OCI_ARCHIVE: /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.oci.tar + IMAGE_TAGS: ${{ steps.prep.outputs.tags }} + run: | + set -euo pipefail + archive_digest="sha256:$(skopeo inspect --raw "oci-archive:${OCI_ARCHIVE}:latest" | sha256sum | cut -d' ' -f1)" + while IFS= read -r image_tag; do + [[ -z "$image_tag" ]] && continue + if [[ "$image_tag" == ghcr.io/* || "$image_tag" == quay.io/* ]]; then + registry="${image_tag%%/*}" + repository="${image_tag#*/}" + else + registry=docker.io + repository="$image_tag" + fi + destination="docker://${registry}/${repository}" + echo "Publishing ${registry}/${repository} from tested OCI archive." + skopeo copy --authfile "$HOME/.docker/config.json" --all --preserve-digests \ + "oci-archive:${OCI_ARCHIVE}:latest" "$destination" + remote_digest="sha256:$(skopeo inspect --authfile "$HOME/.docker/config.json" --raw "$destination" | sha256sum | cut -d' ' -f1)" + if [[ "$remote_digest" != "$archive_digest" ]]; then + echo "::error::Published digest mismatch for ${registry}/${repository}: expected $archive_digest, got $remote_digest." + exit 1 + fi + echo "Verified ${registry}/${repository} at $remote_digest." + done < <(tr ',' '\n' <<< "$IMAGE_TAGS") + echo "digest=$archive_digest" >> "$GITHUB_OUTPUT" + + - name: Publish the latest image tag + if: steps.prep.outputs.latest_candidate == 'true' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPOSITORY: ${{ github.repository }} + RELEASE_TAG: ${{ inputs.image_version || github.ref_name }} + SOURCE_SHA: ${{ needs.test-build.outputs.tested_sha }} + IMAGE_DIGEST: ${{ steps.docker_build.outputs.digest }} + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} + QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }} + run: | + set -euo pipefail + release_tag_pattern='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$' + if [[ ! "$RELEASE_TAG" =~ $release_tag_pattern || -z "$SOURCE_SHA" || -z "$IMAGE_DIGEST" ]]; then + echo "::error::Invalid release tag or missing source SHA/image digest." + exit 1 + fi + + API="repos/$REPOSITORY" + # Different release versions may publish concurrently. Re-check master + # and publication history immediately before updating latest; a narrow + # race between those checks and the registry write remains possible. + master_sha=$(gh api "$API/git/ref/heads/master" --jq '.object.sha') + if [[ "$SOURCE_SHA" != "$master_sha" ]]; then + echo "::notice::Release source is no longer current master; leaving latest unchanged." + exit 0 + fi + + published_versions=$(bash .workflow-tools/.github/scripts/list-published-release-versions.sh "$API") + highest_prior=$(printf '%s\n' "$published_versions" | sort -V | tail -n1) + if [[ -n "$highest_prior" ]]; then + lowest=$(printf '%s\n%s\n' "$RELEASE_TAG" "$highest_prior" | sort -V | head -n1) + if [[ "$RELEASE_TAG" != "$highest_prior" && "$lowest" == "$RELEASE_TAG" ]]; then + echo "::notice::Release $RELEASE_TAG is older than $highest_prior; leaving latest unchanged." + exit 0 + fi + fi + + USER="$GITHUB_REPOSITORY_OWNER" + IMAGE_NAME="${REPOSITORY##*/}" + IMAGE_NAME="${IMAGE_NAME#docker-}" + DOCKER_IMAGE="$USER/$IMAGE_NAME" + if [[ -n "$DOCKERHUB_USERNAME" && -n "$DOCKERHUB_TOKEN" ]]; then + docker buildx imagetools create --tag "$DOCKER_IMAGE:latest" "$DOCKER_IMAGE@$IMAGE_DIGEST" + fi + if [[ -n "$GITHUB_TOKEN" ]]; then + docker buildx imagetools create --tag "ghcr.io/$DOCKER_IMAGE:latest" "ghcr.io/$DOCKER_IMAGE@$IMAGE_DIGEST" + fi + if [[ -n "$QUAY_USERNAME" && -n "$QUAY_TOKEN" ]]; then + docker buildx imagetools create --tag "quay.io/$DOCKER_IMAGE:latest" "quay.io/$DOCKER_IMAGE@$IMAGE_DIGEST" + fi + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} + + if [[ ! "$RELEASE_TAG" =~ $tag_pattern || -z "$SOURCE_REF_INPUT" ]]; then + echo "::error::Tag creation requires a valid release tag and explicit source_ref." + exit 1 + fi + SOURCE_SHA=$(git rev-parse HEAD) + if [[ "$SOURCE_SHA" != "$SOURCE_REF_INPUT" ]]; then + echo "::error::Checked-out source does not match source_ref." + exit 1 + fi + relation=$(gh api "repos/$REPOSITORY/compare/$SOURCE_SHA...master" --jq '.status') + if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then + echo "::error::Release source is not in master history." + exit 1 + fi + + tag_file=$(mktemp) + tag_error=$(mktemp) + if gh api "repos/$REPOSITORY/git/ref/tags/$RELEASE_TAG" >"$tag_file" 2>"$tag_error"; then + tag_ref_sha=$(jq -r '.object.sha' < "$tag_file") + tag_type=$(jq -r '.object.type' < "$tag_file") + tag_commit_sha=$tag_ref_sha + while [[ "$tag_type" == "tag" ]]; do + tag_object=$(gh api "repos/$REPOSITORY/git/tags/$tag_commit_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_object") + tag_commit_sha=$(jq -r '.object.sha' <<< "$tag_object") + done + if [[ "$tag_type" != "commit" ]]; then + echo "::error::Existing release tag does not resolve to a commit." + exit 1 + fi + if [[ "$REPLACEMENT_INPUT" == "true" ]]; then + if [[ -z "$EXPECTED_TAG_REF_SHA" || "$tag_ref_sha" != "$EXPECTED_TAG_REF_SHA" ]]; then + echo "::error::Release tag changed since replacement was approved." + exit 1 + fi + if [[ "$tag_commit_sha" != "$SOURCE_SHA" ]]; then + auth=$(printf 'x-access-token:%s' "$TAG_PUSH_TOKEN" | base64 -w0) + git -c "http.extraheader=AUTHORIZATION: basic $auth" \\ + push --force-with-lease="refs/tags/$RELEASE_TAG:$EXPECTED_TAG_REF_SHA" \\ + origin "$SOURCE_SHA:refs/tags/$RELEASE_TAG" + echo "Moved $RELEASE_TAG to tested commit $SOURCE_SHA." + else + echo "Release tag already points to tested commit $SOURCE_SHA." + fi + elif [[ "$tag_commit_sha" == "$SOURCE_SHA" ]]; then + echo "Release tag already points to tested commit $SOURCE_SHA." + else + echo "::error::Release tag already exists at a different commit." + exit 1 + fi + else + if [[ "$REPLACEMENT_INPUT" == "true" ]]; then + echo "::error::Cannot replace a tag that does not exist." + exit 1 + fi + if ! grep -Eqi 'HTTP 404|status.?404' "$tag_error"; then + cat "$tag_error" + echo "::error::Could not safely determine whether the release tag exists." + exit 1 + fi + if ! GH_TOKEN="$TAG_PUSH_TOKEN" gh api --method POST "repos/$REPOSITORY/git/refs" \\ + -f "ref=refs/tags/$RELEASE_TAG" -f "sha=$SOURCE_SHA"; then + echo "::error::Could not create the release tag." + exit 1 + fi + echo "Created $RELEASE_TAG at tested commit $SOURCE_SHA." + fi + - name: Prepare image metadata id: metadata env: @@ -501,6 +1412,9 @@ jobs: sarif_file: "sarif-reports" release-docker: name: Release Docker images + permissions: + contents: write + packages: write needs: - test-build - test From c725f6263facfacd9295145644365f7dfd790bf4 Mon Sep 17 00:00:00 2001 From: waja Date: Mon, 16 Nov 2026 00:03:47 +0100 Subject: [PATCH 2/5] Dispatch a single validated release pipeline --- .github/workflows/release.yml | 102 +--------------------------------- 1 file changed, 3 insertions(+), 99 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b778895..370e65e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -424,104 +424,6 @@ jobs: bash .github/scripts/check-release-image-history.sh \ "repos/$REPOSITORY" "$RELEASE_TAG" "$RELEASE_SHA" "$ALLOW_TAG_REPLACEMENT" - - name: Start or reuse tests for the resulting merge commit - id: test_dispatch - env: - GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} - REPOSITORY: ${{ github.repository }} - RELEASE_TAG: ${{ inputs.tag }} - RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} - run: | - set -euo pipefail - API="repos/$REPOSITORY" - DEFAULT_BRANCH=$(gh api "$API" --jq '.default_branch') - TEST_ID="release-test-$RELEASE_TAG-$RELEASE_SHA" - TEST_TITLE="Docker Image CI test-$TEST_ID" - - run_lines=$(gh api --paginate --method GET \ - "$API/actions/workflows/docker.image.yml/runs" \ - -f event=workflow_dispatch -F per_page=100 \ - --jq '.workflow_runs[] | {id,event,display_title,status,conclusion,created_at,html_url}') - existing=$(jq -s -c --arg title "$TEST_TITLE" ' - [.[] | select(.display_title == $title)] - | sort_by(.created_at) | last // empty - ' <<< "$run_lines") - - if [[ -n "$existing" ]]; then - status=$(jq -r '.status' <<< "$existing") - conclusion=$(jq -r '.conclusion // empty' <<< "$existing") - run_id=$(jq -r '.id' <<< "$existing") - if [[ "$status" != "completed" || "$conclusion" == "success" ]]; then - echo "run_id=$run_id" >> "$GITHUB_OUTPUT" - echo "Reusing merge-commit test run $run_id ($status)." - exit 0 - fi - echo "The prior test run failed; starting a fresh test run." - fi - - dispatched_at=$(date -u +'%Y-%m-%dT%H:%M:%SZ') - gh workflow run docker.image.yml --repo "$REPOSITORY" \ - --ref "$DEFAULT_BRANCH" \ - -f publish=false -f source_ref="$RELEASE_SHA" \ - -f test_id="$TEST_ID" -f image_version="$RELEASE_TAG" - echo "dispatched_at=$dispatched_at" >> "$GITHUB_OUTPUT" - echo "Started tests for merge commit $RELEASE_SHA." - - - name: Wait for tests on the resulting merge commit - env: - GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} - REPOSITORY: ${{ github.repository }} - RELEASE_TAG: ${{ inputs.tag }} - RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} - RUN_ID: ${{ steps.test_dispatch.outputs.run_id }} - DISPLAY_TITLE: Docker Image CI test-release-test-${{ inputs.tag }}-${{ steps.release_commit.outputs.sha }} - DISPATCHED_AT: ${{ steps.test_dispatch.outputs.dispatched_at }} - run: | - set -euo pipefail - API="repos/$REPOSITORY" - bash .github/scripts/wait-for-workflow-run.sh \ - "$API" "$RUN_ID" "$DISPLAY_TITLE" "$DISPATCHED_AT" 5400 - - - name: Create or replace tag only after merge-commit tests pass - if: steps.validate.outputs.mode != 'tagged' - env: - GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} - TAG_PUSH_TOKEN: ${{ github.token }} - REPOSITORY: ${{ github.repository }} - RELEASE_TAG: ${{ inputs.tag }} - RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} - run: | - set -euo pipefail - API="repos/$REPOSITORY" - relation=$(gh api "$API/compare/$RELEASE_SHA...master" --jq '.status') - if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then - echo "::error::Release commit is no longer in master history." - exit 1 - fi - - if [[ "${{ steps.validate.outputs.mode }}" == "replace_tagged" ]]; then - expected_ref_sha="${{ steps.validate.outputs.tag_ref_sha }}" - # GitHub's ref PATCH endpoint has no compare-and-swap condition. - # A force-with-lease push atomically rejects a stale expected ref. - # Use GITHUB_TOKEN for the tag mutation so this workflow's - # own replacement does not trigger a competing tag-push publisher. - git -c "http.extraheader=AUTHORIZATION: basic $(printf 'x-access-token:%s' "$TAG_PUSH_TOKEN" | base64 -w0)" \ - fetch --no-tags origin master - git -c "http.extraheader=AUTHORIZATION: basic $(printf 'x-access-token:%s' "$TAG_PUSH_TOKEN" | base64 -w0)" \ - push --force-with-lease="refs/tags/$RELEASE_TAG:$expected_ref_sha" \ - origin "$RELEASE_SHA:refs/tags/$RELEASE_TAG" - echo "Moved $RELEASE_TAG to tested commit $RELEASE_SHA." - else - # The tag was absent during validation. POST cannot overwrite it. - if ! GH_TOKEN="$TAG_PUSH_TOKEN" gh api --method POST "$API/git/refs" \ - -f "ref=refs/tags/$RELEASE_TAG" -f "sha=$RELEASE_SHA"; then - echo "::error::Could not create the release tag." - echo "The tag may have been created concurrently; retry to resume." - exit 1 - fi - echo "Created $RELEASE_TAG at tested commit $RELEASE_SHA." - fi - - name: Start or resume Docker image publication id: docker_dispatch env: @@ -589,7 +491,9 @@ jobs: -f publish=true -f source_ref="$RELEASE_SHA" \ -f test_id="$PUBLISH_TEST_ID" \ -f image_version="$RELEASE_TAG" \ - -f replacement="$replacement_effective" + -f replacement="$replacement_effective" \\ + -f create_release_tag="$([[ "$RELEASE_MODE" == "tagged" ]] && echo false || echo true)" \\ + -f expected_tag_ref_sha="${{ steps.validate.outputs.tag_ref_sha }}" echo "dispatched_at=$dispatched_at" >> "$GITHUB_OUTPUT" echo "Started Docker image publication for $RELEASE_TAG." From 89444e3d356775d9ccf2ade4f77ce2090249e7d8 Mon Sep 17 00:00:00 2001 From: waja Date: Mon, 16 Nov 2026 00:04:07 +0100 Subject: [PATCH 3/5] Restore development workflow after branching release pipeline changes --- .github/workflows/docker.image.yml | 914 ----------------------------- 1 file changed, 914 deletions(-) diff --git a/.github/workflows/docker.image.yml b/.github/workflows/docker.image.yml index 9ec7f5b..3970d3d 100644 --- a/.github/workflows/docker.image.yml +++ b/.github/workflows/docker.image.yml @@ -33,16 +33,6 @@ on: required: false type: boolean default: false - create_release_tag: - description: "Create or move the release tag after all validation jobs pass" - required: false - type: boolean - default: false - expected_tag_ref_sha: - description: "Expected existing tag ref SHA for an atomic replacement" - required: false - type: string - default: "" push: branches: - $default-branch @@ -111,907 +101,6 @@ jobs: path: .workflow-tools fetch-depth: 0 - - name: Create or verify release tag after all validation jobs pass - if: ${{ github.event_name == 'workflow_dispatch' && inputs.create_release_tag }} - env: - GH_TOKEN: ${{ github.token }} - TAG_PUSH_TOKEN: ${{ github.token }} - RELEASE_TAG: ${{ inputs.image_version }} - SOURCE_REF_INPUT: ${{ inputs.source_ref }} - REPLACEMENT_INPUT: ${{ inputs.replacement }} - EXPECTED_TAG_REF_SHA: ${{ inputs.expected_tag_ref_sha }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - tag_pattern='^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}-[0-9]{1,3} env: - BUILD_VERSION_INPUT: ${{ inputs.image_version }} - SOURCE_REF_INPUT: ${{ inputs.source_ref }} - HEAD_REF_INPUT: ${{ github.head_ref }} - REF_NAME_INPUT: ${{ github.ref_name }} - GITHUB_EVENT_NAME: ${{ github.event_name }} - run: | - set -euo pipefail - - if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then - vcs_branch="$HEAD_REF_INPUT" - else - vcs_branch="$(bash .workflow-tools/.github/scripts/resolve-vcs-branch.sh)" - fi - - { - echo "build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" - echo "build_version=${BUILD_VERSION_INPUT:-$(git describe --tags --always)}" - echo "vcs_ref=$(git rev-parse --short HEAD)" - echo "vcs_url=$(git config --get remote.origin.url)" - echo "vcs_branch=$vcs_branch" - } >> "$GITHUB_OUTPUT" - - - name: Perform ShiftLeft Scan - uses: ShiftLeftSecurity/scan-action@v1.3.0 - env: - WORKSPACE: "" - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SCAN_AUTO_BUILD: true - with: - output: reports - # Scan auto-detects the languages in your project. To override uncomment the below variable and set the type - # type: credscan,java - # type: python - - name: Upload report - uses: github/codeql-action/upload-sarif@v4 - with: - sarif_file: reports - test-build: - needs: - - super-linter - - shiftleft - outputs: - tested_sha: ${{ steps.build_metadata.outputs.vcs_sha }} - build_version: ${{ steps.build_metadata.outputs.build_version }} - vcs_ref: ${{ steps.build_metadata.outputs.vcs_ref }} - vcs_url: ${{ steps.build_metadata.outputs.vcs_url }} - vcs_branch: ${{ steps.build_metadata.outputs.vcs_branch }} - strategy: - fail-fast: false - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v7 - with: - ref: ${{ inputs.source_ref || ((github.event_name == 'pull_request' || github.event_name == 'push') && github.sha) || github.ref }} - fetch-depth: 0 - - name: Checkout workflow tools - uses: actions/checkout@v7 - with: - ref: ${{ github.event.repository.default_branch }} - path: .workflow-tools - fetch-depth: 0 - - - name: Prepare build commit metadata - id: build_metadata - env: - BUILD_VERSION_INPUT: ${{ inputs.image_version }} - SOURCE_REF_INPUT: ${{ inputs.source_ref }} - HEAD_REF_INPUT: ${{ github.head_ref }} - REF_NAME_INPUT: ${{ github.ref_name }} - GITHUB_EVENT_NAME: ${{ github.event_name }} - run: | - set -euo pipefail - - if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then - vcs_branch="$HEAD_REF_INPUT" - else - vcs_branch="$(bash .workflow-tools/.github/scripts/resolve-vcs-branch.sh)" - fi - - { - echo "vcs_sha=$(git rev-parse HEAD)" - echo "vcs_ref=$(git rev-parse --short HEAD)" - echo "build_version=${BUILD_VERSION_INPUT:-$(git describe --tags --always)}" - echo "vcs_url=$(git config --get remote.origin.url)" - echo "vcs_branch=$vcs_branch" - } >> "$GITHUB_OUTPUT" - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@v4 - with: - driver-opts: network=host - - name: Build single-platform test image - id: docker_build - uses: docker/build-push-action@v7 - with: - builder: ${{ steps.buildx.outputs.name }} - push: false - outputs: type=docker,dest=/tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar - tags: localhost:5000/foobar/${{ env.IMAGE_NAME }} - cache-from: type=gha,scope=speedtest - cache-to: type=gha,mode=max,scope=speedtest - context: . - file: ./Dockerfile - platforms: linux/amd64 - build-args: | - BUILD_DATE=${{ needs.shiftleft.outputs.build_date }} - BUILD_VERSION=${{ steps.build_metadata.outputs.build_version }} - VCS_REF=${{ steps.build_metadata.outputs.vcs_ref }} - VCS_URL=${{ steps.build_metadata.outputs.vcs_url }} - VCS_BRANCH=${{ steps.build_metadata.outputs.vcs_branch }} - - name: Upload single-platform test image - uses: actions/upload-artifact@v7 - with: - name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} - path: /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar - if-no-files-found: error - compression-level: 0 - test: - name: Run test - needs: - - test-build - strategy: - fail-fast: false - runs-on: ubuntu-latest - services: - registry: - image: registry:3 - ports: - - 5000:5000 - steps: - - name: Download single-platform test image - uses: actions/download-artifact@v8 - with: - name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} - path: /tmp - - name: Load image into local registry - run: | - docker load --input /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar - docker push localhost:5000/foobar/${{ env.IMAGE_NAME }} - - name: Inspect the Docker image - run: | - docker buildx imagetools inspect localhost:5000/foobar/${{ env.IMAGE_NAME }} - - name: Test the Docker image - run: | - CONTAINER_OUTPUT=$(docker run --rm localhost:5000/foobar/${{ env.IMAGE_NAME }} -h) - echo "Container output: $CONTAINER_OUTPUT" - echo "Container output (quoted): '$CONTAINER_OUTPUT'" - - TEST_STRING=$(echo "${CONTAINER_OUTPUT}" | head -1 | cut -d' ' -f2 | tr -d '\r') - echo "Extracted test string: '$TEST_STRING'" - - if ! [ "${TEST_STRING}" = "speedtest-cli" ]; then - echo "ERROR: Expected 'speedtest-cli' but got '$TEST_STRING'" - exit 1 - fi - release-build: - name: Build multi-platform release candidate - needs: - - test-build - - test - - dockle - - trivy - - anchore - - shiftleft - runs-on: ubuntu-latest - steps: - - name: Checkout tested source - uses: actions/checkout@v7 - with: - ref: ${{ needs.test-build.outputs.tested_sha }} - fetch-depth: 0 - - name: Verify tested source - env: - EXPECTED_SHA: ${{ needs.test-build.outputs.tested_sha }} - run: | - set -euo pipefail - test -n "$EXPECTED_SHA" - test "$(git rev-parse HEAD)" = "$EXPECTED_SHA" - - name: Set up QEMU - uses: docker/setup-qemu-action@v4 - with: - platforms: all - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@v4 - with: - driver-opts: network=host - - name: Build multi-platform OCI release candidate - uses: docker/build-push-action@v7 - with: - builder: ${{ steps.buildx.outputs.name }} - push: false - outputs: type=oci,name=localhost:5000/foobar/${{ env.IMAGE_NAME }}:latest,dest=/tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.oci.tar - tags: localhost:5000/foobar/${{ env.IMAGE_NAME }}:latest - cache-from: type=gha,scope=speedtest - cache-to: type=gha,mode=max,scope=speedtest - context: . - file: ./Dockerfile - platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64 - build-args: | - BUILD_DATE=${{ needs.shiftleft.outputs.build_date }} - BUILD_VERSION=${{ needs.test-build.outputs.build_version }} - VCS_REF=${{ needs.test-build.outputs.vcs_ref }} - VCS_URL=${{ needs.test-build.outputs.vcs_url }} - VCS_BRANCH=${{ needs.test-build.outputs.vcs_branch }} - - name: Upload multi-platform OCI image - uses: actions/upload-artifact@v7 - with: - name: multiarch-${{ github.run_id }} - path: /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.oci.tar - if-no-files-found: error - compression-level: 0 - - platform-smoke: - name: Smoke test ${{ matrix.platform }} - needs: - - release-build - strategy: - fail-fast: false - matrix: - include: - - platform: linux/386 - arch: 386 - variant: "" - - platform: linux/amd64 - arch: amd64 - variant: "" - - platform: linux/arm/v6 - arch: arm - variant: v6 - - platform: linux/arm/v7 - arch: arm - variant: v7 - - platform: linux/arm64 - arch: arm64 - variant: "" - runs-on: ubuntu-latest - services: - registry: - image: registry:3 - ports: - - 5000:5000 - steps: - - name: Checkout workflow scripts - uses: actions/checkout@v7 - - name: Download multi-platform OCI image - uses: actions/download-artifact@v8 - with: - name: multiarch-${{ github.run_id }} - path: /tmp - - name: Install Skopeo - run: | - sudo apt-get update - sudo apt-get install --yes skopeo - - name: Set up QEMU - uses: docker/setup-qemu-action@v4 - with: - platforms: all - - name: Select platform image from tested OCI index - env: - ARCHIVE: /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.oci.tar - ARCH: ${{ matrix.arch }} - VARIANT: ${{ matrix.variant }} - PLATFORM_TAG: ${{ matrix.arch }}-${{ matrix.variant || 'default' }} - run: | - set -euo pipefail - args=(--override-os linux --override-arch "$ARCH") - if [[ -n "$VARIANT" ]]; then - args+=(--override-variant "$VARIANT") - fi - skopeo copy --dest-tls-verify=false "${args[@]}" \ - "oci-archive:${ARCHIVE}:latest" \ - "docker://localhost:5000/foobar/${IMAGE_NAME}:${PLATFORM_TAG}" - - name: Run platform smoke test - env: - PLATFORM: ${{ matrix.platform }} - PLATFORM_TAG: ${{ matrix.arch }}-${{ matrix.variant || 'default' }} - run: | - set -euo pipefail - docker pull --platform "$PLATFORM" "localhost:5000/foobar/${IMAGE_NAME}:$PLATFORM_TAG" - output=$(docker run --rm --platform "$PLATFORM" \ - "localhost:5000/foobar/${IMAGE_NAME}:$PLATFORM_TAG" -h) - first_line=$(printf '%s\n' "$output" | head -1) - if [[ "$first_line" != *"speedtest-cli"* ]]; then - echo "::error::Unexpected output for $PLATFORM: $output" - exit 1 - fi - - dockle: - name: Run Dockle tests - needs: - - test-build - strategy: - fail-fast: false - runs-on: ubuntu-latest - services: - registry: - image: registry:3 - ports: - - 5000:5000 - steps: - - name: Download single-platform test image - uses: actions/download-artifact@v8 - with: - name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} - path: /tmp - - name: Load image into local registry - run: | - docker load --input /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar - docker push localhost:5000/foobar/${{ env.IMAGE_NAME }} - - name: Run dockle container image linter - uses: goodwithtech/dockle-action@v0.4.15 - with: - image: "registry:5000/foobar/${{ env.IMAGE_NAME }}" - insecure: true - format: "sarif" - exit-code: "1" - exit-level: "warn" - ignore: "CIS-DI-0001,CIS-DI-0010,DKL-DI-0006" - output: sarif-reports - - name: Upload Reports - uses: github/codeql-action/upload-sarif@v4 - with: - sarif_file: "sarif-reports" - trivy: - name: Run Trivy tests - needs: - - test-build - strategy: - fail-fast: false - runs-on: ubuntu-latest - services: - registry: - image: registry:3 - ports: - - 5000:5000 - steps: - - name: Download single-platform test image - uses: actions/download-artifact@v8 - with: - name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} - path: /tmp - - name: Load image into local registry - run: | - docker load --input /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar - docker push localhost:5000/foobar/${{ env.IMAGE_NAME }} - - name: Run Trivy vulnerability scanner - uses: aquasecurity/trivy-action@v0.36.0 - with: - image-ref: localhost:5000/foobar/${{ env.IMAGE_NAME }} - format: "template" - template: "@/contrib/sarif.tpl" - output: "trivy-results.sarif" - severity: "CRITICAL,HIGH" - - name: Upload Reports - uses: github/codeql-action/upload-sarif@v4 - with: - sarif_file: "trivy-results.sarif" - anchore: - name: Run Anchore tests - needs: - - test-build - strategy: - fail-fast: false - runs-on: ubuntu-latest - services: - registry: - image: registry:3 - ports: - - 5000:5000 - steps: - - name: Download single-platform test image - uses: actions/download-artifact@v8 - with: - name: ${{ env.IMAGE_NAME }}-${{ github.run_id }} - path: /tmp - - name: Load image into local registry - run: | - docker load --input /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.tar - docker push localhost:5000/foobar/${{ env.IMAGE_NAME }} - - name: Run the Anchore scan action itself with GitHub Advanced Security code scanning integration enabled - uses: anchore/scan-action@v7.4.2 - with: - image: localhost:5000/foobar/${{ env.IMAGE_NAME }} - acs-report-enable: true - severity-cutoff: critical - fail-build: false - output-file: "./results.sarif" - - name: Move reports - run: mkdir -p sarif-reports && cp ./results.sarif ./sarif-reports/ - - name: Upload Reports - uses: github/codeql-action/upload-sarif@v4 - with: - sarif_file: "sarif-reports" - release-docker: - name: Release Docker images - permissions: - contents: write - packages: write - needs: - - test-build - - test - - release-build - - platform-smoke - - dockle - - trivy - - anchore - strategy: - fail-fast: false - runs-on: ubuntu-latest - steps: - - name: Checkout tested source - uses: actions/checkout@v7 - with: - ref: ${{ needs.test-build.outputs.tested_sha }} - fetch-depth: 0 - - - name: Verify tested source - env: - EXPECTED_SHA: ${{ needs.test-build.outputs.tested_sha }} - run: | - set -euo pipefail - test -n "$EXPECTED_SHA" - test "$(git rev-parse HEAD)" = "$EXPECTED_SHA" - - - name: Checkout workflow tools - uses: actions/checkout@v7 - with: - ref: ${{ github.event.repository.default_branch }} - path: .workflow-tools - fetch-depth: 0 - - - name: Prepare image metadata - id: metadata - env: - BUILD_VERSION_INPUT: ${{ inputs.image_version }} - SOURCE_REF_INPUT: ${{ inputs.source_ref }} - HEAD_REF_INPUT: ${{ github.head_ref }} - REF_NAME_INPUT: ${{ github.ref_name }} - GITHUB_EVENT_NAME: ${{ github.event_name }} - run: | - set -euo pipefail - - if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then - vcs_branch="$HEAD_REF_INPUT" - else - vcs_branch="$(bash .workflow-tools/.github/scripts/resolve-vcs-branch.sh)" - fi - - { - echo "build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" - echo "build_version=${BUILD_VERSION_INPUT:-$(git describe --tags --always)}" - echo "vcs_ref=$(git rev-parse --short HEAD)" - echo "vcs_url=$(git config --get remote.origin.url)" - echo "vcs_branch=$vcs_branch" - } >> "$GITHUB_OUTPUT" - - - name: Prepare - id: prep - if: ${{ github.event_name != 'pull_request' }} - env: - DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SOURCE_REF_INPUT: ${{ inputs.source_ref }} - IMAGE_VERSION_INPUT: ${{ inputs.image_version }} - PUBLISH_INPUT: ${{ inputs.publish }} - REPLACEMENT_INPUT: ${{ inputs.replacement }} - TEST_ID_INPUT: ${{ inputs.test_id }} - CURRENT_RUN_ID: ${{ github.run_id }} - EVENT_NAME_INPUT: ${{ github.event_name }} - EVENT_REF_INPUT: ${{ github.ref }} - EVENT_BEFORE_INPUT: ${{ github.event.before }} - QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} - QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }} - run: | - USER="${GITHUB_REPOSITORY_OWNER}" - case "$REPLACEMENT_INPUT" in - true|false|"") ;; - *) echo "::error::Invalid replacement input."; exit 1 ;; - esac - replacement_effective=false - suppress_latest=false - if [[ "$REPLACEMENT_INPUT" == "true" ]]; then - replacement_effective=true - suppress_latest=true - fi - - # Direct tag pushes may create a new release, but replacements must - # go through release.yml so the explicit opt-in and tests are enforced. - if [[ "$EVENT_NAME_INPUT" == "push" && "$EVENT_REF_INPUT" == refs/tags/* ]]; then - if [[ "$EVENT_BEFORE_INPUT" != "0000000000000000000000000000000000000000" ]]; then - echo "::error::Updating an existing release tag by push is not allowed." - echo "Use the Release workflow with allow_tag_replacement=true." - exit 1 - fi - fi - IMAGE_NAME="$(echo "$GITHUB_REPOSITORY" | awk -F '/' '{print $2}' | sed s/docker-//)" - DOCKER_IMAGE="${USER}/${IMAGE_NAME}" - REALEASE_TAG=false - VERSION=edge - # running on a tag - if [[ $GITHUB_REF == refs/tags/* ]]; then - VERSION="${GITHUB_REF#refs/tags/}" - fi - # running on a branch - if [[ "$GITHUB_REF" == refs/heads/* ]]; then - VERSION="${GITHUB_REF#refs/heads/}" - fi - # running on schedule - # shellcheck disable=SC2050 - if [ "${{ github.event_name }}" = "schedule" ]; then - VERSION=nightly - fi - # running on master branch - if [ "$VERSION" = "master" ]; then - VERSION=latest - fi - # Release automation can run the workflow from the default branch - # while explicitly checking out the tested merge commit. - if [[ -n "$IMAGE_VERSION_INPUT" ]]; then - VERSION="$IMAGE_VERSION_INPUT" - fi - - # Validate direct new-tag pushes against the actual tag target and - # master history. Existing-tag updates are rejected above. - if [[ "$EVENT_NAME_INPUT" == "push" && "$EVENT_REF_INPUT" == refs/tags/* ]]; then - if [[ ! "$VERSION" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$ ]]; then - echo "::error::Invalid release tag $VERSION." - exit 1 - fi - source_sha=$(git rev-parse HEAD) - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") - tag_type=$(jq -r '.object.type' <<< "$tag_json") - tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - while [[ "$tag_type" == "tag" ]]; do - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") - tag_type=$(jq -r '.object.type' <<< "$tag_json") - tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - done - if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then - echo "::error::Pushed tag $VERSION does not resolve to checked-out commit $source_sha." - exit 1 - fi - relation=$(gh api "repos/$GITHUB_REPOSITORY/compare/$source_sha...master" --jq '.status') - if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then - echo "::error::Pushed tag $VERSION is not in master history." - exit 1 - fi - fi - # The replacement bypass is restricted to the validated release - # workflow and re-checks the tag/source relationship independently. - if [[ "$REPLACEMENT_INPUT" == "true" ]]; then - release_tag_pattern='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$' - if [[ "$EVENT_NAME_INPUT" != "workflow_dispatch" || - ! "$VERSION" =~ $release_tag_pattern || - -z "$SOURCE_REF_INPUT" ]]; then - echo "::error::Replacement requires workflow_dispatch, a valid release tag, and an explicit source_ref." - exit 1 - fi - - source_sha=$(git rev-parse HEAD) - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") - tag_type=$(jq -r '.object.type' <<< "$tag_json") - tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - while [[ "$tag_type" == "tag" ]]; do - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") - tag_type=$(jq -r '.object.type' <<< "$tag_json") - tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - done - if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then - echo "::error::Replacement tag $VERSION must point to tested source $source_sha." - exit 1 - fi - - relation=$(gh api "repos/$GITHUB_REPOSITORY/compare/$source_sha...master" --jq '.status') - if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then - echo "::error::Replacement source is not in master history." - exit 1 - fi - fi - TAGS="${DOCKER_IMAGE}:${VERSION}" - release_tag_pattern='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$' - if [[ "$VERSION" =~ $release_tag_pattern ]]; then - REALEASE_TAG=true - fi - - if [[ "$EVENT_NAME_INPUT" == "workflow_dispatch" && "$PUBLISH_INPUT" != "false" ]]; then - source_sha=$(git rev-parse HEAD) - if [[ "$TEST_ID_INPUT" == release-publish-* || "$VERSION" =~ $release_tag_pattern ]]; then - expected_prefix="release-publish-$VERSION-$source_sha-run-" - if [[ ! "$VERSION" =~ $release_tag_pattern || "$TEST_ID_INPUT" != "$expected_prefix"* ]]; then - echo "::error::Release publication test_id must match the actual image version and checked-out source SHA ($expected_prefix)." - exit 1 - fi - fi - fi - - # Check whether this source is still the current master before any - # publication can update latest, including direct master builds. - if [[ "$PUBLISH_INPUT" != "false" && - ( "$VERSION" == "latest" || "$VERSION" =~ $release_tag_pattern ) ]]; then - source_sha=$(git rev-parse HEAD) - master_sha=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/master" --jq '.object.sha') - if [[ "$source_sha" != "$master_sha" ]]; then - suppress_latest=true - echo "::notice::Source is not current master; leaving latest unchanged." - fi - fi - - # Publication history and tag checks apply only to actual release - # publications. Test-only dispatches must not require an existing tag. - if [[ "$PUBLISH_INPUT" != "false" && "$VERSION" =~ $release_tag_pattern ]]; then - # Treat every prior publication attempt as potentially having pushed - # an image, even if the workflow later failed or was cancelled. - # Same-SHA retries are safe; a different SHA requires explicit approval. - if [[ "$EVENT_NAME_INPUT" == "push" && "$EVENT_REF_INPUT" == refs/tags/* ]]; then - bash .workflow-tools/.github/scripts/check-release-image-history.sh \ - "repos/$GITHUB_REPOSITORY" "$VERSION" "$source_sha" \ - "$replacement_effective" "$CURRENT_RUN_ID" "true" - fi - - if [[ "$EVENT_NAME_INPUT" == "workflow_dispatch" ]]; then - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") - tag_type=$(jq -r '.object.type' <<< "$tag_json") - tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - while [[ "$tag_type" == "tag" ]]; do - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") - tag_type=$(jq -r '.object.type' <<< "$tag_json") - tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - done - if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then - echo "::error::Release tag $VERSION does not resolve to tested source $source_sha." - exit 1 - fi - - relation=$(gh api "repos/$GITHUB_REPOSITORY/compare/$source_sha...master" --jq '.status') - if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then - echo "::error::Release source is not in master history." - exit 1 - fi - - bash .workflow-tools/.github/scripts/check-release-image-history.sh \ - "repos/$GITHUB_REPOSITORY" "$VERSION" "$source_sha" "$replacement_effective" "$CURRENT_RUN_ID" - fi - fi - - # A stale master build must not push a direct :latest tag at all. - # Release-version tags remain publishable; only their latest promotion is suppressed. - if [[ "$VERSION" == "latest" && "$suppress_latest" == "true" ]]; then - TAGS="" - fi - TAGS_INT="$TAGS" - TAGS="" - if [[ -n "$TAGS_INT" ]]; then - if [ "$DOCKERHUB_USERNAME" != '' ] && [ "$DOCKERHUB_TOKEN" != '' ]; then - TAGS="$TAGS_INT" - fi - if [ "$GITHUB_TOKEN" != '' ]; then - if [[ -n "$TAGS" ]]; then - TAGS="$TAGS,ghcr.io/$TAGS_INT" - else - TAGS="ghcr.io/$TAGS_INT" - fi - fi - if [ "$QUAY_USERNAME" != '' ] && [ "$QUAY_TOKEN" != '' ]; then - if [[ -n "$TAGS" ]]; then - TAGS="$TAGS,quay.io/$TAGS_INT" - else - TAGS="quay.io/$TAGS_INT" - fi - fi - fi - latest_candidate=false - if [[ "$PUBLISH_INPUT" != "false" && "$REALEASE_TAG" == "true" && - "$suppress_latest" != "true" && -n "$TAGS" ]]; then - latest_candidate=true - fi - { - echo "tags=${TAGS}" - echo "latest_candidate=$latest_candidate" - } >> "$GITHUB_OUTPUT" - - - name: Download validated multi-platform OCI image - uses: actions/download-artifact@v8 - with: - name: multiarch-${{ github.run_id }} - path: /tmp - - - name: Install Skopeo - run: | - sudo apt-get update - sudo apt-get install --yes skopeo - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v4 - - - name: Login to ghcr - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - if: ${{ github.event_name != 'pull_request' && env.GITHUB_TOKEN != '' }} - uses: docker/login-action@v4 - with: - registry: ghcr.io - username: ${USER} - password: ${{ env.GITHUB_TOKEN }} - - - name: Login to DockerHub Container Registry - env: - DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} - if: ${{ github.event_name != 'pull_request' && env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }} - uses: docker/login-action@v4 - with: - username: ${{ env.DOCKERHUB_USERNAME }} - password: ${{ env.DOCKERHUB_TOKEN }} - - - name: Login to Quay Container Registry - env: - QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} - QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }} - if: ${{ github.event_name != 'pull_request' && env.QUAY_USERNAME != '' && env.QUAY_TOKEN != '' }} - uses: docker/login-action@v4 - with: - registry: quay.io - username: ${{ env.QUAY_USERNAME }} - password: ${{ env.QUAY_TOKEN }} - - # Keep this step name stable: release-history scripts use it to detect publication attempts. - - name: Build and push - id: docker_build - if: ${{ github.event_name != 'pull_request' && (github.event_name != 'workflow_dispatch' || inputs.publish) && steps.prep.outputs.tags != '' }} - env: - OCI_ARCHIVE: /tmp/${{ env.IMAGE_NAME }}-${{ github.run_id }}.oci.tar - IMAGE_TAGS: ${{ steps.prep.outputs.tags }} - run: | - set -euo pipefail - archive_digest="sha256:$(skopeo inspect --raw "oci-archive:${OCI_ARCHIVE}:latest" | sha256sum | cut -d' ' -f1)" - while IFS= read -r image_tag; do - [[ -z "$image_tag" ]] && continue - if [[ "$image_tag" == ghcr.io/* || "$image_tag" == quay.io/* ]]; then - registry="${image_tag%%/*}" - repository="${image_tag#*/}" - else - registry=docker.io - repository="$image_tag" - fi - destination="docker://${registry}/${repository}" - echo "Publishing ${registry}/${repository} from tested OCI archive." - skopeo copy --authfile "$HOME/.docker/config.json" --all --preserve-digests \ - "oci-archive:${OCI_ARCHIVE}:latest" "$destination" - remote_digest="sha256:$(skopeo inspect --authfile "$HOME/.docker/config.json" --raw "$destination" | sha256sum | cut -d' ' -f1)" - if [[ "$remote_digest" != "$archive_digest" ]]; then - echo "::error::Published digest mismatch for ${registry}/${repository}: expected $archive_digest, got $remote_digest." - exit 1 - fi - echo "Verified ${registry}/${repository} at $remote_digest." - done < <(tr ',' '\n' <<< "$IMAGE_TAGS") - echo "digest=$archive_digest" >> "$GITHUB_OUTPUT" - - - name: Publish the latest image tag - if: steps.prep.outputs.latest_candidate == 'true' - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - REPOSITORY: ${{ github.repository }} - RELEASE_TAG: ${{ inputs.image_version || github.ref_name }} - SOURCE_SHA: ${{ needs.test-build.outputs.tested_sha }} - IMAGE_DIGEST: ${{ steps.docker_build.outputs.digest }} - DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} - QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }} - run: | - set -euo pipefail - release_tag_pattern='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$' - if [[ ! "$RELEASE_TAG" =~ $release_tag_pattern || -z "$SOURCE_SHA" || -z "$IMAGE_DIGEST" ]]; then - echo "::error::Invalid release tag or missing source SHA/image digest." - exit 1 - fi - - API="repos/$REPOSITORY" - # Different release versions may publish concurrently. Re-check master - # and publication history immediately before updating latest; a narrow - # race between those checks and the registry write remains possible. - master_sha=$(gh api "$API/git/ref/heads/master" --jq '.object.sha') - if [[ "$SOURCE_SHA" != "$master_sha" ]]; then - echo "::notice::Release source is no longer current master; leaving latest unchanged." - exit 0 - fi - - published_versions=$(bash .workflow-tools/.github/scripts/list-published-release-versions.sh "$API") - highest_prior=$(printf '%s\n' "$published_versions" | sort -V | tail -n1) - if [[ -n "$highest_prior" ]]; then - lowest=$(printf '%s\n%s\n' "$RELEASE_TAG" "$highest_prior" | sort -V | head -n1) - if [[ "$RELEASE_TAG" != "$highest_prior" && "$lowest" == "$RELEASE_TAG" ]]; then - echo "::notice::Release $RELEASE_TAG is older than $highest_prior; leaving latest unchanged." - exit 0 - fi - fi - - USER="$GITHUB_REPOSITORY_OWNER" - IMAGE_NAME="${REPOSITORY##*/}" - IMAGE_NAME="${IMAGE_NAME#docker-}" - DOCKER_IMAGE="$USER/$IMAGE_NAME" - if [[ -n "$DOCKERHUB_USERNAME" && -n "$DOCKERHUB_TOKEN" ]]; then - docker buildx imagetools create --tag "$DOCKER_IMAGE:latest" "$DOCKER_IMAGE@$IMAGE_DIGEST" - fi - if [[ -n "$GITHUB_TOKEN" ]]; then - docker buildx imagetools create --tag "ghcr.io/$DOCKER_IMAGE:latest" "ghcr.io/$DOCKER_IMAGE@$IMAGE_DIGEST" - fi - if [[ -n "$QUAY_USERNAME" && -n "$QUAY_TOKEN" ]]; then - docker buildx imagetools create --tag "quay.io/$DOCKER_IMAGE:latest" "quay.io/$DOCKER_IMAGE@$IMAGE_DIGEST" - fi - - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} - - if [[ ! "$RELEASE_TAG" =~ $tag_pattern || -z "$SOURCE_REF_INPUT" ]]; then - echo "::error::Tag creation requires a valid release tag and explicit source_ref." - exit 1 - fi - SOURCE_SHA=$(git rev-parse HEAD) - if [[ "$SOURCE_SHA" != "$SOURCE_REF_INPUT" ]]; then - echo "::error::Checked-out source does not match source_ref." - exit 1 - fi - relation=$(gh api "repos/$REPOSITORY/compare/$SOURCE_SHA...master" --jq '.status') - if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then - echo "::error::Release source is not in master history." - exit 1 - fi - - tag_file=$(mktemp) - tag_error=$(mktemp) - if gh api "repos/$REPOSITORY/git/ref/tags/$RELEASE_TAG" >"$tag_file" 2>"$tag_error"; then - tag_ref_sha=$(jq -r '.object.sha' < "$tag_file") - tag_type=$(jq -r '.object.type' < "$tag_file") - tag_commit_sha=$tag_ref_sha - while [[ "$tag_type" == "tag" ]]; do - tag_object=$(gh api "repos/$REPOSITORY/git/tags/$tag_commit_sha") - tag_type=$(jq -r '.object.type' <<< "$tag_object") - tag_commit_sha=$(jq -r '.object.sha' <<< "$tag_object") - done - if [[ "$tag_type" != "commit" ]]; then - echo "::error::Existing release tag does not resolve to a commit." - exit 1 - fi - if [[ "$REPLACEMENT_INPUT" == "true" ]]; then - if [[ -z "$EXPECTED_TAG_REF_SHA" || "$tag_ref_sha" != "$EXPECTED_TAG_REF_SHA" ]]; then - echo "::error::Release tag changed since replacement was approved." - exit 1 - fi - if [[ "$tag_commit_sha" != "$SOURCE_SHA" ]]; then - auth=$(printf 'x-access-token:%s' "$TAG_PUSH_TOKEN" | base64 -w0) - git -c "http.extraheader=AUTHORIZATION: basic $auth" \\ - push --force-with-lease="refs/tags/$RELEASE_TAG:$EXPECTED_TAG_REF_SHA" \\ - origin "$SOURCE_SHA:refs/tags/$RELEASE_TAG" - echo "Moved $RELEASE_TAG to tested commit $SOURCE_SHA." - else - echo "Release tag already points to tested commit $SOURCE_SHA." - fi - elif [[ "$tag_commit_sha" == "$SOURCE_SHA" ]]; then - echo "Release tag already points to tested commit $SOURCE_SHA." - else - echo "::error::Release tag already exists at a different commit." - exit 1 - fi - else - if [[ "$REPLACEMENT_INPUT" == "true" ]]; then - echo "::error::Cannot replace a tag that does not exist." - exit 1 - fi - if ! grep -Eqi 'HTTP 404|status.?404' "$tag_error"; then - cat "$tag_error" - echo "::error::Could not safely determine whether the release tag exists." - exit 1 - fi - if ! GH_TOKEN="$TAG_PUSH_TOKEN" gh api --method POST "repos/$REPOSITORY/git/refs" \\ - -f "ref=refs/tags/$RELEASE_TAG" -f "sha=$SOURCE_SHA"; then - echo "::error::Could not create the release tag." - exit 1 - fi - echo "Created $RELEASE_TAG at tested commit $SOURCE_SHA." - fi - - name: Prepare image metadata id: metadata env: @@ -1412,9 +501,6 @@ jobs: sarif_file: "sarif-reports" release-docker: name: Release Docker images - permissions: - contents: write - packages: write needs: - test-build - test From ae52b596e0c52130df100309e65322a6ac440659 Mon Sep 17 00:00:00 2001 From: waja Date: Mon, 16 Nov 2026 00:04:10 +0100 Subject: [PATCH 4/5] Restore development workflow after branching release pipeline changes --- .github/workflows/release.yml | 102 +++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 370e65e..b778895 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -424,6 +424,104 @@ jobs: bash .github/scripts/check-release-image-history.sh \ "repos/$REPOSITORY" "$RELEASE_TAG" "$RELEASE_SHA" "$ALLOW_TAG_REPLACEMENT" + - name: Start or reuse tests for the resulting merge commit + id: test_dispatch + env: + GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} + REPOSITORY: ${{ github.repository }} + RELEASE_TAG: ${{ inputs.tag }} + RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} + run: | + set -euo pipefail + API="repos/$REPOSITORY" + DEFAULT_BRANCH=$(gh api "$API" --jq '.default_branch') + TEST_ID="release-test-$RELEASE_TAG-$RELEASE_SHA" + TEST_TITLE="Docker Image CI test-$TEST_ID" + + run_lines=$(gh api --paginate --method GET \ + "$API/actions/workflows/docker.image.yml/runs" \ + -f event=workflow_dispatch -F per_page=100 \ + --jq '.workflow_runs[] | {id,event,display_title,status,conclusion,created_at,html_url}') + existing=$(jq -s -c --arg title "$TEST_TITLE" ' + [.[] | select(.display_title == $title)] + | sort_by(.created_at) | last // empty + ' <<< "$run_lines") + + if [[ -n "$existing" ]]; then + status=$(jq -r '.status' <<< "$existing") + conclusion=$(jq -r '.conclusion // empty' <<< "$existing") + run_id=$(jq -r '.id' <<< "$existing") + if [[ "$status" != "completed" || "$conclusion" == "success" ]]; then + echo "run_id=$run_id" >> "$GITHUB_OUTPUT" + echo "Reusing merge-commit test run $run_id ($status)." + exit 0 + fi + echo "The prior test run failed; starting a fresh test run." + fi + + dispatched_at=$(date -u +'%Y-%m-%dT%H:%M:%SZ') + gh workflow run docker.image.yml --repo "$REPOSITORY" \ + --ref "$DEFAULT_BRANCH" \ + -f publish=false -f source_ref="$RELEASE_SHA" \ + -f test_id="$TEST_ID" -f image_version="$RELEASE_TAG" + echo "dispatched_at=$dispatched_at" >> "$GITHUB_OUTPUT" + echo "Started tests for merge commit $RELEASE_SHA." + + - name: Wait for tests on the resulting merge commit + env: + GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} + REPOSITORY: ${{ github.repository }} + RELEASE_TAG: ${{ inputs.tag }} + RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} + RUN_ID: ${{ steps.test_dispatch.outputs.run_id }} + DISPLAY_TITLE: Docker Image CI test-release-test-${{ inputs.tag }}-${{ steps.release_commit.outputs.sha }} + DISPATCHED_AT: ${{ steps.test_dispatch.outputs.dispatched_at }} + run: | + set -euo pipefail + API="repos/$REPOSITORY" + bash .github/scripts/wait-for-workflow-run.sh \ + "$API" "$RUN_ID" "$DISPLAY_TITLE" "$DISPATCHED_AT" 5400 + + - name: Create or replace tag only after merge-commit tests pass + if: steps.validate.outputs.mode != 'tagged' + env: + GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} + TAG_PUSH_TOKEN: ${{ github.token }} + REPOSITORY: ${{ github.repository }} + RELEASE_TAG: ${{ inputs.tag }} + RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} + run: | + set -euo pipefail + API="repos/$REPOSITORY" + relation=$(gh api "$API/compare/$RELEASE_SHA...master" --jq '.status') + if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then + echo "::error::Release commit is no longer in master history." + exit 1 + fi + + if [[ "${{ steps.validate.outputs.mode }}" == "replace_tagged" ]]; then + expected_ref_sha="${{ steps.validate.outputs.tag_ref_sha }}" + # GitHub's ref PATCH endpoint has no compare-and-swap condition. + # A force-with-lease push atomically rejects a stale expected ref. + # Use GITHUB_TOKEN for the tag mutation so this workflow's + # own replacement does not trigger a competing tag-push publisher. + git -c "http.extraheader=AUTHORIZATION: basic $(printf 'x-access-token:%s' "$TAG_PUSH_TOKEN" | base64 -w0)" \ + fetch --no-tags origin master + git -c "http.extraheader=AUTHORIZATION: basic $(printf 'x-access-token:%s' "$TAG_PUSH_TOKEN" | base64 -w0)" \ + push --force-with-lease="refs/tags/$RELEASE_TAG:$expected_ref_sha" \ + origin "$RELEASE_SHA:refs/tags/$RELEASE_TAG" + echo "Moved $RELEASE_TAG to tested commit $RELEASE_SHA." + else + # The tag was absent during validation. POST cannot overwrite it. + if ! GH_TOKEN="$TAG_PUSH_TOKEN" gh api --method POST "$API/git/refs" \ + -f "ref=refs/tags/$RELEASE_TAG" -f "sha=$RELEASE_SHA"; then + echo "::error::Could not create the release tag." + echo "The tag may have been created concurrently; retry to resume." + exit 1 + fi + echo "Created $RELEASE_TAG at tested commit $RELEASE_SHA." + fi + - name: Start or resume Docker image publication id: docker_dispatch env: @@ -491,9 +589,7 @@ jobs: -f publish=true -f source_ref="$RELEASE_SHA" \ -f test_id="$PUBLISH_TEST_ID" \ -f image_version="$RELEASE_TAG" \ - -f replacement="$replacement_effective" \\ - -f create_release_tag="$([[ "$RELEASE_MODE" == "tagged" ]] && echo false || echo true)" \\ - -f expected_tag_ref_sha="${{ steps.validate.outputs.tag_ref_sha }}" + -f replacement="$replacement_effective" echo "dispatched_at=$dispatched_at" >> "$GITHUB_OUTPUT" echo "Started Docker image publication for $RELEASE_TAG." From 949ccde0b02ceca357333419b92064a2b065ab8f Mon Sep 17 00:00:00 2001 From: waja Date: Wed, 18 Nov 2026 06:56:19 +0100 Subject: [PATCH 5/5] Unify release validation and publication workflow --- .github/workflows/docker.image.yml | 225 +++++++++++++++++++++++++---- .github/workflows/release.yml | 166 +++++++++------------ 2 files changed, 267 insertions(+), 124 deletions(-) diff --git a/.github/workflows/docker.image.yml b/.github/workflows/docker.image.yml index 3970d3d..ff54153 100644 --- a/.github/workflows/docker.image.yml +++ b/.github/workflows/docker.image.yml @@ -33,6 +33,16 @@ on: required: false type: boolean default: false + create_release_tag: + description: "Create or move the release tag after all validation jobs pass" + required: false + type: boolean + default: false + expected_tag_ref_sha: + description: "Expected existing tag ref SHA for an atomic replacement" + required: false + type: string + default: "" push: branches: - $default-branch @@ -501,6 +511,10 @@ jobs: sarif_file: "sarif-reports" release-docker: name: Release Docker images + permissions: + actions: read + contents: write + packages: write needs: - test-build - test @@ -576,6 +590,7 @@ jobs: EVENT_NAME_INPUT: ${{ github.event_name }} EVENT_REF_INPUT: ${{ github.ref }} EVENT_BEFORE_INPUT: ${{ github.event.before }} + CREATE_RELEASE_TAG_INPUT: ${{ inputs.create_release_tag }} QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }} run: | @@ -665,17 +680,21 @@ jobs: fi source_sha=$(git rev-parse HEAD) - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") - tag_type=$(jq -r '.object.type' <<< "$tag_json") - tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - while [[ "$tag_type" == "tag" ]]; do - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") + # When the release job creates or replaces the tag later, its + # current target is intentionally not required to match source yet. + if [[ "$CREATE_RELEASE_TAG_INPUT" != "true" ]]; then + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") tag_type=$(jq -r '.object.type' <<< "$tag_json") tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - done - if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then - echo "::error::Replacement tag $VERSION must point to tested source $source_sha." - exit 1 + while [[ "$tag_type" == "tag" ]]; do + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + done + if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then + echo "::error::Replacement tag $VERSION must point to tested source $source_sha." + exit 1 + fi fi relation=$(gh api "repos/$GITHUB_REPOSITORY/compare/$source_sha...master" --jq '.status') @@ -726,23 +745,26 @@ jobs: fi if [[ "$EVENT_NAME_INPUT" == "workflow_dispatch" ]]; then - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") - tag_type=$(jq -r '.object.type' <<< "$tag_json") - tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - while [[ "$tag_type" == "tag" ]]; do - tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") - tag_type=$(jq -r '.object.type' <<< "$tag_json") - tag_sha=$(jq -r '.object.sha' <<< "$tag_json") - done - if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then - echo "::error::Release tag $VERSION does not resolve to tested source $source_sha." - exit 1 - fi + if [[ "$CREATE_RELEASE_TAG_INPUT" != "true" ]]; then + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$VERSION") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + while [[ "$tag_type" == "tag" ]]; do + tag_json=$(gh api "repos/$GITHUB_REPOSITORY/git/tags/$tag_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + done + if [[ "$tag_type" != "commit" || "$tag_sha" != "$source_sha" ]]; then + echo "::error::Release tag $VERSION does not resolve to tested source $source_sha." + exit 1 + fi + + relation=$(gh api "repos/$GITHUB_REPOSITORY/compare/$source_sha...master" --jq '.status') + if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then + echo "::error::Release source is not in master history." + exit 1 + fi - relation=$(gh api "repos/$GITHUB_REPOSITORY/compare/$source_sha...master" --jq '.status') - if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then - echo "::error::Release source is not in master history." - exit 1 fi bash .workflow-tools/.github/scripts/check-release-image-history.sh \ @@ -832,6 +854,159 @@ jobs: password: ${{ env.QUAY_TOKEN }} # Keep this step name stable: release-history scripts use it to detect publication attempts. + - name: Create or verify release tag after all validation jobs pass + if: ${{ github.event_name == 'workflow_dispatch' && inputs.publish && inputs.create_release_tag }} + env: + GH_TOKEN: ${{ github.token }} + TAG_PUSH_TOKEN: ${{ github.token }} + RELEASE_TAG: ${{ inputs.image_version }} + SOURCE_REF_INPUT: ${{ inputs.source_ref }} + REPLACEMENT_INPUT: ${{ inputs.replacement }} + EXPECTED_TAG_REF_SHA: ${{ inputs.expected_tag_ref_sha }} + REPOSITORY: ${{ github.repository }} + TEST_ID_INPUT: ${{ inputs.test_id }} + CURRENT_RUN_ID: ${{ github.run_id }} + run: | + set -euo pipefail + tag_pattern='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$' + if [[ ! "$RELEASE_TAG" =~ $tag_pattern || -z "$SOURCE_REF_INPUT" ]]; then + echo "::error::Tag creation requires a valid release tag and explicit source_ref." + exit 1 + fi + SOURCE_SHA=$(git rev-parse HEAD) + SOURCE_REF_BRANCH="$SOURCE_REF_INPUT" + if [[ "$SOURCE_REF_BRANCH" == refs/heads/* ]]; then + SOURCE_REF_BRANCH="${SOURCE_REF_BRANCH#refs/heads/}" + fi + if ! SOURCE_REF_SHA=$(git rev-parse --verify "${SOURCE_REF_INPUT}^{commit}" 2>/dev/null); then + if ! SOURCE_REF_SHA=$(git rev-parse --verify "refs/remotes/origin/${SOURCE_REF_BRANCH}^{commit}" 2>/dev/null); then + echo "::error::Could not resolve source_ref to a commit: $SOURCE_REF_INPUT" + exit 1 + fi + fi + if [[ "$SOURCE_SHA" != "$SOURCE_REF_SHA" ]]; then + echo "::error::Checked-out source does not match source_ref ($SOURCE_REF_SHA)." + exit 1 + fi + + # Validate the release publication ID before any tag mutation. The + # later Prepare step performs the same check for publication setup. + expected_test_id_prefix="release-publish-$RELEASE_TAG-$SOURCE_SHA-run-" + if [[ "$TEST_ID_INPUT" != "$expected_test_id_prefix"* || + "$TEST_ID_INPUT" == "$expected_test_id_prefix" ]]; then + echo "::error::Release publication test_id must start with $expected_test_id_prefix and include a run identifier." + exit 1 + fi + + relation=$(gh api "repos/$REPOSITORY/compare/$SOURCE_SHA...master" --jq '.status') + if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then + echo "::error::Release source is not in master history." + exit 1 + fi + + # Reject conflicting publication history before creating or moving + # the Git tag. This guard also protects direct workflow dispatches. + bash .workflow-tools/.github/scripts/check-release-image-history.sh \ + "repos/$REPOSITORY" "$RELEASE_TAG" "$SOURCE_SHA" "$REPLACEMENT_INPUT" "$CURRENT_RUN_ID" "false" + + tag_file=$(mktemp) + tag_error=$(mktemp) + if gh api "repos/$REPOSITORY/git/ref/tags/$RELEASE_TAG" >"$tag_file" 2>"$tag_error"; then + tag_ref_sha=$(jq -r '.object.sha' < "$tag_file") + tag_type=$(jq -r '.object.type' < "$tag_file") + tag_commit_sha=$tag_ref_sha + while [[ "$tag_type" == "tag" ]]; do + tag_object=$(gh api "repos/$REPOSITORY/git/tags/$tag_commit_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_object") + tag_commit_sha=$(jq -r '.object.sha' <<< "$tag_object") + done + if [[ "$tag_type" != "commit" ]]; then + echo "::error::Existing release tag does not resolve to a commit." + exit 1 + fi + if [[ "$REPLACEMENT_INPUT" == "true" ]]; then + if [[ "$tag_commit_sha" == "$SOURCE_SHA" ]]; then + # A previous attempt may have moved the tag successfully but + # failed before publication. Treat that target state as complete. + echo "Release tag already points to tested commit $SOURCE_SHA." + else + if [[ -z "$EXPECTED_TAG_REF_SHA" || "$tag_ref_sha" != "$EXPECTED_TAG_REF_SHA" ]]; then + echo "::error::Release tag changed since replacement was approved." + exit 1 + fi + auth=$(printf 'x-access-token:%s' "$TAG_PUSH_TOKEN" | base64 -w0) + git -c "http.extraheader=AUTHORIZATION: basic $auth" \ + push --force-with-lease="refs/tags/$RELEASE_TAG:$EXPECTED_TAG_REF_SHA" \ + origin "$SOURCE_SHA:refs/tags/$RELEASE_TAG" + echo "Moved $RELEASE_TAG to tested commit $SOURCE_SHA." + fi + elif [[ "$tag_commit_sha" == "$SOURCE_SHA" ]]; then + echo "Release tag already points to tested commit $SOURCE_SHA." + else + echo "::error::Release tag already exists at a different commit." + exit 1 + fi + else + if [[ "$REPLACEMENT_INPUT" == "true" ]]; then + echo "::error::Cannot replace a tag that does not exist." + exit 1 + fi + if ! grep -Eqi 'HTTP 404|status.?404' "$tag_error"; then + cat "$tag_error" + echo "::error::Could not safely determine whether the release tag exists." + exit 1 + fi + if ! GH_TOKEN="$TAG_PUSH_TOKEN" gh api --method POST "repos/$REPOSITORY/git/refs" \ + -f "ref=refs/tags/$RELEASE_TAG" -f "sha=$SOURCE_SHA"; then + echo "::error::Could not create the release tag." + exit 1 + fi + echo "Created $RELEASE_TAG at tested commit $SOURCE_SHA." + fi + + # Confirm the final ref resolves to the tested source after either + # creating or replacing the tag. + final_tag_json=$(gh api "repos/$REPOSITORY/git/ref/tags/$RELEASE_TAG") + final_tag_type=$(jq -r '.object.type' <<< "$final_tag_json") + final_tag_sha=$(jq -r '.object.sha' <<< "$final_tag_json") + while [[ "$final_tag_type" == "tag" ]]; do + final_tag_json=$(gh api "repos/$REPOSITORY/git/tags/$final_tag_sha") + final_tag_type=$(jq -r '.object.type' <<< "$final_tag_json") + final_tag_sha=$(jq -r '.object.sha' <<< "$final_tag_json") + done + if [[ "$final_tag_type" != "commit" || "$final_tag_sha" != "$SOURCE_SHA" ]]; then + echo "::error::Release tag $RELEASE_TAG does not resolve to tested source $SOURCE_SHA after mutation." + exit 1 + fi + + - name: Verify existing release tag immediately before publication + if: ${{ github.event_name == 'workflow_dispatch' && inputs.publish && !inputs.create_release_tag && inputs.image_version != '' }} + env: + GH_TOKEN: ${{ github.token }} + REPOSITORY: ${{ github.repository }} + RELEASE_TAG: ${{ inputs.image_version }} + run: | + set -euo pipefail + tag_pattern='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}$' + if [[ ! "$RELEASE_TAG" =~ $tag_pattern ]]; then + echo "Image version '$RELEASE_TAG' is not a release tag; skipping Git tag verification." + exit 0 + fi + tag_json=$(gh api "repos/$REPOSITORY/git/ref/tags/$RELEASE_TAG") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + while [[ "$tag_type" == "tag" ]]; do + tag_json=$(gh api "repos/$REPOSITORY/git/tags/$tag_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + done + tested_sha=$(git rev-parse HEAD) + if [[ "$tag_type" != "commit" || "$tag_sha" != "$tested_sha" ]]; then + echo "::error::Release tag $RELEASE_TAG does not resolve to tested source $tested_sha." + exit 1 + fi + echo "Release tag $RELEASE_TAG still points to tested source $tested_sha." + - name: Build and push id: docker_build if: ${{ github.event_name != 'pull_request' && (github.event_name != 'workflow_dispatch' || inputs.publish) && steps.prep.outputs.tags != '' }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b778895..79a6ee4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -424,108 +424,11 @@ jobs: bash .github/scripts/check-release-image-history.sh \ "repos/$REPOSITORY" "$RELEASE_TAG" "$RELEASE_SHA" "$ALLOW_TAG_REPLACEMENT" - - name: Start or reuse tests for the resulting merge commit - id: test_dispatch - env: - GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} - REPOSITORY: ${{ github.repository }} - RELEASE_TAG: ${{ inputs.tag }} - RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} - run: | - set -euo pipefail - API="repos/$REPOSITORY" - DEFAULT_BRANCH=$(gh api "$API" --jq '.default_branch') - TEST_ID="release-test-$RELEASE_TAG-$RELEASE_SHA" - TEST_TITLE="Docker Image CI test-$TEST_ID" - - run_lines=$(gh api --paginate --method GET \ - "$API/actions/workflows/docker.image.yml/runs" \ - -f event=workflow_dispatch -F per_page=100 \ - --jq '.workflow_runs[] | {id,event,display_title,status,conclusion,created_at,html_url}') - existing=$(jq -s -c --arg title "$TEST_TITLE" ' - [.[] | select(.display_title == $title)] - | sort_by(.created_at) | last // empty - ' <<< "$run_lines") - - if [[ -n "$existing" ]]; then - status=$(jq -r '.status' <<< "$existing") - conclusion=$(jq -r '.conclusion // empty' <<< "$existing") - run_id=$(jq -r '.id' <<< "$existing") - if [[ "$status" != "completed" || "$conclusion" == "success" ]]; then - echo "run_id=$run_id" >> "$GITHUB_OUTPUT" - echo "Reusing merge-commit test run $run_id ($status)." - exit 0 - fi - echo "The prior test run failed; starting a fresh test run." - fi - - dispatched_at=$(date -u +'%Y-%m-%dT%H:%M:%SZ') - gh workflow run docker.image.yml --repo "$REPOSITORY" \ - --ref "$DEFAULT_BRANCH" \ - -f publish=false -f source_ref="$RELEASE_SHA" \ - -f test_id="$TEST_ID" -f image_version="$RELEASE_TAG" - echo "dispatched_at=$dispatched_at" >> "$GITHUB_OUTPUT" - echo "Started tests for merge commit $RELEASE_SHA." - - - name: Wait for tests on the resulting merge commit - env: - GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} - REPOSITORY: ${{ github.repository }} - RELEASE_TAG: ${{ inputs.tag }} - RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} - RUN_ID: ${{ steps.test_dispatch.outputs.run_id }} - DISPLAY_TITLE: Docker Image CI test-release-test-${{ inputs.tag }}-${{ steps.release_commit.outputs.sha }} - DISPATCHED_AT: ${{ steps.test_dispatch.outputs.dispatched_at }} - run: | - set -euo pipefail - API="repos/$REPOSITORY" - bash .github/scripts/wait-for-workflow-run.sh \ - "$API" "$RUN_ID" "$DISPLAY_TITLE" "$DISPATCHED_AT" 5400 - - - name: Create or replace tag only after merge-commit tests pass - if: steps.validate.outputs.mode != 'tagged' - env: - GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} - TAG_PUSH_TOKEN: ${{ github.token }} - REPOSITORY: ${{ github.repository }} - RELEASE_TAG: ${{ inputs.tag }} - RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} - run: | - set -euo pipefail - API="repos/$REPOSITORY" - relation=$(gh api "$API/compare/$RELEASE_SHA...master" --jq '.status') - if [[ "$relation" != "ahead" && "$relation" != "identical" ]]; then - echo "::error::Release commit is no longer in master history." - exit 1 - fi - - if [[ "${{ steps.validate.outputs.mode }}" == "replace_tagged" ]]; then - expected_ref_sha="${{ steps.validate.outputs.tag_ref_sha }}" - # GitHub's ref PATCH endpoint has no compare-and-swap condition. - # A force-with-lease push atomically rejects a stale expected ref. - # Use GITHUB_TOKEN for the tag mutation so this workflow's - # own replacement does not trigger a competing tag-push publisher. - git -c "http.extraheader=AUTHORIZATION: basic $(printf 'x-access-token:%s' "$TAG_PUSH_TOKEN" | base64 -w0)" \ - fetch --no-tags origin master - git -c "http.extraheader=AUTHORIZATION: basic $(printf 'x-access-token:%s' "$TAG_PUSH_TOKEN" | base64 -w0)" \ - push --force-with-lease="refs/tags/$RELEASE_TAG:$expected_ref_sha" \ - origin "$RELEASE_SHA:refs/tags/$RELEASE_TAG" - echo "Moved $RELEASE_TAG to tested commit $RELEASE_SHA." - else - # The tag was absent during validation. POST cannot overwrite it. - if ! GH_TOKEN="$TAG_PUSH_TOKEN" gh api --method POST "$API/git/refs" \ - -f "ref=refs/tags/$RELEASE_TAG" -f "sha=$RELEASE_SHA"; then - echo "::error::Could not create the release tag." - echo "The tag may have been created concurrently; retry to resume." - exit 1 - fi - echo "Created $RELEASE_TAG at tested commit $RELEASE_SHA." - fi - - name: Start or resume Docker image publication id: docker_dispatch env: GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ github.token }} REPOSITORY: ${{ github.repository }} RELEASE_TAG: ${{ inputs.tag }} RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} @@ -576,6 +479,34 @@ jobs: conclusion=$(jq -r '.conclusion // empty' <<< "$existing") run_id=$(jq -r '.id' <<< "$existing") if [[ "$status" != "completed" || "$conclusion" == "success" ]]; then + if [[ "$status" == "completed" && "$conclusion" == "success" ]]; then + # A successful earlier publication may outlive its Git tag. + # Restore a missing tag only after confirming the publication + # history above is safe; never move an existing tag here. + tag_error=$(mktemp) + if tag_json=$(gh api "$API/git/ref/tags/$RELEASE_TAG" 2>"$tag_error"); then + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + while [[ "$tag_type" == "tag" ]]; do + tag_json=$(gh api "$API/git/tags/$tag_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + done + if [[ "$tag_type" != "commit" || "$tag_sha" != "$RELEASE_SHA" ]]; then + echo "::error::Existing release tag $RELEASE_TAG does not point to the published source." + exit 1 + fi + else + if ! grep -Eqi 'HTTP 404|status.?404' "$tag_error"; then + cat "$tag_error" + echo "::error::Could not safely verify release tag $RELEASE_TAG." + exit 1 + fi + GH_TOKEN="$GITHUB_TOKEN" gh api --method POST "$API/git/refs" \ + -f "ref=refs/tags/$RELEASE_TAG" -f "sha=$RELEASE_SHA" + echo "Restored missing release tag $RELEASE_TAG at $RELEASE_SHA." + fi + fi echo "run_id=$run_id" >> "$GITHUB_OUTPUT" echo "Reusing publication run $run_id ($status)." exit 0 @@ -589,7 +520,9 @@ jobs: -f publish=true -f source_ref="$RELEASE_SHA" \ -f test_id="$PUBLISH_TEST_ID" \ -f image_version="$RELEASE_TAG" \ - -f replacement="$replacement_effective" + -f replacement="$replacement_effective" \ + -f create_release_tag="$([[ "$RELEASE_MODE" == "tagged" ]] && echo false || echo true)" \ + -f expected_tag_ref_sha="${{ steps.validate.outputs.tag_ref_sha }}" echo "dispatched_at=$dispatched_at" >> "$GITHUB_OUTPUT" echo "Started Docker image publication for $RELEASE_TAG." @@ -608,6 +541,41 @@ jobs: bash .github/scripts/wait-for-workflow-run.sh \ "$API" "$RUN_ID" "$DISPLAY_TITLE" "$DISPATCHED_AT" 5400 + - name: Verify release tag after publication + env: + GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ github.token }} + REPOSITORY: ${{ github.repository }} + RELEASE_TAG: ${{ inputs.tag }} + RELEASE_SHA: ${{ steps.release_commit.outputs.sha }} + run: | + set -euo pipefail + API="repos/$REPOSITORY" + tag_error=$(mktemp) + if tag_json=$(gh api "$API/git/ref/tags/$RELEASE_TAG" 2>"$tag_error"); then + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + while [[ "$tag_type" == "tag" ]]; do + tag_json=$(gh api "$API/git/tags/$tag_sha") + tag_type=$(jq -r '.object.type' <<< "$tag_json") + tag_sha=$(jq -r '.object.sha' <<< "$tag_json") + done + if [[ "$tag_type" != "commit" || "$tag_sha" != "$RELEASE_SHA" ]]; then + echo "::error::Existing release tag $RELEASE_TAG does not point to the published source." + exit 1 + fi + echo "Release tag $RELEASE_TAG still points to $RELEASE_SHA." + else + if ! grep -Eqi 'HTTP 404|status.?404' "$tag_error"; then + cat "$tag_error" + echo "::error::Could not safely verify release tag $RELEASE_TAG." + exit 1 + fi + GH_TOKEN="$GITHUB_TOKEN" gh api --method POST "$API/git/refs" \ + -f "ref=refs/tags/$RELEASE_TAG" -f "sha=$RELEASE_SHA" + echo "Restored missing release tag $RELEASE_TAG at $RELEASE_SHA." + fi + - name: Open back-merge PR if master has changes env: GH_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }}