|
| 1 | +name: Docker |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: ["*"] |
| 6 | + branches: |
| 7 | + - "main" |
| 8 | + pull_request: |
| 9 | + branches: ["**"] |
| 10 | + |
| 11 | +env: |
| 12 | + # Hostname of your registry |
| 13 | + REGISTRY: docker.io |
| 14 | + # Image repository, without hostname and tag |
| 15 | + IMAGE_NAME: ${{ github.repository }} |
| 16 | + SHA: ${{ github.event.pull_request.head.sha || github.event.after }} |
| 17 | + |
| 18 | +jobs: |
| 19 | + build: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + permissions: |
| 22 | + pull-requests: write |
| 23 | + |
| 24 | + steps: |
| 25 | + # Authenticate to the container registry |
| 26 | + - name: Authenticate to registry ${{ env.REGISTRY }} |
| 27 | + uses: docker/login-action@v3 |
| 28 | + with: |
| 29 | + registry: ${{ env.REGISTRY }} |
| 30 | + username: ${{ secrets.REGISTRY_USER }} |
| 31 | + password: ${{ secrets.REGISTRY_TOKEN }} |
| 32 | + |
| 33 | + - name: Setup Docker buildx |
| 34 | + uses: docker/setup-buildx-action@v3 |
| 35 | + |
| 36 | + # Extract metadata (tags, labels) for Docker |
| 37 | + - name: Extract Docker metadata |
| 38 | + id: meta |
| 39 | + uses: docker/metadata-action@v5 |
| 40 | + with: |
| 41 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 42 | + labels: | |
| 43 | + org.opencontainers.image.revision=${{ env.SHA }} |
| 44 | + tags: | |
| 45 | + type=edge,branch=$repo.default_branch |
| 46 | + type=semver,pattern=v{{version}} |
| 47 | + type=sha,prefix=,suffix=,format=short |
| 48 | +
|
| 49 | + # Build and push Docker image with Buildx |
| 50 | + # (don't push on PR, load instead) |
| 51 | + - name: Build and push Docker image |
| 52 | + id: build-and-push |
| 53 | + uses: docker/build-push-action@v6 |
| 54 | + with: |
| 55 | + sbom: ${{ github.event_name != 'pull_request' }} |
| 56 | + provenance: ${{ github.event_name != 'pull_request' }} |
| 57 | + push: ${{ github.event_name != 'pull_request' }} |
| 58 | + load: ${{ github.event_name == 'pull_request' }} |
| 59 | + tags: ${{ steps.meta.outputs.tags }} |
| 60 | + labels: ${{ steps.meta.outputs.labels }} |
| 61 | + cache-from: type=gha |
| 62 | + cache-to: type=gha,mode=max |
0 commit comments