|
| 1 | +name: Staging dev relay image |
| 2 | + |
| 3 | +# Publishes pre-merge relay runtime images for bb-block staging only. |
| 4 | +# |
| 5 | +# Operators run this workflow from the default branch and provide a target ref in |
| 6 | +# this repository. GitHub's workflow_dispatch permission model limits triggering |
| 7 | +# to collaborators who can run repository workflows; no actor allowlist is kept |
| 8 | +# here. The target ref is resolved to an immutable commit SHA before checkout, |
| 9 | +# and the published tag is derived from that full SHA. |
| 10 | + |
| 11 | +on: |
| 12 | + workflow_dispatch: |
| 13 | + inputs: |
| 14 | + target_ref: |
| 15 | + description: "Branch, tag, refs/heads/*, or refs/tags/* in block/buzz to publish for bb-block staging" |
| 16 | + required: true |
| 17 | + type: string |
| 18 | + |
| 19 | +permissions: {} |
| 20 | + |
| 21 | +env: |
| 22 | + IMAGE_NAME: ghcr.io/block/buzz-staging-dev |
| 23 | + ECR_REPOSITORY: 929862310821.dkr.ecr.us-west-2.amazonaws.com/ghcr.io/block/buzz-staging-dev |
| 24 | + |
| 25 | +jobs: |
| 26 | + resolve: |
| 27 | + name: Resolve target ref |
| 28 | + runs-on: ubuntu-24.04 |
| 29 | + timeout-minutes: 5 |
| 30 | + permissions: |
| 31 | + contents: read |
| 32 | + outputs: |
| 33 | + target_sha: ${{ steps.resolve.outputs.target_sha }} |
| 34 | + image_tag: ${{ steps.resolve.outputs.image_tag }} |
| 35 | + steps: |
| 36 | + - name: Require reviewed workflow from main in canonical repository |
| 37 | + env: |
| 38 | + DISPATCH_REF: ${{ github.ref }} |
| 39 | + REPOSITORY: ${{ github.repository }} |
| 40 | + run: | |
| 41 | + set -euo pipefail |
| 42 | + if [ "$REPOSITORY" != "block/buzz" ]; then |
| 43 | + echo "::error::Staging dev relay image publication is restricted to block/buzz" |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | +
|
| 47 | + if [ "$DISPATCH_REF" != "refs/heads/main" ]; then |
| 48 | + echo "::error::Dispatch this workflow from main, not $DISPATCH_REF" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Resolve target ref in this repository |
| 53 | + id: resolve |
| 54 | + env: |
| 55 | + TARGET_REF_INPUT: ${{ inputs.target_ref }} |
| 56 | + GITHUB_REPOSITORY: ${{ github.repository }} |
| 57 | + run: | |
| 58 | + set -euo pipefail |
| 59 | +
|
| 60 | + target_ref=$(printf '%s' "$TARGET_REF_INPUT" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') |
| 61 | +
|
| 62 | + if [ -z "$target_ref" ]; then |
| 63 | + echo "target_ref must not be empty" >&2 |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | +
|
| 67 | + if [ ${#target_ref} -gt 255 ]; then |
| 68 | + echo "target_ref is too long" >&2 |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + if [[ ! "$target_ref" =~ ^[A-Za-z0-9._/@-]+$ ]]; then |
| 73 | + echo "target_ref contains unsupported characters; use a branch, tag, or full refs/heads/* or refs/tags/* name" >&2 |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +
|
| 77 | + case "$target_ref" in |
| 78 | + -*|*..*|*.lock|refs/pull/*|pull/*|*/pull/*) |
| 79 | + echo "target_ref is not an allowed repository branch/tag/ref" >&2 |
| 80 | + exit 1 |
| 81 | + ;; |
| 82 | + esac |
| 83 | +
|
| 84 | + workdir=$(mktemp -d) |
| 85 | + trap 'rm -rf "$workdir"' EXIT |
| 86 | + git -C "$workdir" init --quiet |
| 87 | + git -C "$workdir" remote add origin "https://github.com/${GITHUB_REPOSITORY}.git" |
| 88 | +
|
| 89 | + fetch_commit() { |
| 90 | + local ref=$1 |
| 91 | + git -C "$workdir" fetch --no-tags --depth=1 origin "$ref" >/dev/null 2>&1 |
| 92 | + git -C "$workdir" rev-parse --verify "FETCH_HEAD^{commit}" |
| 93 | + } |
| 94 | +
|
| 95 | + target_sha="" |
| 96 | + if [[ "$target_ref" == refs/heads/* || "$target_ref" == refs/tags/* ]]; then |
| 97 | + target_sha=$(fetch_commit "$target_ref") || { |
| 98 | + echo "target_ref did not resolve in ${GITHUB_REPOSITORY}: $target_ref" >&2 |
| 99 | + exit 1 |
| 100 | + } |
| 101 | + else |
| 102 | + if target_sha=$(fetch_commit "refs/heads/${target_ref}"); then |
| 103 | + : |
| 104 | + elif target_sha=$(fetch_commit "refs/tags/${target_ref}"); then |
| 105 | + : |
| 106 | + else |
| 107 | + echo "target_ref did not resolve as a branch or tag in ${GITHUB_REPOSITORY}: $target_ref" >&2 |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + fi |
| 111 | +
|
| 112 | + target_sha=$(printf '%s' "$target_sha" | tr '[:upper:]' '[:lower:]') |
| 113 | + if [[ ! "$target_sha" =~ ^[0-9a-f]{40}$ ]]; then |
| 114 | + echo "resolved target SHA is invalid: $target_sha" >&2 |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | +
|
| 118 | + image_tag="dev-sha-${target_sha}-run-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" |
| 119 | + { |
| 120 | + echo "target_sha=${target_sha}" |
| 121 | + echo "image_tag=${image_tag}" |
| 122 | + } >> "$GITHUB_OUTPUT" |
| 123 | +
|
| 124 | + printf "Resolved \`%s\` to \`%s\`; image tag \`%s\`.\n" "$target_ref" "$target_sha" "$image_tag" >> "$GITHUB_STEP_SUMMARY" |
| 125 | +
|
| 126 | + build: |
| 127 | + name: Build staging relay runtime (${{ matrix.platform }}) |
| 128 | + runs-on: ${{ matrix.runner }} |
| 129 | + needs: resolve |
| 130 | + timeout-minutes: 60 |
| 131 | + permissions: |
| 132 | + contents: read |
| 133 | + packages: write |
| 134 | + strategy: |
| 135 | + fail-fast: false |
| 136 | + matrix: |
| 137 | + include: |
| 138 | + - platform: linux/amd64 |
| 139 | + runner: ubuntu-24.04 |
| 140 | + arch: amd64 |
| 141 | + - platform: linux/arm64 |
| 142 | + runner: ubuntu-24.04-arm |
| 143 | + arch: arm64 |
| 144 | + steps: |
| 145 | + - name: Checkout resolved target |
| 146 | + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 |
| 147 | + with: |
| 148 | + ref: ${{ needs.resolve.outputs.target_sha }} |
| 149 | + fetch-depth: 1 |
| 150 | + persist-credentials: false |
| 151 | + |
| 152 | + - name: Set up Docker Buildx |
| 153 | + uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0 |
| 154 | + with: |
| 155 | + buildkitd-config-inline: | |
| 156 | + [worker.oci] |
| 157 | + max-parallelism = 2 |
| 158 | +
|
| 159 | + - name: Log in to GHCR |
| 160 | + uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0 |
| 161 | + with: |
| 162 | + registry: ghcr.io |
| 163 | + username: ${{ github.repository_owner }} |
| 164 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 165 | + |
| 166 | + - name: Extract metadata |
| 167 | + id: meta |
| 168 | + uses: docker/metadata-action@80c7e94dd9b9319bd5eb7a0e0fe9291e23a2a2e9 # v6.1.0 |
| 169 | + with: |
| 170 | + images: ${{ env.IMAGE_NAME }} |
| 171 | + labels: | |
| 172 | + org.opencontainers.image.title=Buzz staging dev relay |
| 173 | + org.opencontainers.image.description=Pre-merge Buzz relay runtime image for bb-block staging only |
| 174 | + org.opencontainers.image.licenses=Apache-2.0 |
| 175 | + org.opencontainers.image.revision=${{ needs.resolve.outputs.target_sha }} |
| 176 | +
|
| 177 | + - name: Build and push runtime image by digest |
| 178 | + id: build |
| 179 | + uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0 |
| 180 | + with: |
| 181 | + context: . |
| 182 | + file: ./Dockerfile |
| 183 | + target: runtime |
| 184 | + platforms: ${{ matrix.platform }} |
| 185 | + labels: ${{ steps.meta.outputs.labels }} |
| 186 | + outputs: type=image,name=${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true |
| 187 | + cache-from: | |
| 188 | + type=registry,ref=${{ env.IMAGE_NAME }}-buildcache:${{ matrix.arch }} |
| 189 | + cache-to: | |
| 190 | + type=registry,ref=${{ env.IMAGE_NAME }}-buildcache:${{ matrix.arch }},mode=max,compression=zstd |
| 191 | +
|
| 192 | + - name: Export digest |
| 193 | + env: |
| 194 | + DIGEST: ${{ steps.build.outputs.digest }} |
| 195 | + run: | |
| 196 | + set -euo pipefail |
| 197 | + mkdir -p /tmp/digests-release |
| 198 | + touch "/tmp/digests-release/${DIGEST#sha256:}" |
| 199 | +
|
| 200 | + - name: Upload digest |
| 201 | + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 |
| 202 | + with: |
| 203 | + name: staging-dev-relay-digests-${{ matrix.arch }} |
| 204 | + path: /tmp/digests-release/* |
| 205 | + if-no-files-found: error |
| 206 | + retention-days: 1 |
| 207 | + |
| 208 | + merge: |
| 209 | + name: Publish staging relay runtime manifest |
| 210 | + runs-on: ubuntu-24.04 |
| 211 | + needs: |
| 212 | + - resolve |
| 213 | + - build |
| 214 | + timeout-minutes: 15 |
| 215 | + permissions: |
| 216 | + contents: read |
| 217 | + packages: write |
| 218 | + steps: |
| 219 | + - name: Download per-arch digests |
| 220 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 221 | + with: |
| 222 | + path: /tmp/digests |
| 223 | + pattern: staging-dev-relay-digests-* |
| 224 | + merge-multiple: true |
| 225 | + |
| 226 | + - name: Set up Docker Buildx |
| 227 | + uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0 |
| 228 | + |
| 229 | + - name: Log in to GHCR |
| 230 | + uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0 |
| 231 | + with: |
| 232 | + registry: ghcr.io |
| 233 | + username: ${{ github.repository_owner }} |
| 234 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 235 | + |
| 236 | + - name: Create and push manifest list |
| 237 | + id: manifest |
| 238 | + working-directory: /tmp/digests |
| 239 | + env: |
| 240 | + IMAGE_NAME: ${{ env.IMAGE_NAME }} |
| 241 | + IMAGE_TAG: ${{ needs.resolve.outputs.image_tag }} |
| 242 | + run: | |
| 243 | + set -euo pipefail |
| 244 | + digests=() |
| 245 | + for digest in *; do |
| 246 | + digests+=("${IMAGE_NAME}@sha256:${digest}") |
| 247 | + done |
| 248 | +
|
| 249 | + docker buildx imagetools create -t "${IMAGE_NAME}:${IMAGE_TAG}" "${digests[@]}" |
| 250 | + merged_digest=$(docker buildx imagetools inspect "${IMAGE_NAME}:${IMAGE_TAG}" \ |
| 251 | + --format '{{json .Manifest}}' | jq -r '.digest') |
| 252 | + echo "digest=${merged_digest}" >> "$GITHUB_OUTPUT" |
| 253 | +
|
| 254 | + - name: Deployment summary |
| 255 | + env: |
| 256 | + IMAGE_NAME: ${{ env.IMAGE_NAME }} |
| 257 | + ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }} |
| 258 | + IMAGE_TAG: ${{ needs.resolve.outputs.image_tag }} |
| 259 | + TARGET_SHA: ${{ needs.resolve.outputs.target_sha }} |
| 260 | + MERGED_DIGEST: ${{ steps.manifest.outputs.digest }} |
| 261 | + run: | |
| 262 | + { |
| 263 | + echo "### Published bb-block staging dev relay image" |
| 264 | + echo |
| 265 | + echo "**Source commit:** \`${TARGET_SHA}\`" |
| 266 | + echo "**GHCR image:** \`${IMAGE_NAME}:${IMAGE_TAG}\`" |
| 267 | + echo "**Manifest digest:** \`${MERGED_DIGEST}\`" |
| 268 | + echo |
| 269 | + echo "Set bb-block staging BPCI values to:" |
| 270 | + echo '```yaml' |
| 271 | + echo "buzz:" |
| 272 | + echo " image:" |
| 273 | + echo " repository: ${ECR_REPOSITORY}" |
| 274 | + echo " tag: ${IMAGE_TAG}" |
| 275 | + echo '```' |
| 276 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments