|
| 1 | +# Build and publish a per-PR multi-arch GHCR image for preview/testing. |
| 2 | +# |
| 3 | +# Like Deploy, this builds per-arch images (amd64 + arm64) and stitches them into |
| 4 | +# a single multi-arch manifest. Unlike Deploy (which covers every OS), this targets |
| 5 | +# one OS only: the latest AlmaLinux produced by the ci/tags_config.sh configuration |
| 6 | +# (currently almalinux 10). The published tag carries a `pr-<number>` suffix so each |
| 7 | +# PR gets its own image. When the PR is closed the image is deleted from GHCR. |
| 8 | +name: PR Docker Image |
| 9 | + |
| 10 | +run-name: PR #${{ github.event.pull_request.number }} image |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + packages: write |
| 15 | + |
| 16 | +env: |
| 17 | + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true |
| 18 | + |
| 19 | +concurrency: |
| 20 | + group: pr-docker-image-${{ github.event.pull_request.number }} |
| 21 | + cancel-in-progress: true |
| 22 | + |
| 23 | +on: |
| 24 | + pull_request: |
| 25 | + types: [ opened, synchronize, reopened, closed ] |
| 26 | + |
| 27 | +jobs: |
| 28 | + resolve: |
| 29 | + if: ${{ github.event.action != 'closed' }} |
| 30 | + name: Resolve build parameters |
| 31 | + runs-on: ubuntu-latest |
| 32 | + outputs: |
| 33 | + image: ${{ steps.cfg.outputs.image }} |
| 34 | + gemc_tag: ${{ steps.cfg.outputs.gemc_tag }} |
| 35 | + geant4_tag: ${{ steps.cfg.outputs.geant4_tag }} |
| 36 | + alma_tag: ${{ steps.cfg.outputs.alma_tag }} |
| 37 | + tag: ${{ steps.cfg.outputs.tag }} |
| 38 | + steps: |
| 39 | + - name: Checkout repository |
| 40 | + uses: actions/checkout@v6 |
| 41 | + |
| 42 | + - id: cfg |
| 43 | + name: Resolve image ref, tags and latest AlmaLinux |
| 44 | + run: | |
| 45 | + source ci/tags_config.sh |
| 46 | + image="$(build_image_ref)" |
| 47 | + gemc_tag="$(get_gemc_tags | awk '{print $1}')" |
| 48 | + geant4_tag="$(get_geant4_tags | awk '{print $1}')" |
| 49 | + alma_tag="$(get_latest_almalinux)" |
| 50 | + tag="${gemc_tag}-almalinux-${alma_tag}-pr-${{ github.event.pull_request.number }}" |
| 51 | + { |
| 52 | + echo "image=$image" |
| 53 | + echo "gemc_tag=$gemc_tag" |
| 54 | + echo "geant4_tag=$geant4_tag" |
| 55 | + echo "alma_tag=$alma_tag" |
| 56 | + echo "tag=$tag" |
| 57 | + } >> "$GITHUB_OUTPUT" |
| 58 | + echo "Will build and push ${image}:${tag} (amd64 + arm64)" |
| 59 | +
|
| 60 | + # Per-arch build jobs. Each pushes an arch-suffixed tag (…-amd64 / …-arm64) |
| 61 | + # that the manifest job stitches into the bare PR tag. |
| 62 | + build_arch: |
| 63 | + if: ${{ github.event.action != 'closed' }} |
| 64 | + name: almalinux/${{ needs.resolve.outputs.alma_tag }} ${{ matrix.arch }} |
| 65 | + needs: resolve |
| 66 | + runs-on: ${{ matrix.runner }} |
| 67 | + strategy: |
| 68 | + fail-fast: false |
| 69 | + matrix: |
| 70 | + include: |
| 71 | + - arch: amd64 |
| 72 | + runner: ubuntu-latest |
| 73 | + platform: linux/amd64 |
| 74 | + - arch: arm64 |
| 75 | + runner: ubuntu-24.04-arm |
| 76 | + platform: linux/arm64 |
| 77 | + env: |
| 78 | + DOCKER_BUILD_SUMMARY: false |
| 79 | + steps: |
| 80 | + - name: Checkout repository |
| 81 | + uses: actions/checkout@v6 |
| 82 | + with: |
| 83 | + fetch-depth: 0 |
| 84 | + |
| 85 | + - name: Free up disk space |
| 86 | + uses: ./.github/actions/free-disk-space |
| 87 | + with: |
| 88 | + prune_docker: "true" |
| 89 | + show_tree: "true" |
| 90 | + |
| 91 | + - name: Set up Buildx |
| 92 | + uses: docker/setup-buildx-action@v4 |
| 93 | + |
| 94 | + - name: Log in to GHCR |
| 95 | + uses: docker/login-action@v4 |
| 96 | + with: |
| 97 | + registry: ghcr.io |
| 98 | + username: ${{ github.actor }} |
| 99 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + |
| 101 | + - id: meta |
| 102 | + name: Docker metadata (PR arch tag) |
| 103 | + uses: docker/metadata-action@v6 |
| 104 | + with: |
| 105 | + images: ${{ needs.resolve.outputs.image }} |
| 106 | + tags: | |
| 107 | + type=raw,value=${{ needs.resolve.outputs.tag }}-${{ matrix.arch }} |
| 108 | + labels: | |
| 109 | + org.opencontainers.image.source=${{ github.repository }} |
| 110 | + org.opencontainers.image.description=GEMC PR #${{ github.event.pull_request.number }} (almalinux ${{ needs.resolve.outputs.alma_tag }}, ${{ matrix.arch }}) |
| 111 | +
|
| 112 | + - name: Generate Dockerfile |
| 113 | + run: | |
| 114 | + python3 ci/dockerfile_creator.py \ |
| 115 | + -i almalinux \ |
| 116 | + -t "${{ needs.resolve.outputs.alma_tag }}" \ |
| 117 | + --gemc-version "${{ needs.resolve.outputs.gemc_tag }}" \ |
| 118 | + --geant4-version "${{ needs.resolve.outputs.geant4_tag }}" \ |
| 119 | + --source context \ |
| 120 | + --package-arch "${{ matrix.arch }}" \ |
| 121 | + > Dockerfile.generated |
| 122 | + cat Dockerfile.generated |
| 123 | +
|
| 124 | + - name: Build & Push |
| 125 | + uses: docker/build-push-action@v7 |
| 126 | + with: |
| 127 | + pull: true |
| 128 | + no-cache: true |
| 129 | + context: . |
| 130 | + file: ./Dockerfile.generated |
| 131 | + target: final |
| 132 | + platforms: ${{ matrix.platform }} |
| 133 | + push: true |
| 134 | + tags: ${{ steps.meta.outputs.tags }} |
| 135 | + labels: ${{ steps.meta.outputs.labels }} |
| 136 | + |
| 137 | + # Stitch the per-arch tags into a single multi-arch manifest under the bare PR tag. |
| 138 | + manifest: |
| 139 | + if: ${{ github.event.action != 'closed' }} |
| 140 | + name: Multi-arch manifest |
| 141 | + needs: [ resolve, build_arch ] |
| 142 | + runs-on: ubuntu-latest |
| 143 | + steps: |
| 144 | + - name: Log in to GHCR |
| 145 | + uses: docker/login-action@v4 |
| 146 | + with: |
| 147 | + registry: ghcr.io |
| 148 | + username: ${{ github.actor }} |
| 149 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 150 | + |
| 151 | + - name: Set up Buildx |
| 152 | + uses: docker/setup-buildx-action@v4 |
| 153 | + |
| 154 | + - name: Create multi-arch manifest |
| 155 | + shell: bash |
| 156 | + run: | |
| 157 | + BASE_TAG="${{ needs.resolve.outputs.image }}:${{ needs.resolve.outputs.tag }}" |
| 158 | + AMD_TAG="${BASE_TAG}-amd64" |
| 159 | + ARM_TAG="${BASE_TAG}-arm64" |
| 160 | +
|
| 161 | + # If the arm64 build was skipped for any reason, only include amd64. |
| 162 | + if docker buildx imagetools inspect "$ARM_TAG" >/dev/null 2>&1; then |
| 163 | + docker buildx imagetools create -t "$BASE_TAG" "$AMD_TAG" "$ARM_TAG" |
| 164 | + else |
| 165 | + docker buildx imagetools create -t "$BASE_TAG" "$AMD_TAG" |
| 166 | + fi |
| 167 | +
|
| 168 | + - name: Summary |
| 169 | + if: ${{ always() }} |
| 170 | + shell: bash |
| 171 | + run: | |
| 172 | + echo "### GEMC PR image (multi-arch)" >> "$GITHUB_STEP_SUMMARY" |
| 173 | + echo '```' >> "$GITHUB_STEP_SUMMARY" |
| 174 | + echo "${{ needs.resolve.outputs.image }}:${{ needs.resolve.outputs.tag }}" >> "$GITHUB_STEP_SUMMARY" |
| 175 | + echo '```' >> "$GITHUB_STEP_SUMMARY" |
| 176 | +
|
| 177 | + cleanup: |
| 178 | + if: ${{ github.event.action == 'closed' }} |
| 179 | + name: Delete PR image |
| 180 | + runs-on: ubuntu-latest |
| 181 | + steps: |
| 182 | + - name: Checkout repository |
| 183 | + uses: actions/checkout@v6 |
| 184 | + |
| 185 | + - name: Delete pr-<number> images from GHCR |
| 186 | + env: |
| 187 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 188 | + OWNER: ${{ github.repository_owner }} |
| 189 | + PR: ${{ github.event.pull_request.number }} |
| 190 | + run: | |
| 191 | + source ci/tags_config.sh |
| 192 | + package="$(lc "${GITHUB_REPOSITORY##*/}")" |
| 193 | + # This workflow publishes the bare "…-pr-<number>" manifest plus the |
| 194 | + # "…-pr-<number>-amd64" / "…-pr-<number>-arm64" per-arch tags. Match all |
| 195 | + # three, anchored so PR 12 does not also match PR 123. |
| 196 | + pr_regex="-pr-${PR}(-amd64|-arm64)?\$" |
| 197 | + echo "Searching ${OWNER}/${package} for versions tagged /${pr_regex}/" |
| 198 | +
|
| 199 | + # gemc is an organisation, so use the orgs packages endpoint. |
| 200 | + ids="$(gh api --paginate \ |
| 201 | + "/orgs/${OWNER}/packages/container/${package}/versions" \ |
| 202 | + --jq "[.[] | select(any(.metadata.container.tags[]?; test(\"${pr_regex}\"))) | .id] | .[]" \ |
| 203 | + 2>/dev/null || true)" |
| 204 | +
|
| 205 | + if [[ -z "$ids" ]]; then |
| 206 | + echo "No matching package versions found; nothing to delete." |
| 207 | + exit 0 |
| 208 | + fi |
| 209 | +
|
| 210 | + for id in $ids; do |
| 211 | + echo "Deleting version ${id}" |
| 212 | + gh api -X DELETE "/orgs/${OWNER}/packages/container/${package}/versions/${id}" |
| 213 | + done |
0 commit comments