Build and Push Docker Image #10
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 Image | |
| on: | |
| workflow_dispatch: | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| build: | |
| timeout-minutes: 30 | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - platform: linux/amd64 | |
| runner: ubuntu-latest | |
| - platform: linux/arm64 | |
| runner: ubuntu-24.04-arm | |
| runs-on: ${{ matrix.runner }} | |
| permissions: | |
| contents: read | |
| packages: write | |
| outputs: | |
| digest-amd64: ${{ steps.build.outputs.digest }} | |
| digest-arm64: ${{ steps.build.outputs.digest }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Get short commit SHA | |
| id: sha | |
| run: echo "short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
| - name: Set up Bun | |
| uses: oven-sh/setup-bun@v2 | |
| with: | |
| bun-version: latest | |
| - name: Install frontend dependencies | |
| run: bun install --cwd frontend | |
| - name: Build frontend | |
| run: bun run --cwd frontend build | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract platform suffix | |
| id: platform | |
| run: | | |
| platform="${{ matrix.platform }}" | |
| echo "suffix=${platform#linux/}" >> $GITHUB_OUTPUT | |
| - name: Extract Docker metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=raw,value=${{ steps.sha.outputs.short }}-${{ steps.platform.outputs.suffix }} | |
| - name: Build and push Docker image | |
| id: build | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| target: production | |
| push: true | |
| platforms: ${{ matrix.platform }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha,scope=${{ matrix.platform }} | |
| cache-to: type=gha,mode=max,scope=${{ matrix.platform }} | |
| build-args: | | |
| PYTHON_VERSION=3.12 | |
| manifest: | |
| timeout-minutes: 15 | |
| needs: build | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Get short commit SHA | |
| id: sha | |
| run: echo "short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Create and push manifest | |
| run: | | |
| IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" | |
| IMAGE="${IMAGE,,}" # Convert to lowercase | |
| SHA="${{ steps.sha.outputs.short }}" | |
| # Create manifest for commit SHA tag | |
| docker manifest create ${IMAGE}:${SHA} \ | |
| --amend ${IMAGE}:${SHA}-amd64 \ | |
| --amend ${IMAGE}:${SHA}-arm64 | |
| docker manifest push ${IMAGE}:${SHA} | |
| # Create manifest for latest tag | |
| docker manifest create ${IMAGE}:latest \ | |
| --amend ${IMAGE}:${SHA}-amd64 \ | |
| --amend ${IMAGE}:${SHA}-arm64 | |
| docker manifest push ${IMAGE}:latest |