|
| 1 | +name: Build and Push Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + tags: |
| 8 | + - 'v*.*.*' # Triggers on version tags like v1.0.0, v1.2.3, etc. |
| 9 | + pull_request: |
| 10 | + branches: |
| 11 | + - main |
| 12 | + |
| 13 | +env: |
| 14 | + DOCKER_IMAGE: matteomori/sentinel |
| 15 | + |
| 16 | +jobs: |
| 17 | + build-and-push: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + permissions: |
| 20 | + contents: read |
| 21 | + packages: write |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout code |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - name: Set up Go |
| 28 | + uses: actions/setup-go@v5 |
| 29 | + with: |
| 30 | + go-version: '1.23' |
| 31 | + cache: true |
| 32 | + |
| 33 | + - name: Run tests |
| 34 | + run: go test -v ./... |
| 35 | + |
| 36 | + - name: Set up Docker Buildx |
| 37 | + uses: docker/setup-buildx-action@v3 |
| 38 | + |
| 39 | + - name: Log in to DockerHub |
| 40 | + if: github.event_name != 'pull_request' |
| 41 | + uses: docker/login-action@v3 |
| 42 | + with: |
| 43 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 44 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 45 | + |
| 46 | + - name: Extract metadata (tags, labels) |
| 47 | + id: meta |
| 48 | + uses: docker/metadata-action@v5 |
| 49 | + with: |
| 50 | + images: ${{ env.DOCKER_IMAGE }} |
| 51 | + tags: | |
| 52 | + # Tag as 'latest' for pushes to main branch |
| 53 | + type=raw,value=latest,enable={{is_default_branch}} |
| 54 | + # Tag with git tag (v1.2.3 -> 1.2.3) |
| 55 | + type=semver,pattern={{version}} |
| 56 | + # Tag with major.minor (v1.2.3 -> 1.2) |
| 57 | + type=semver,pattern={{major}}.{{minor}} |
| 58 | + # Tag with major version (v1.2.3 -> 1) |
| 59 | + type=semver,pattern={{major}} |
| 60 | + # Tag with short SHA for commits |
| 61 | + type=sha,prefix=sha-,format=short |
| 62 | + # Tag with branch name for non-main branches |
| 63 | + type=ref,event=branch |
| 64 | + # Tag with PR number for pull requests |
| 65 | + type=ref,event=pr |
| 66 | +
|
| 67 | + - name: Build and push Docker image |
| 68 | + id: build |
| 69 | + uses: docker/build-push-action@v5 |
| 70 | + with: |
| 71 | + context: . |
| 72 | + push: ${{ github.event_name != 'pull_request' }} |
| 73 | + tags: ${{ steps.meta.outputs.tags }} |
| 74 | + labels: ${{ steps.meta.outputs.labels }} |
| 75 | + cache-from: type=gha |
| 76 | + cache-to: type=gha,mode=max |
| 77 | + platforms: linux/amd64,linux/arm64 |
| 78 | + |
| 79 | + - name: Generate artifact attestation |
| 80 | + if: github.event_name != 'pull_request' |
| 81 | + uses: actions/attest-build-provenance@v1 |
| 82 | + with: |
| 83 | + subject-name: ${{ env.DOCKER_IMAGE }} |
| 84 | + subject-digest: ${{ steps.build.outputs.digest }} |
| 85 | + push-to-registry: true |
0 commit comments