GitHub Actions Docker Build & Publish Workflow

Build and push a Docker image to Docker Hub or GHCR on every push

Generate a GitHub Actions workflow that sets up Docker Buildx, logs into Docker Hub or GitHub Container Registry, builds a multi-platform image with layer caching, and pushes tags derived from branch, semver, and commit SHA.

What is the difference between Docker Hub and GHCR?

Docker Hub is Docker's public registry at docker.io and GHCR is GitHub Container Registry at ghcr.io, tied to your GitHub account or org. GHCR can authenticate with the built-in GITHUB_TOKEN, while Docker Hub needs a username and access token stored as secrets.

Publishing a Docker image from CI should be repeatable and multi-architecture. This generator produces a GitHub Actions workflow that configures Docker Buildx, logs into Docker Hub or GHCR, builds with layer caching, and pushes tags derived automatically from your Git context.

How it works

The workflow chains the official Docker actions:

  1. docker/setup-qemu-action enables cross-architecture emulation for multi-platform builds.
  2. docker/setup-buildx-action provisions the Buildx builder.
  3. docker/login-action authenticates to the chosen registry.
  4. docker/metadata-action computes tags and labels from the branch, semver tags, and commit SHA.
  5. docker/build-push-action builds for your selected platforms, pushes the image, and reads/writes the GitHub Actions layer cache.

Tips and notes

  • For GHCR, the workflow can use the built-in GITHUB_TOKEN; for Docker Hub you must add DOCKERHUB_USERNAME and a DOCKERHUB_TOKEN access token.
  • Multi-platform builds are slower but produce a single tag that runs on both Intel and ARM hosts.
  • Layer caching via type=gha makes incremental builds fast; cold builds still pay full cost.
  • The metadata action’s semver tags only appear when you push a Git tag like v1.2.3.