--- name: Docker Image CI run-name: >- Docker Image CI ${{ (github.event_name == 'workflow_dispatch' && inputs.publish == false && format('test-{0}', inputs.test_id || inputs.image_version || github.ref_name)) || inputs.test_id || (github.event_name == 'workflow_dispatch' && inputs.publish == true && inputs.image_version != '' && format('release-publish-{0}-{1}', inputs.image_version, github.sha)) || inputs.image_version || github.ref_name }} # checkov:skip=CKV_GHA_7: The release workflow needs test-only workflow inputs. on: workflow_dispatch: inputs: publish: description: "Publish images after tests (false runs tests only)" required: false type: boolean default: true source_ref: description: "Commit or ref to check out for release validation" required: false type: string default: "" test_id: description: "Optional unique run name used by release automation" required: false type: string default: "" image_version: description: "Override the image version for release automation" required: false type: string default: "" replacement: description: "Republish an existing release tag without moving latest" 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 - development - master tags: - "*.*.*-*" # Run tests for any PRs pull_request: schedule: - cron: "43 10 * * 4" # Serialize publication of the same image version. Different versions may # publish concurrently; the latest-version guard below minimizes stale updates. concurrency: group: docker-image-${{ github.event_name == 'workflow_dispatch' && inputs.publish == false && (inputs.test_id || github.run_id) || inputs.image_version || github.ref_name }} cancel-in-progress: false queue: max env: IMAGE_NAME: speedtest jobs: super-linter: name: super-linter strategy: fail-fast: false runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v7 with: # super-linter needs the full git history to get the # list of files that changed across commits ref: ${{ inputs.source_ref || (github.event_name == 'pull_request' && github.sha) || github.ref }} fetch-depth: 0 - name: Lint Code Base uses: super-linter/super-linter@v9.0.0 env: DEFAULT_BRANCH: master GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_ACTIONS_CONFIG_FILE: actionlint.yml VALIDATE_GITHUB_ACTIONS_ZIZMOR: false VALIDATE_TRIVY: false VALIDATE_BIOME_FORMAT: false shiftleft: name: shiftleft outputs: build_date: ${{ steps.metadata.outputs.build_date }} build_version: ${{ steps.metadata.outputs.build_version }} vcs_ref: ${{ steps.metadata.outputs.vcs_ref }} vcs_url: ${{ steps.metadata.outputs.vcs_url }} vcs_branch: ${{ steps.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.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 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: 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: actions: read 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 }} CREATE_RELEASE_TAG_INPUT: ${{ inputs.create_release_tag }} 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) # 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") 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') 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 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 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: 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) if [[ "$SOURCE_SHA" != "$SOURCE_REF_INPUT" ]]; then echo "::error::Checked-out source does not match source_ref." 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 [[ -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 # 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 != '' }} 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 }}