Sub topic 10
Pulling images from private registries
This chapter assumes you can write a basic services block and know how GitHub Actions secrets work. Public images such as postgres:16 need no login. If your service image lives in a private registry, the runner has to authenticate before it can pull the image.
Why a service needs credentials
Service containers are started by the runner before any step runs. Because no step has run yet, you can't put a docker/login-action step in front of them. Instead, you give the credentials to the service itself, using a credentials key.
The key takes a username and a password. Both values should come from secrets so they never appear in the workflow file.
Declaring credentials
name: integration-tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
api-mock:
image: ghcr.io/example-org/api-mock:1.4
credentials:
username: ${{ github.actor }}
password: ${{ secrets.GHCR_PULL_TOKEN }}
ports:
- 8080:8080
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integration
env:
API_MOCK_URL: http://localhost:8080The runner uses these credentials only to pull the image for this service. The image name includes the registry host (ghcr.io), so the runner knows which registry to log in to. For Docker Hub, omit the host and use a name like example-org/api-mock:1.4.
Credentials for the job container
If the job itself runs in a container, that container can need credentials as well. It uses the same key under container.
jobs:
test:
runs-on: ubuntu-latest
container:
image: ghcr.io/example-org/build-image:2
credentials:
username: ${{ github.actor }}
password: ${{ secrets.GHCR_PULL_TOKEN }}
services:
db:
image: ghcr.io/example-org/seeded-postgres:16
credentials:
username: ${{ github.actor }}
password: ${{ secrets.GHCR_PULL_TOKEN }}
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integration
env:
DATABASE_URL: postgres://postgres:postgres@db:5432/postgresEach container has its own credentials block. Credentials set on one do not carry over to the others, even when they share a registry.
Choosing the token
| Registry | Username | Password |
|---|---|---|
| GitHub Container Registry | `github.actor` or a fixed user name | A token with `read:packages` scope, or `secrets.GITHUB_TOKEN` when the package grants this repository access |
| Docker Hub | Your Docker Hub user name | An access token with read-only permission |
| Amazon ECR | `AWS` | A short-lived password from `aws ecr get-login-password` |
Use the narrowest token that can pull images. Never use a personal password. Prefer a read-only token owned by a service account, so that rotating it does not depend on one person.
Secrets and pull requests
Secrets are not passed to workflows triggered by pull requests from forks. In that case secrets.GHCR_PULL_TOKEN is empty, the login fails, and the job stops while it starts the service. Two ways to handle this:
- Run the integration tests only on
pushand on pull requests from the same repository. - Publish a public copy of the image for fork builds, and select the image through an expression.
Recognizing a credentials failure
A credentials problem appears in the "Initialize containers" step of the job log, before any of your own steps. Typical messages are:
unauthorizedordenied: the token is wrong, expired, or lacks the read scope.manifest unknownornot found: the tag or image name is wrong, or the token cannot see a private package, which some registries report as "not found".Error: ... usernamewith an empty value: the secret is not defined or is not available to this event.
Because the failure happens before steps run, none of your steps will show output. Check that step first.
Exercise
A job's service uses a private image from ghcr.io. The workflow passes on pushes to your branches, but on pull requests from forks it fails in "Initialize containers" with an authentication error, and none of your steps run. What is the cause, and what are two ways to fix it?
Show answer
Secrets are not available to workflows triggered by fork pull requests, so the password is empty and the registry rejects the pull. Either limit the integration tests to pushes and same-repository pull requests, or use a public copy of the image for fork builds.