From 28eddad797524aed8085f4eae6f13672da88005e Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Mon, 19 May 2025 10:15:11 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20GitHub=20Actions=20workflow?= =?UTF-8?q?=20for=20building=20and=20pushing=20multi-architecture=20Docker?= =?UTF-8?q?=20images?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/docker-build-multi.yml | 93 ++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 .github/workflows/docker-build-multi.yml diff --git a/.github/workflows/docker-build-multi.yml b/.github/workflows/docker-build-multi.yml new file mode 100644 index 0000000..042433c --- /dev/null +++ b/.github/workflows/docker-build-multi.yml @@ -0,0 +1,93 @@ +name: Build and Push Docker Images (Multi-Arch) + +on: + push: + branches: [main] + tags: ['v*'] + pull_request: + +env: + REGISTRY: ghcr.io + IMAGE: ${{ github.repository }} + +jobs: + # Build platform-specific images in parallel + build: + name: Build ${{ matrix.platform }} + runs-on: ${{ matrix.runner }} + strategy: + matrix: + include: + - platform: linux/amd64 + runner: ubuntu-latest + platform-suffix: amd64 + - platform: linux/arm64 + runner: macos-latest + platform-suffix: arm64 + + permissions: + contents: read + packages: write + + services: + redis: + image: redis:7-alpine + ports: ['6379:6379'] + + steps: + - uses: actions/checkout@v4 + + - uses: docker/setup-buildx-action@v3 + + - uses: docker/login-action@v3 + if: github.event_name != 'pull_request' + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - uses: docker/build-push-action@v5 + with: + context: . + platforms: ${{ matrix.platform }} + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ env.REGISTRY }}/${{ env.IMAGE }}:${{ matrix.platform-suffix }}-${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + + # Create and push the multi-platform manifest + manifest: + name: Create and Push Manifest + runs-on: ubuntu-latest + needs: build + if: github.event_name != 'pull_request' + + permissions: + contents: write + packages: write + + steps: + - uses: actions/checkout@v4 + + - uses: docker/setup-buildx-action@v3 + + - uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Create and push manifest + run: | + # Create and push the manifest with both architectures + docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE }}:latest \ + ${{ env.REGISTRY }}/${{ env.IMAGE }}:amd64-${{ github.sha }} \ + ${{ env.REGISTRY }}/${{ env.IMAGE }}:arm64-${{ github.sha }} + + docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE }}:${{ github.sha }} \ + ${{ env.REGISTRY }}/${{ env.IMAGE }}:amd64-${{ github.sha }} \ + ${{ env.REGISTRY }}/${{ env.IMAGE }}:arm64-${{ github.sha }} + + # Push the manifests + docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE }}:latest + docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE }}:${{ github.sha }} \ No newline at end of file