| 1 | name: Build and Push Docker Image |
| 2 | |
| 3 | on: |
| 4 | push: |
| 5 | branches: |
| 6 | - master |
| 7 | tags: |
| 8 | - 'v*' |
| 9 | pull_request: |
| 10 | branches: |
| 11 | - master |
| 12 | |
| 13 | env: |
| 14 | REGISTRY: ghcr.io |
| 15 | IMAGE_NAME: ${{ github.repository }} |
| 16 | |
| 17 | jobs: |
| 18 | build-and-push: |
| 19 | runs-on: ubuntu-latest |
| 20 | permissions: |
| 21 | contents: read |
| 22 | packages: write |
| 23 | |
| 24 | steps: |
| 25 | - name: Checkout repository |
| 26 | uses: actions/checkout@v4 |
| 27 | |
| 28 | - name: Set up QEMU |
| 29 | uses: docker/setup-qemu-action@v3 |
| 30 | |
| 31 | - name: Set up Docker Buildx |
| 32 | uses: docker/setup-buildx-action@v3 |
| 33 | |
| 34 | - name: Log in to Container Registry |
| 35 | if: github.event_name != 'pull_request' |
| 36 | uses: docker/login-action@v3 |
| 37 | with: |
| 38 | registry: ${{ env.REGISTRY }} |
| 39 | username: ${{ github.actor }} |
| 40 | password: ${{ secrets.GITHUB_TOKEN }} |
| 41 | |
| 42 | - name: Extract metadata (tags, labels) |
| 43 | id: meta |
| 44 | uses: docker/metadata-action@v5 |
| 45 | with: |
| 46 | images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 47 | tags: | |
| 48 | type=ref,event=branch |
| 49 | type=ref,event=pr |
| 50 | type=semver,pattern={{version}} |
| 51 | type=semver,pattern={{major}}.{{minor}} |
| 52 | type=semver,pattern={{major}} |
| 53 | type=sha |
| 54 | type=raw,value=latest,enable={{is_default_branch}} |
| 55 | |
| 56 | - name: Build and push Docker image |
| 57 | uses: docker/build-push-action@v6 |
| 58 | with: |
| 59 | context: . |
| 60 | platforms: ${{ github.event_name == 'pull_request' && 'linux/amd64' || 'linux/amd64,linux/arm64' }} |
| 61 | push: ${{ github.event_name != 'pull_request' }} |
| 62 | tags: ${{ steps.meta.outputs.tags }} |
| 63 | labels: ${{ steps.meta.outputs.labels }} |
| 64 | cache-from: type=gha |
| 65 | cache-to: type=gha,mode=max |
| 66 | |
| 67 | |