|
| 1 | +# Based on https://docs.github.com/en/actions/publishing-packages/publishing-docker-images |
| 2 | + |
| 3 | +name: Publish Docker image |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_dispatch: |
| 7 | + push: |
| 8 | + branches: [ "main" ] |
| 9 | + # Publish semver tags as releases. |
| 10 | + tags: [ 'v*.*.*' ] |
| 11 | + paths: |
| 12 | + - '.github/workflows/docker-publish.yml' |
| 13 | + - 'Dockerfile' |
| 14 | + - 'Manifest.toml' |
| 15 | + - 'Project.toml' |
| 16 | + - 'julia_cpu_target.sh' |
| 17 | + pull_request: |
| 18 | + branches: [ "main" ] |
| 19 | + paths: |
| 20 | + - '.github/workflows/docker-publish.yml' |
| 21 | + - 'Dockerfile' |
| 22 | + - 'Manifest.toml' |
| 23 | + - 'Project.toml' |
| 24 | + - 'julia_cpu_target.sh' |
| 25 | + |
| 26 | +env: |
| 27 | + REGISTRY: ghcr.io |
| 28 | + IMAGE_NAME: ${{ github.repository }} |
| 29 | + |
| 30 | +concurrency: |
| 31 | + # Skip intermediate builds: always. |
| 32 | + # Cancel intermediate builds: always. |
| 33 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 34 | + cancel-in-progress: true |
| 35 | + |
| 36 | +jobs: |
| 37 | + build-and-push-image: |
| 38 | + |
| 39 | + runs-on: ubuntu-latest |
| 40 | + permissions: |
| 41 | + contents: read |
| 42 | + packages: write |
| 43 | + attestations: write |
| 44 | + # This is used to complete the identity challenge |
| 45 | + # with sigstore/fulcio when running outside of PRs. |
| 46 | + id-token: write |
| 47 | + |
| 48 | + steps: |
| 49 | + |
| 50 | + - name: Checkout repository |
| 51 | + uses: actions/checkout@v4 |
| 52 | + |
| 53 | + # Docker is terrible and doesn't like uppercase image names. |
| 54 | + - name: Lowercase image name |
| 55 | + run: | |
| 56 | + IMAGE_NAME=$(echo ${IMAGE_NAME} | tr A-Z a-z) |
| 57 | + echo "IMAGE_NAME=${IMAGE_NAME}" >> "${GITHUB_ENV}" |
| 58 | +
|
| 59 | + - name: Set up QEMU |
| 60 | + uses: docker/setup-qemu-action@v3 |
| 61 | + with: |
| 62 | + platforms: arm64 |
| 63 | + |
| 64 | + # Needed to make caching on GHA work: https://stackoverflow.com/a/73884678. |
| 65 | + # https://github.com/docker/setup-buildx-action |
| 66 | + - name: Setup Docker buildx |
| 67 | + uses: docker/setup-buildx-action@v3 |
| 68 | + |
| 69 | + # Log into the registry except on PRs. |
| 70 | + # https://github.com/docker/login-action |
| 71 | + - name: Log into registry ${{ env.REGISTRY }} |
| 72 | + if: github.event_name != 'pull_request' |
| 73 | + uses: docker/login-action@v3 |
| 74 | + with: |
| 75 | + registry: ${{ env.REGISTRY }} |
| 76 | + username: ${{ github.actor }} |
| 77 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + |
| 79 | + # Extract metadata (tags, labels) for Docker |
| 80 | + # https://github.com/docker/metadata-action |
| 81 | + - name: Extract Docker metadata |
| 82 | + id: meta |
| 83 | + uses: docker/metadata-action@v5 |
| 84 | + with: |
| 85 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 86 | + |
| 87 | + # Build and push Docker image with Buildx (don't push on PR) |
| 88 | + # https://github.com/docker/build-push-action |
| 89 | + - name: Build and push Docker image |
| 90 | + id: build-and-push |
| 91 | + uses: docker/build-push-action@v5 |
| 92 | + with: |
| 93 | + context: . |
| 94 | + push: ${{ github.event_name != 'pull_request' }} |
| 95 | + # Note: building only for `linux/amd64` takes about 5 minutes, building also for |
| 96 | + # `linux/arm64` takes about 40 minutes because QEMU is super slow. If you're |
| 97 | + # desperate about build times remove `linux/arm64`, but keep in mind the |
| 98 | + # `linux/amd64` image won't work very well on Apple Silicon. |
| 99 | + platforms: linux/amd64,linux/arm64 |
| 100 | + tags: ${{ steps.meta.outputs.tags }} |
| 101 | + labels: ${{ steps.meta.outputs.labels }} |
| 102 | + # See: https://docs.docker.com/build/ci/github-actions/cache/#github-cache |
| 103 | + cache-from: type=gha |
| 104 | + cache-to: type=gha,mode=max |
0 commit comments