Sub topic 10
Handling credentials and permissions safely in CI
This chapter assumes you can read a GitHub Actions workflow and have seen the docker/login-action step that logs in to ghcr.io. Here you will limit what that login can do, and who can trigger it.
Principles
A CI job runs code that you did not write by hand: dependencies, build scripts, and sometimes pull requests from strangers. Treat every credential as something that code could read. Three rules follow:
- Give each job the least permission it needs.
- Prefer short-lived credentials over stored passwords.
- Do not expose credentials to code you have not reviewed.
Use the built-in token with explicit permissions
GitHub creates a GITHUB_TOKEN for each workflow run and revokes it when the run ends. Nothing is stored in the repository, and nothing can be reused later. You control its scope with a permissions block. Any permission you do not list is set to none.
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4Set a read-only default at the top of the file. Grant packages: write only on the job that pushes. If a test job is added later, it does not inherit write access to your registry.
Keep credentials away from pull requests
Workflows triggered by pull_request from a fork receive a read-only token and no repository secrets. This is intended. Do not work around it with pull_request_target, which runs with secrets and write access. If it builds the pull request's code, that code can read them.
Skip login on pull requests, so the build still runs but cannot publish:
- name: Log in to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}Secrets needed during the build
Sometimes a build needs a credential, such as a token for a private package registry. Do not pass it with ARG or ENV. Both are recorded in the image history, and anyone who can pull the image can read them. Use a BuildKit secret mount instead. The file exists only while that one RUN step executes.
# syntax=docker/dockerfile:1
FROM node:20-alpine
WORKDIR /app
COPY package.json package-lock.json .npmrc ./
RUN --mount=type=secret,id=npm_token \
NPM_TOKEN="$(cat /run/secrets/npm_token)" npm ciHere .npmrc refers to ${NPM_TOKEN}, so the token is never written into a layer. In the workflow, pass the secret to the build:
- uses: docker/build-push-action@v6
with:
context: .
secrets: |
npm_token=${{ secrets.NPM_TOKEN }}Choosing a credential type
| Credential | Lifetime | Use when |
|---|---|---|
| `GITHUB_TOKEN` | One run | Pushing to ghcr.io from the same repository |
| OIDC federation | Minutes | Pushing to a cloud registry such as ECR, without stored keys |
| Stored secret (PAT, registry password) | Until rotated | The registry supports nothing else |
For cloud registries, OpenID Connect (OIDC) lets the job prove its identity to the cloud provider and receive a temporary token. This needs id-token: write on the job. If you must store a secret, scope it to one repository and one registry, and set an expiry.
Exercise
A workflow has no permissions block, pushes on every pull request, and passes an npm token with ARG NPM_TOKEN. Name two changes that make it safer.
Show answer
First, add a read-only top-level permissions block and grant packages: write only to the job that pushes. Skip login and set push: false when the event is pull_request. Second, replace ARG NPM_TOKEN with a RUN --mount=type=secret step and pass the value through the secrets input of docker/build-push-action, so the token never appears in image history.