Sub topic 9
Publishing to GHCR from GitHub Actions
This chapter assumes you can build an image, understand docker login, and have a GitHub repository with a Dockerfile. You should also know what a personal access token (PAT) is. The goal is to publish an image to GitHub Container Registry (GHCR) from a workflow without storing a PAT.
Why use the built-in token
Every workflow run gets a short-lived credential called GITHUB_TOKEN. GitHub creates it when the job starts and revokes it when the job ends. If the workflow grants it the packages: write permission, it can push to GHCR. This has three advantages over a PAT:
- Nothing to rotate, because the token expires with the run.
- Nothing to leak from a person's account, because it is not tied to a user.
- Access is limited to the repository the workflow runs in.
The workflow
name: publish
on:
push:
tags: ["v*"]
permissions:
contents: read
packages: write
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}Here is what each part does:
on.push.tagsstarts the workflow when you push a tag that begins withv, such asv1.0.0.permissionssets the token's scope for the whole workflow. Granting onlycontents: readandpackages: writefollows least privilege. Any permission you do not list is set to none.docker/login-actionrunsdocker login ghcr.iofor you, using the token as the password.docker/build-push-actionbuilds the image and pushes it. The tag is the commit SHA, so each build gets a unique, traceable name.
Common errors
Image names must be lowercase. github.repository keeps the original capitalization, so a repository such as MyOrg/MyApp produces an invalid name. Lowercase the value in a step before you use it:
- name: Lowercase image name
run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"Then use ${{ env.IMAGE }}:${{ github.sha }} as the tag.
Other frequent failures:
denied: permission_denied: write_package: the workflow is missingpackages: write. It can also happen when the package already exists and is not linked to this repository. In that case, open the package settings and give the repository write access under "Manage Actions access".- Workflow permissions set to read-only: an organization or repository default can restrict the token. A
permissionsblock in the workflow file overrides the default, but an organization policy can still cap it. - Pushes from pull requests from forks: the token for those runs is read-only. Publish only on pushes to your own branches or tags.
Visibility of the new package
A package that a workflow creates is private by default. You can change its visibility in the package settings. Private packages inherit access from the linked repository. You will cover this in the access chapters.
Exercise
Your workflow fails with "denied: permissiondenied: writepackage". Name two things to check.
Show answer
Check that the workflow or job has packages: write under permissions. Then check that the package, if it already exists, grants this repository write access under "Manage Actions access" in the package settings. An organization-level read-only policy for GITHUB_TOKEN is a third possible cause.