name: Docker Build & Publish # This tells GitHub: "Run this every time I push code to the main branch" on: push: branches: [ "main" ] # Also run if I create a Release tag (e.g., v1.0) tags: [ 'v*.*.*' ] pull_request: branches: [ "main" ] env: # Use GitHub's built-in registry (ghcr.io) REGISTRY: ghcr.io # Use the repository name as the image name IMAGE_NAME: ${{ github.repository }} jobs: build: runs-on: ubuntu-latest permissions: contents: read packages: write # Needed to push the image to GHCR steps: - name: Checkout repository uses: actions/checkout@v3 # Set up Docker Buildx (The builder engine) - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 # Login to GitHub Container Registry using the automatic GitHub Token - name: Log into registry ${{ env.REGISTRY }} if: github.event_name != 'pull_request' uses: docker/login-action@v2 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} # Generate tags (e.g., :latest, :v1.0, :main) - name: Extract Docker metadata id: meta uses: docker/metadata-action@v4 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=raw,value=latest,enable={{is_default_branch}} type=ref,event=branch type=semver,pattern={{version}} # Build the image and push it to the registry - name: Build and push Docker image uses: docker/build-push-action@v4 with: context: . push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }}