diff --git a/.github/actions/build-app/action.yml b/.github/actions/build-app/action.yml index 3452866..4891ee0 100644 --- a/.github/actions/build-app/action.yml +++ b/.github/actions/build-app/action.yml @@ -1,46 +1,94 @@ name: build-app -description: Build and Push Docker Image +description: Builds one image for a single platform and pushes it by digest. Tags are applied later, to the merged manifest, by `merge-image`. inputs: dockerfile: required: true description: Dockerfile path + image: + required: true + description: Image name (e.g. `repository-server`) + platform: + required: true + description: Target platform (e.g. `linux/amd64`). Should match the runner's native architecture so no QEMU emulation is needed. push: required: true - description: Push image to registry - image: + description: If true, pushes the untagged per-platform image and uploads its digest as an artifact + registry: required: true - description: Image name - platforms: + description: Container registry to push the image to + registry-username: required: true - description: Image platforms - tag-prefix: + description: Username to log in to the container registry + registry-password: required: true - description: Tag prefix (e.g. 'rn' for 'rn-123-a1') + description: Password to log in to the container registry runs: using: composite - + steps: - - name: Extract metadata (tags, labels) for Docker + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + + - name: Log in to Container Registry + uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0 + with: + registry: ${{ inputs.registry }} + username: ${{ inputs.registry-username }} + password: ${{ inputs.registry-password }} + + # Image references must be lowercase, but the owner is `OpenShock`. + - name: Resolve image name + id: image + shell: bash + env: + IMAGE: ${{ inputs.registry }}/${{ github.repository_owner }}/${{ inputs.image }} + run: echo "name=${IMAGE,,}" >> "$GITHUB_OUTPUT" + + # Only labels are used here; tags go on the multi-arch manifest in `merge-image`. + - name: Extract metadata (labels) for Docker id: meta uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0 with: - images: ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ inputs.image }} - flavor: | - latest=false - tags: | - type=raw,value=${{ inputs.tag-prefix }}-${{ github.run_number }}-a${{ github.run_attempt }} - - - name: Build and push - uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0 + images: ${{ steps.image.outputs.name }} + + - name: Build and push Docker image by digest + id: build + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 with: context: . file: ${{ inputs.dockerfile }} - push: ${{ inputs.push }} - tags: ${{ steps.meta.outputs.tags }} + platforms: ${{ inputs.platform }} labels: ${{ steps.meta.outputs.labels }} - platforms: ${{ inputs.platforms }} + outputs: type=image,name=${{ steps.image.outputs.name }},push-by-digest=true,name-canonical=true,push=${{ inputs.push }} + # Scoped per image *and* platform: several build jobs can share this cache, and an unscoped + # `type=gha` gives them all one scope, where each one evicts the others' layers. cache-from: | - type=gha + type=gha,scope=${{ inputs.image }}-${{ inputs.platform }} cache-to: | - type=gha \ No newline at end of file + type=gha,mode=max,scope=${{ inputs.image }}-${{ inputs.platform }} + + # The merge job needs to know which digests belong to this image, so the image name is part of + # the artifact name and not just the platform. + - name: Export digest + id: digest + if: inputs.push == 'true' + shell: bash + env: + DIGEST: ${{ steps.build.outputs.digest }} + IMAGE: ${{ inputs.image }} + PLATFORM: ${{ inputs.platform }} + run: | + set -euo pipefail + mkdir -p "$RUNNER_TEMP/digests" + touch "$RUNNER_TEMP/digests/${DIGEST#sha256:}" + echo "artifact-name=digests-${IMAGE}-${PLATFORM//\//-}" >> "$GITHUB_OUTPUT" + + - name: Upload digest + if: inputs.push == 'true' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: ${{ steps.digest.outputs.artifact-name }} + path: ${{ runner.temp }}/digests/* + if-no-files-found: error + retention-days: 1 diff --git a/.github/actions/merge-image/action.yml b/.github/actions/merge-image/action.yml new file mode 100644 index 0000000..cd0959d --- /dev/null +++ b/.github/actions/merge-image/action.yml @@ -0,0 +1,106 @@ +name: merge-image +description: Combines the per-platform images pushed by `build-app` into a single multi-arch manifest and tags it. There is no per-build tag; deploys pin the manifest digest output here. +inputs: + registry: + required: true + description: Container registry to push the manifest to + image: + required: true + description: Image name (e.g. `repository-server`) + registry-username: + required: true + description: Username to log in to the container registry + registry-password: + required: true + description: Password to log in to the container registry + latest: + required: false + default: 'false' + description: If true, also tags the image as `latest` (set when the pushed tag is the newest release) +outputs: + digest: + description: Digest of the multi-arch manifest (`sha256:...`) + value: ${{ steps.digest.outputs.digest }} + +runs: + using: composite + + steps: + # Only this image's digests: `build-app` names its artifacts digests--. + - name: Download digests + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + path: ${{ runner.temp }}/digests/${{ inputs.image }} + pattern: digests-${{ inputs.image }}-* + merge-multiple: true + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + + - name: Log in to Container Registry + uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0 + with: + registry: ${{ inputs.registry }} + username: ${{ inputs.registry-username }} + password: ${{ inputs.registry-password }} + + # Image references must be lowercase, but the owner is `OpenShock`. + - name: Resolve image name + id: image + shell: bash + env: + IMAGE: ${{ inputs.registry }}/${{ github.repository_owner }}/${{ inputs.image }} + run: echo "name=${IMAGE,,}" >> "$GITHUB_OUTPUT" + + - name: Extract metadata (tags, annotations) for Docker + id: meta + uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0 + env: + # Annotate the manifest list itself so GHCR shows description/source for the multi-arch image. + DOCKER_METADATA_ANNOTATIONS_LEVELS: index + with: + images: ${{ steps.image.outputs.name }} + flavor: | + latest=false + tags: | + type=raw,value={{branch}},enable=${{ github.ref_type == 'branch' && github.event_name != 'pull_request' }} + type=raw,value=latest,enable=${{ inputs.latest == 'true' }} + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') && !startsWith(github.ref, 'refs/tags/0.') }} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}}.{{minor}}.{{patch}} + + - name: Create and push multi-arch manifest + shell: bash + working-directory: ${{ runner.temp }}/digests/${{ inputs.image }} + env: + IMAGE: ${{ steps.image.outputs.name }} + META_JSON: ${{ steps.meta.outputs.json }} + run: | + set -euo pipefail + args=() + while IFS= read -r tag; do args+=(-t "$tag"); done < <(jq -r '.tags[]' <<<"$META_JSON") + while IFS= read -r annotation; do args+=(--annotation "$annotation"); done < <(jq -r '.annotations[]?' <<<"$META_JSON") + for digest in *; do args+=("$IMAGE@sha256:$digest"); done + docker buildx imagetools create "${args[@]}" + + - name: Inspect image + shell: bash + env: + IMAGE: ${{ steps.image.outputs.name }} + VERSION: ${{ steps.meta.outputs.version }} + run: docker buildx imagetools inspect "$IMAGE:$VERSION" + + # Every tag above is mutable (branch, semver, latest), so deploys reference the digest instead. + - name: Resolve manifest digest + id: digest + shell: bash + env: + IMAGE: ${{ steps.image.outputs.name }} + VERSION: ${{ steps.meta.outputs.version }} + run: | + set -euo pipefail + digest=$(docker buildx imagetools inspect "$IMAGE:$VERSION" --format '{{json .Manifest}}' | jq -r '.digest') + [[ "$digest" =~ ^sha256:[0-9a-f]{64}$ ]] || { echo "Unexpected digest: $digest" >&2; exit 1; } + echo "digest=$digest" >> "$GITHUB_OUTPUT" diff --git a/.github/actions/promote-image/action.yml b/.github/actions/promote-image/action.yml deleted file mode 100644 index 11c2f05..0000000 --- a/.github/actions/promote-image/action.yml +++ /dev/null @@ -1,52 +0,0 @@ -inputs: - image: - required: true - description: Image name - run-tag: - required: true - description: Tag for this run - latest: - required: false - description: Tag image as latest - default: false - -runs: - using: composite - - steps: - - - name: Extract metadata (tags, labels) for Promotion - id: remeta - uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0 - with: - images: ${{ inputs.image }} - flavor: | - latest=false - tags: | - type=raw,value={{branch}},enable=${{ github.ref_type == 'branch' && github.event_name != 'pull_request' }} - type=raw,value=latest,enable=${{ inputs.latest }} - type=ref,event=branch - type=ref,event=pr - type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }} - type=semver,pattern={{major}}.{{minor}} - type=semver,pattern={{major}}.{{minor}}.{{patch}} - - # Point those tags at the built digest without rebuilding - - name: Promote by retagging digest (no rebuild) - shell: bash - run: | - set -euo pipefail - - # lower-case the repo right here - IMG='${{ inputs.image }}' # e.g. ghcr.io/OpenShock/api - IMG_LC="${IMG,,}" # bash lowercase; POSIX alt below - - # metadata-action outputs newline-separated tags - mapfile -t TAGS <<< "${{ steps.remeta.outputs.tags }}" - ARGS=() - for t in "${TAGS[@]}"; do - [[ -n "$t" ]] && ARGS+=( --tag "$t" ) - done - - # Create/overwrite manifest tags pointing at the original digest - docker buildx imagetools create "${ARGS[@]}" "${IMG_LC}:${{ inputs.run-tag }}" \ No newline at end of file diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index fefc966..e7959e4 100644 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -7,6 +7,17 @@ on: - '**' types: [ opened, reopened, synchronize ] workflow_call: + inputs: + latest: + description: 'Tag image as latest' + required: false + type: boolean + default: false + push: + description: 'Push image to registry (makes it push if it usually wouldnt)' + required: false + type: boolean + default: false workflow_dispatch: name: ci-build @@ -40,71 +51,74 @@ jobs: set -euo pipefail dotnet test -c Release --project ${{ matrix.project }} + # Each image builds on a runner matching its own architecture (no QEMU) and pushes an untagged + # image by digest. `merge` then stitches each image's digests into one tagged manifest. build: - name: Build (${{ matrix.image }}) - runs-on: ubuntu-latest + name: Build (${{ matrix.image.name }} / ${{ matrix.platform.name }}) + runs-on: ${{ matrix.platform.runner }} + permissions: + contents: read + packages: write strategy: fail-fast: false matrix: - include: - - dockerfile: docker/RepositoryServer.Dockerfile - image: repository-server + image: + - name: repository-server + dockerfile: docker/RepositoryServer.Dockerfile + platform: + - name: linux/amd64 + runner: ubuntu-latest + env: + PUSH_IMAGE: ${{ inputs.push || (github.ref_protected && github.event_name != 'pull_request') }} + outputs: + push-image: ${{ env.PUSH_IMAGE }} steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 - - - name: Log in to Container Registry - uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0 + - name: Build ${{ matrix.image.name }} + uses: ./.github/actions/build-app with: + dockerfile: ${{ matrix.image.dockerfile }} + image: ${{ matrix.image.name }} + platform: ${{ matrix.platform.name }} + push: ${{ env.PUSH_IMAGE }} registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} + registry-username: ${{ github.actor }} + registry-password: ${{ secrets.GITHUB_TOKEN }} - - name: Build ${{ matrix.image }} - uses: ./.github/actions/build-app - with: - dockerfile: ${{ matrix.dockerfile }} - push: ${{ inputs.push || (github.ref_protected && github.event_name != 'pull_request') }} - image: ${{ matrix.image }} - platforms: ${{ inputs.platforms || 'linux/amd64' }} - tag-prefix: ${{ inputs.tag-prefix || 'rn' }} - - promote-image: - name: Promote Image (${{ matrix.image }}) + # Not a matrix: matrix jobs can't expose a per-entry output, and deploys need this image's digest. + merge: + name: Merge (repository-server) needs: [build, tests] runs-on: ubuntu-latest - if: ${{ inputs.push || (github.ref_protected && github.event_name != 'pull_request') }} - strategy: - fail-fast: false - matrix: - include: - - image: repository-server - + if: needs.build.outputs.push-image == 'true' + permissions: + contents: read + packages: write + outputs: + digest: ${{ steps.merge.outputs.digest }} + steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - name: Log in to Container Registry - uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0 + - name: Merge repository-server + id: merge + uses: ./.github/actions/merge-image with: registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Promote Manifest ${{ matrix.image }} - uses: ./.github/actions/promote-image - with: - image: ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ matrix.image }} - run-tag: ${{ inputs.tag-prefix || 'rn' }}-${{ github.run_number }}-a${{ github.run_attempt }} + image: repository-server + registry-username: ${{ github.actor }} + registry-password: ${{ secrets.GITHUB_TOKEN }} latest: ${{ inputs.latest || false }} - + + # Deploys pin the manifest digest rather than a tag: no per-build tags are created, and + # re-running this job alone sends the same digest. deploy-prod-workflow: runs-on: ubuntu-latest - needs: promote-image + needs: merge if: ${{ github.ref_type == 'branch' && github.event_name != 'pull_request' && github.ref_name == 'master' }} environment: production steps: @@ -114,12 +128,12 @@ jobs: repo: openshock/kubernetes-cluster-gitops event-type: update-repository-server-prod client-payload: | - {"tag": "${{ inputs.tag-prefix || 'rn' }}-${{ github.run_number }}-a${{ github.run_attempt }}"} + {"digest": "${{ needs.merge.outputs.digest }}"} token: ${{ secrets.GITOPS_PAT }} deploy-dev: runs-on: ubuntu-latest - needs: promote-image + needs: merge if: ${{ github.ref_type == 'branch' && github.event_name != 'pull_request' && github.ref_name == 'develop' }} environment: development