diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..8e5e92b --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,68 @@ +name: Build and Publish Docker Image + +on: + pull_request: + push: + branches: [main] + tags: ["v*.*.*"] + +concurrency: + group: docker-publish-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Buildx + uses: docker/setup-buildx-action@v3 + + # Only log in when we're actually going to push. Pull requests (including + # those from forks) build the image to validate the Dockerfile but never + # push, so they need no registry credentials or secrets. + - name: Log in to GHCR + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Tagging scheme: + # push to main -> :latest, :sha- + # tag v1.2.3 -> :stable, :1.2.3, :1.2 + # pull request -> (none; build only) + # :latest tracks main HEAD; :stable tracks the newest release tag. + - name: Compute tags and labels + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository }} + flavor: | + latest=false + tags: | + type=raw,value=latest,enable={{is_default_branch}} + type=sha,prefix=sha-,enable={{is_default_branch}} + type=raw,value=stable,enable=${{ startsWith(github.ref, 'refs/tags/v') }} + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + # Read the layer cache on every run; only write it when pushing, since + # fork pull requests can't write to the Actions cache. + cache-from: type=gha + cache-to: ${{ github.event_name != 'pull_request' && 'type=gha,mode=max' || '' }}