# Docker build and publish workflow

> GitHub's official Docker publish starter workflow, explained and hardened by Latchkey.

Source: https://latchkey.dev/workflows/docker-publish-ghcr  
Upstream: [GitHub official starter workflow](https://github.com/actions/starter-workflows/blob/main/ci/docker-publish.yml) (ci/docker-publish.yml), licence CC0-1.0

## What it does

This is the Docker publish workflow GitHub offers by default. It builds a container image with Buildx, signs it with cosign, and pushes it to the GitHub Container Registry, using the GitHub Actions cache for layers.

It uses several third-party actions pinned only to tags, which Latchkey flags for SHA pinning below, and it adds concurrency and a job timeout.

## The workflow

```yaml
name: Docker

on:
  push:
    branches: [ "main" ]
    tags: [ 'v*.*.*' ]
  pull_request:
    branches: [ "main" ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Install cosign
        if: github.event_name != 'pull_request'
        uses: sigstore/cosign-installer@v3.5.0
        with:
          cosign-release: 'v2.2.4'
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3.0.0
      - name: Log into registry ${{ env.REGISTRY }}
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3.0.0
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@v5.0.0
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
      - name: Build and push Docker image
        id: build-and-push
        uses: docker/build-push-action@v5.0.0
        with:
          context: .
          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
```

## FAQ

### What does the Docker build and publish workflow workflow do?

This is the Docker publish workflow GitHub offers by default. It builds a container image with Buildx, signs it with cosign, and pushes it to the GitHub Container Registry, using the GitHub Actions cache for layers.

### What CI health grade does this workflow get?

This Docker workflow grades C. Paste your own workflow into the Latchkey grader to see its grade and the exact fixes.

### How can I improve this Docker workflow?

Add run de-duplication and job timeouts. Latchkey applies these automatically on managed runners when you point runs-on at Latchkey.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
