Build and publish a container image with clear tags
In this task you'll build a container image in the pipeline and publish it with tags people can trust.
GitHub Container Registry (ghcr.io) stores images next to your code. A workflow signs in with its own token (packages: write), builds the image and pushes it. The metadata action works out tags for you: the branch name, the short SHA, and on a version tag the full and short version (1.4.0, 1.4).
Tags are labels, and labels can move: latest and 1.4 point to new images over time. Deploy by an immutable reference, the version tag you never reuse or, better, the image digest (sha256:...), so you know exactly what's running. Push only from main and from version tags; pull requests should build but not push.
Example. Harbourline Design's preview builds were all tagged latest, and twice a client saw an unfinished site. Now every image is tagged with the short commit SHA, and production deploys by digest.
Before you start
You need: A free GitHub account and a public test repository (Actions minutes are free for public repositories). Time: 30 minutes.
Where to look
- Repository
- .github/workflows/image.yml
- Packages
Steps
- Add a small
Dockerfile(for exampleFROM python:3.13-slimand copy app.py). - Add
.github/workflows/image.ymlfrom Commands. It pushes only on main and v tags. - Open a pull request and see the image built but not pushed.
- Merge, then open your profile's Packages and see the image with
mainandsha-...tags. - Push tag v1.2.0 and see
1.2.0and1.2added. Note the digest the build printed.
Commands
permissions:
contents: read
packages: write
jobs:
image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch
type=sha
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- uses: docker/build-push-action@v6
with:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Watch out for
- Cost
- Free for public images. Private images use your plan's package storage.
- Security
- Use the workflow's GITHUB_TOKEN, not a personal token, and grant
packages: writeonly to the job that pushes. - Common mistakes
- Deploying
latest, and pushing images from pull requests (anyone can open one).
Clean up
Delete the test image from Packages, Package settings, Delete this package. Deleting the repository doesn't delete its images.
Words to know
- GHCR
- GitHub Container Registry, at ghcr.io.
- Digest
- A hash that identifies one exact image and never changes.
- Immutable tag
- A tag you promise never to point at a different image.
- OCI label
- Metadata in the image, such as the source repository and commit.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On