ci: add Docker build and push workflow #2
Workflow file for this run
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: | |
| push: | |
| branches: | |
| - main | |
| tags: | |
| - 'v*.*.*' # Triggers on version tags like v1.0.0, v1.2.3, etc. | |
| pull_request: | |
| branches: | |
| - main | |
| env: | |
| DOCKER_IMAGE: matteomori/sentinel | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: '1.23' | |
| cache: true | |
| - name: Run tests | |
| run: go test -v ./... | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to DockerHub | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Extract metadata (tags, labels) | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.DOCKER_IMAGE }} | |
| tags: | | |
| # Tag as 'latest' for pushes to main branch | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| # Tag with git tag (v1.2.3 -> 1.2.3) | |
| type=semver,pattern={{version}} | |
| # Tag with major.minor (v1.2.3 -> 1.2) | |
| type=semver,pattern={{major}}.{{minor}} | |
| # Tag with major version (v1.2.3 -> 1) | |
| type=semver,pattern={{major}} | |
| # Tag with short SHA for commits | |
| type=sha,prefix=sha-,format=short | |
| # Tag with branch name for non-main branches | |
| type=ref,event=branch | |
| # Tag with PR number for pull requests | |
| type=ref,event=pr | |
| - name: Build and push Docker image | |
| id: build | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| platforms: linux/amd64 | |
| - name: Generate artifact attestation | |
| if: github.event_name != 'pull_request' | |
| uses: actions/attest-build-provenance@v1 | |
| with: | |
| subject-name: ${{ env.DOCKER_IMAGE }} | |
| subject-digest: ${{ steps.build.outputs.digest }} | |
| push-to-registry: true |