name: Build and Push Docker Image on: push: branches: [main] tags: ["v*"] pull_request: branches: [main] # Set permissions for GITHUB_TOKEN permissions: contents: read packages: write jobs: build: runs-on: ubuntu-latest steps: # Step 1: Check out the repository code - name: Checkout uses: actions/checkout@v4 # Step 2: Set up Docker Buildx for advanced features - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 # Step 3: Log in to GitHub Container Registry - name: Log in to GHCR uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} # Step 4: Generate tags and labels from Git metadata - name: Extract metadata id: meta uses: docker/metadata-action@v5 with: images: ghcr.io/${{ github.repository }} tags: | # Tag with branch name type=ref,event=branch # Tag with PR number type=ref,event=pr # Tag with semver from git tag type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} # Tag with short SHA type=sha,prefix= # Step 5: Build and push the Docker image - name: Build and push uses: docker/build-push-action@v5 with: context: . # Only push on main branch and tags, not PRs push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} # Enable build cache for faster builds cache-from: type=gha cache-to: type=gha,mode=max