{"name":"github-actions-docker","title":"GitHub Actions Docker build and push","description":"A GitHub Actions workflow that builds a Docker image on pull requests and pushes it to GitHub Container Registry from main and version tags, with caching.","category":"deploy","updated":"2026-11-30","url":"https://sniptide.com/templates/github-actions-docker","files":[{"name":".github/workflows/docker.yml","language":"yaml","content":"name: Docker\n\non:\n  push:\n    branches: [main]\n    tags: [\"v*.*.*\"]\n  pull_request:\n  workflow_dispatch:\n\nconcurrency:\n  group: docker-${{ github.ref }}\n  cancel-in-progress: true\n\npermissions:\n  contents: read\n\njobs:\n  image:\n    runs-on: ubuntu-latest\n    timeout-minutes: 20\n    permissions:\n      contents: read\n      packages: write\n    steps:\n      - uses: actions/checkout@v7\n        with:\n          persist-credentials: false\n\n      - uses: docker/setup-buildx-action@v4\n\n      # Pull requests only build the image. Pushes to main and version tags also publish it.\n      - if: github.event_name != 'pull_request'\n        uses: docker/login-action@v4\n        with:\n          registry: ghcr.io\n          username: ${{ github.actor }}\n          password: ${{ secrets.GITHUB_TOKEN }}\n\n      - id: meta\n        uses: docker/metadata-action@v6\n        with:\n          images: ghcr.io/${{ github.repository }}\n          tags: |\n            type=ref,event=branch\n            type=ref,event=pr\n            type=semver,pattern={{version}}\n            type=semver,pattern={{major}}.{{minor}}\n            type=sha\n\n      - uses: docker/build-push-action@v7\n        with:\n          context: .\n          push: ${{ github.event_name != 'pull_request' }}\n          tags: ${{ steps.meta.outputs.tags }}\n          labels: ${{ steps.meta.outputs.labels }}\n          cache-from: type=gha\n          cache-to: type=gha,mode=max\n"}]}