|
| 1 | +name: Docker Image Build |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + packages: write |
| 12 | + |
| 13 | +env: |
| 14 | + image_base: ghcr.io/${{ github.repository }} |
| 15 | + |
| 16 | +jobs: |
| 17 | + build-image: |
| 18 | + # Builds each image in a separate job in parallel. |
| 19 | + strategy: |
| 20 | + matrix: |
| 21 | + # The docker/build-push-action uses the git repo as the docker build context instead of |
| 22 | + # cloning the repo to the action runner disk and using the disk context. For the API image |
| 23 | + # we need to set the context to just the api/ directory which the '{{defaultContext}}:api' |
| 24 | + # syntax does: https://github.com/docker/build-push-action?tab=readme-ov-file#git-context. |
| 25 | + include: |
| 26 | + - container: api |
| 27 | + docker_context: "{{defaultContext}}:api" |
| 28 | + dockerfile: Dockerfile |
| 29 | + - container: partners |
| 30 | + docker_context: "{{defaultContext}}" |
| 31 | + dockerfile: Dockerfile.sites.partners |
| 32 | + - container: public |
| 33 | + docker_context: "{{defaultContext}}" |
| 34 | + dockerfile: Dockerfile.sites.public |
| 35 | + runs-on: ubuntu-latest |
| 36 | + steps: |
| 37 | + - name: Log in to GitHub Container Registry |
| 38 | + uses: docker/login-action@v3.6.0 |
| 39 | + with: |
| 40 | + registry: ghcr.io |
| 41 | + username: ${{ github.actor }} |
| 42 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + |
| 44 | + # Required to use image layer cache. |
| 45 | + - name: Setup Docker Buildx |
| 46 | + uses: docker/setup-buildx-action@v3.11.1 |
| 47 | + |
| 48 | + - name: "Build and push image" |
| 49 | + uses: docker/build-push-action@v6.18.0 |
| 50 | + with: |
| 51 | + push: true |
| 52 | + cache-to: type=registry,mode=max,ref=${{ env.image_base }}/${{ matrix.container }}/container-layer-cache:latest |
| 53 | + cache-from: type=registry,ref=${{ env.image_base }}/${{ matrix.container }}/container-layer-cache:latest |
| 54 | + context: ${{ matrix.docker_context }} |
| 55 | + file: ${{ matrix.dockerfile }} |
| 56 | + tags: | |
| 57 | + ${{ env.image_base }}/${{ matrix.container }}:latest |
| 58 | + ${{ env.image_base }}/${{ matrix.container }}:gitsha-${{ github.sha }} |
| 59 | + # Connects the image to the repository: https://docs.github.com/en/packages/learn-github-packages/connecting-a-repository-to-a-package. |
| 60 | + labels: org.opencontainers.image.source=https://github.com/${{ github.repository }} |
0 commit comments