|
| 1 | +name: Build and Push Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - master |
| 8 | + tags: |
| 9 | + - 'v*' |
| 10 | + pull_request: |
| 11 | + branches: |
| 12 | + - main |
| 13 | + - master |
| 14 | + workflow_dispatch: |
| 15 | + |
| 16 | +env: |
| 17 | + REGISTRY: ghcr.io |
| 18 | + IMAGE_NAME: ${{ github.repository }} |
| 19 | + |
| 20 | +jobs: |
| 21 | + build-and-push: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + packages: write |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v4 |
| 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,prefix={{branch}}- |
| 54 | + type=raw,value=latest,enable={{is_default_branch}} |
| 55 | +
|
| 56 | + - name: Build and push Docker image |
| 57 | + id: build |
| 58 | + uses: docker/build-push-action@v5 |
| 59 | + with: |
| 60 | + context: . |
| 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 | + platforms: linux/amd64,linux/arm64 |
| 67 | + |
| 68 | + - name: Generate artifact attestation |
| 69 | + if: github.event_name != 'pull_request' |
| 70 | + uses: actions/attest-build-provenance@v1 |
| 71 | + with: |
| 72 | + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 73 | + subject-digest: ${{ steps.build.outputs.digest }} |
| 74 | + push-to-registry: true |
0 commit comments