Add Docker support with GHCR automated publishing #1
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 Publish Docker Image | |
| on: | |
| push: | |
| branches: | |
| - main | |
| tags: | |
| - 'v*.*.*' | |
| pull_request: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| attestations: write | |
| id-token: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GitHub Container Registry | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata (tags, labels) for Docker | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| # Set latest tag for main branch | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| # Tag with version for releases (v1.0.0 -> 1.0.0) | |
| type=semver,pattern={{version}} | |
| # Tag with major.minor for releases (v1.0.0 -> 1.0) | |
| type=semver,pattern={{major}}.{{minor}} | |
| # Tag with major for releases (v1.0.0 -> 1) | |
| type=semver,pattern={{major}} | |
| # Tag with branch name for PRs | |
| type=ref,event=branch | |
| # Tag with PR number for PRs | |
| type=ref,event=pr | |
| # Tag with SHA for all events | |
| type=sha,prefix={{branch}}- | |
| - name: Build and push Docker image | |
| id: push | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| platforms: linux/amd64,linux/arm64 | |
| 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 | |
| - name: Generate artifact attestation | |
| if: github.event_name != 'pull_request' | |
| uses: actions/attest-build-provenance@v1 | |
| with: | |
| subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| subject-digest: ${{ steps.push.outputs.digest }} | |
| push-to-registry: true | |
| - name: Output image details | |
| if: github.event_name != 'pull_request' | |
| run: | | |
| echo "Image published:" | |
| echo "Registry: ${{ env.REGISTRY }}" | |
| echo "Repository: ${{ env.IMAGE_NAME }}" | |
| echo "Tags:" | |
| echo "${{ steps.meta.outputs.tags }}" | |
| echo "" | |
| echo "Pull with:" | |
| echo "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" |