From 72b0defb5f93d003e99984166f883f825274baec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Sun, 26 Jan 2025 23:15:18 +0100 Subject: [PATCH] update github workflow to build multi architecture container images --- .github/workflows/docker.yml | 106 ++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 40 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 2dab86f..6cb2d3a 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,56 +1,82 @@ name: Docker build and release on: - release: - types: [published] push: - branches: [master,develop] + pull_request: jobs: - deploy_image: - name: 'Publish: DockerHub' - runs-on: ubuntu-latest - if: github.event_name == 'release' || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master' + build: + name: 'Build container image' + runs-on: ${{ matrix.platform == 'linux/arm64' && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }} + outputs: + digest_arm64: ${{ steps.export_digest.outputs.digest_arm64 }} + digest_amd64: ${{ steps.export_digest.outputs.digest_amd64 }} + strategy: + matrix: + platform: + - linux/amd64 + - linux/arm64 steps: - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 + - uses: actions/checkout@v4 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: "ghcr.io/${{ github.repository }}" + + - name: Login to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - name: Extract branch name - shell: bash - run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" - id: extract_branch - - - name: Generating release tags and labels - if: github.event_name == 'release' - id: release_meta - uses: docker/metadata-action@v4 - with: - images: jaedb/snapserver - tags: | - latest - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} - - - name: Generating edge branch labels - if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master' - id: edge_meta - uses: docker/metadata-action@v4 + - name: Build and push by digest + id: build + uses: docker/build-push-action@v6 with: - images: jaedb/snapserver - tags: ${{ steps.extract_branch.outputs.branch }} + platforms: ${{ matrix.platform }} + labels: ${{ steps.meta.outputs.labels }} + outputs: type=image,name=ghcr.io/${{ github.repository }},push-by-digest=true,name-canonical=true,push=true - - name: Login to DockerHub - uses: docker/login-action@v1 + - name: Export digest + id: export_digest + run: | + arch="$(echo -n ${{ matrix.platform }} | sed -e 's/linux\///')" + digest="${{ steps.build.outputs.digest }}" + echo "digest_${arch}=${digest#sha256:}" >> "$GITHUB_OUTPUT" + + merge: + runs-on: "ubuntu-24.04" + needs: + - build + steps: + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} + images: ghcr.io/${{ github.repository }} - - name: Build and push - uses: docker/build-push-action@v2 + - name: Login to GHCR + uses: docker/login-action@v3 with: - push: true - tags: ${{ steps.release_meta.outputs.tags || steps.edge_meta.outputs.tags }} - labels: ${{ steps.release_meta.outputs.labels || steps.edge_meta.outputs.tags }} \ No newline at end of file + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Create manifest list and push + run: | + docker buildx imagetools create \ + $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ + ${{ env.DOCKERHUB_REPO }}@sha256:${{ needs.build.outputs.digest_arm64 }} \ + ${{ env.DOCKERHUB_REPO }}@sha256:${{ needs.build.outputs.digest_amd64 }} + + - name: Inspect image + run: docker buildx imagetools inspect ${{ env.DOCKERHUB_REPO }}:${{ steps.meta.outputs.version }}