Build and Push Docker Images #28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Push Docker Images | |
| on: | |
| push: | |
| branches: | |
| - master | |
| workflow_dispatch: | |
| schedule: | |
| - cron: "0 13 * * 1" | |
| env: | |
| USERNAME: "ericc-ch" | |
| REPO: "copilot-api" | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| REGISTRY_IMAGE: ghcr.io/${{ github.repository }} | |
| jobs: | |
| build: | |
| permissions: | |
| contents: read | |
| packages: write | |
| runs-on: ${{ matrix.runs-on }} | |
| outputs: | |
| latest_tag: ${{ steps.latest_tag.outputs.tag }} | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - runs-on: ubuntu-latest | |
| platform: linux/amd64 | |
| - runs-on: ubuntu-24.04-arm | |
| platform: linux/arm64 | |
| steps: | |
| - name: Checkout current repository | |
| uses: actions/checkout@v6 | |
| - name: Get latest tag | |
| id: latest_tag | |
| run: | | |
| TAG=$(curl -s https://api.github.com/repos/${{ env.USERNAME }}/${{ env.REPO }}/tags | jq -r '.[0].name') | |
| echo "tag=${TAG}" >> $GITHUB_OUTPUT | |
| echo "LATEST_TAG=${TAG}" >> $GITHUB_ENV | |
| - name: Checkout repository | |
| uses: actions/checkout@v6 | |
| with: | |
| repository: ${{ env.USERNAME }}/${{ env.REPO }} | |
| ref: ${{ env.LATEST_TAG }} | |
| path: ${{ env.REPO }} | |
| - name: Use Dockerfile in repo | |
| run: | | |
| cp Dockerfile ${{ env.REPO }}/Dockerfile | |
| - name: Prepare | |
| run: | | |
| platform=${{ matrix.platform }} | |
| echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV | |
| - name: Docker meta | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY_IMAGE }} | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Build and push by digest | |
| id: build | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: ${{ env.REPO }} | |
| platforms: ${{ matrix.platform }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| tags: ${{ env.REGISTRY_IMAGE }} | |
| outputs: type=image,push-by-digest=true,name-canonical=true,push=true | |
| - name: Export digest | |
| run: | | |
| mkdir -p ${{ runner.temp }}/digests | |
| digest="${{ steps.build.outputs.digest }}" | |
| touch "${{ runner.temp }}/digests/${digest#sha256:}" | |
| - name: Upload digest | |
| uses: actions/upload-artifact@v6 | |
| with: | |
| name: digests-${{ env.PLATFORM_PAIR }} | |
| path: ${{ runner.temp }}/digests/* | |
| if-no-files-found: error | |
| retention-days: 1 | |
| merge: | |
| permissions: | |
| contents: read | |
| packages: write | |
| runs-on: ubuntu-latest | |
| needs: | |
| - build | |
| env: | |
| LATEST_TAG: ${{ needs.build.outputs.latest_tag }} | |
| steps: | |
| - name: Download digests | |
| uses: actions/download-artifact@v7 | |
| with: | |
| path: ${{ runner.temp }}/digests | |
| pattern: digests-* | |
| merge-multiple: true | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Docker meta | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY_IMAGE }} | |
| tags: | | |
| type=raw,value=latest,enable=true | |
| type=raw,value=${{ env.LATEST_TAG }},enable=true | |
| - name: Create manifest list and push | |
| working-directory: ${{ runner.temp }}/digests | |
| run: | | |
| docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ | |
| $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) | |
| - name: Inspect image | |
| run: | | |
| docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:latest | |
| create-release: | |
| needs: [build, merge] | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| env: | |
| LATEST_TAG: ${{ needs.build.outputs.latest_tag }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v6 | |
| - name: Create and push tag | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| git tag ${{ env.LATEST_TAG }} || true | |
| git push origin ${{ env.LATEST_TAG }} || true | |
| gh release create ${{ env.LATEST_TAG }} --generate-notes || true |