|
| 1 | +# Build & Publish GitHub Container Registry (GHCR) Images |
| 2 | +name: Build Geant4 Images |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: [ main ] # publish on main updates |
| 7 | + tags: [ 'v*' ] # also publish on version tags |
| 8 | + pull_request: # PRs: build only (no push) |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ghcr-${{ github.ref }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +jobs: |
| 15 | + discover: |
| 16 | + name: Discover Dockerfiles |
| 17 | + runs-on: ubuntu-latest |
| 18 | + outputs: |
| 19 | + matrix: ${{ steps.scan.outputs.matrix }} |
| 20 | + image: ${{ steps.scan.outputs.image }} |
| 21 | + steps: |
| 22 | + - name: Checkout repository |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + # ci/distros_tags.sh must echo a JSON matrix to $GITHUB_OUTPUT: |
| 26 | + # echo "matrix=<JSON>" >> $GITHUB_OUTPUT |
| 27 | + # and the base image name (no tag) like "ghcr.io/<owner>/gemc": |
| 28 | + # echo "image=ghcr.io/<owner>/gemc" >> $GITHUB_OUTPUT |
| 29 | + - id: scan |
| 30 | + name: Build matrix |
| 31 | + run: | |
| 32 | + echo "matrix=$(ci/distros_tags.sh)" >> "$GITHUB_OUTPUT" |
| 33 | + echo "image=ghcr.io/${{ github.repository_owner }}/gemc" >> "$GITHUB_OUTPUT" |
| 34 | +
|
| 35 | + build: |
| 36 | + name: Build ${{ matrix.geant4_tag }} on ${{ matrix.docker_from }} |
| 37 | + needs: discover |
| 38 | + runs-on: ubuntu-latest |
| 39 | + permissions: |
| 40 | + contents: read |
| 41 | + packages: write |
| 42 | + strategy: |
| 43 | + fail-fast: false |
| 44 | + matrix: ${{ fromJSON(needs.discover.outputs.matrix) }} |
| 45 | + env: |
| 46 | + PLATS: linux/amd64,linux/arm64 |
| 47 | + steps: |
| 48 | + - name: Checkout repository |
| 49 | + uses: actions/checkout@v4 |
| 50 | + |
| 51 | + - name: Enable QEMU |
| 52 | + uses: docker/setup-qemu-action@v3 |
| 53 | + |
| 54 | + - name: Set up Buildx |
| 55 | + uses: docker/setup-buildx-action@v3 |
| 56 | + |
| 57 | + - name: Log in to GHCR |
| 58 | + uses: docker/login-action@v3 |
| 59 | + with: |
| 60 | + registry: ghcr.io |
| 61 | + username: ${{ github.actor }} |
| 62 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 63 | + |
| 64 | + - id: meta |
| 65 | + name: Generate tags & labels |
| 66 | + uses: docker/metadata-action@v5 |
| 67 | + with: |
| 68 | + images: ${{ needs.discover.outputs.image }} |
| 69 | + tags: | |
| 70 | + # Single canonical tag: geant4-<tag>-<base> |
| 71 | + type=raw,value=${{ matrix.geant4_tag }}-${{ matrix.docker_from }} |
| 72 | + labels: | |
| 73 | + org.opencontainers.image.source=${{ github.repository }} |
| 74 | + org.opencontainers.image.description=Geant4 image (${{ matrix.geant4_tag }} on ${{ matrix.docker_from }}) |
| 75 | +
|
| 76 | + # Generate a Dockerfile using your python script, passing the FULL base image |
| 77 | + - name: Generate Dockerfile |
| 78 | + run: | |
| 79 | + python3 ci/g4pkglist.py -p "${{ matrix.docker_from }}" --install > Dockerfile.generated |
| 80 | + echo "Generated Dockerfile:" |
| 81 | + sed -n '1,60p' Dockerfile.generated |
| 82 | +
|
| 83 | + # Multi-arch when pushing; single-arch and load locally on PRs |
| 84 | + - name: Build and Push (release/merge) |
| 85 | + if: ${{ github.event_name != 'pull_request' }} |
| 86 | + uses: docker/build-push-action@v5 |
| 87 | + with: |
| 88 | + context: . |
| 89 | + file: ./Dockerfile.generated |
| 90 | + platforms: ${{ env.PLATS }} |
| 91 | + push: true |
| 92 | + tags: ${{ steps.meta.outputs.tags }} |
| 93 | + labels: ${{ steps.meta.outputs.labels }} |
| 94 | + cache-from: type=registry,ref=${{ needs.discover.outputs.image }}:cache |
| 95 | + cache-to: type=registry,ref=${{ needs.discover.outputs.image }}:cache,mode=max |
| 96 | + |
| 97 | + |
| 98 | + - name: Summarize image reference |
| 99 | + if: ${{ always() }} |
| 100 | + env: |
| 101 | + IMAGE: ${{ needs.discover.outputs.image }} |
| 102 | + TAG: ${{ matrix.geant4_tag }}-${{ matrix.docker_from }} |
| 103 | + PUSHED: ${{ github.event_name != 'pull_request' }} |
| 104 | + run: | |
| 105 | + PLATFORM_HINT="--platform=linux/amd64" |
| 106 | + { |
| 107 | + echo "## Docker Image" |
| 108 | + if [ "$PUSHED" = "true" ]; then |
| 109 | + echo "" |
| 110 | + echo "**Tag:** \`$IMAGE:$TAG\`" |
| 111 | + echo "" |
| 112 | + echo "### Pull" |
| 113 | + echo '```bash' |
| 114 | + echo "docker pull $IMAGE:$TAG" |
| 115 | + echo '```' |
| 116 | + echo "### Run" |
| 117 | + echo '```bash' |
| 118 | + echo "docker run --rm -it $IMAGE:$TAG bash" |
| 119 | + echo '```' |
| 120 | + echo "### Run (Apple Silicon Mac)" |
| 121 | + echo '```bash' |
| 122 | + echo "docker run --rm -it $PLATFORM_HINT $IMAGE:$TAG bash" |
| 123 | + echo '```' |
| 124 | + else |
| 125 | + echo "" |
| 126 | + echo "_PR build (image not pushed). Loaded locally on the runner with tags:_" |
| 127 | + echo '```' |
| 128 | + printf '%s\n' "${{ steps.meta.outputs.tags }}" |
| 129 | + echo '```' |
| 130 | + fi |
| 131 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments