Sub topic 13
Automating Pushes in a CI Workflow
This chapter moves the push from your terminal into a continuous integration (CI) pipeline. CI is a service that runs scripted jobs on a clean machine each time your code changes. It assumes you can already build an image, tag it, and push it by hand. It also assumes you have a Docker Hub account and a Git repository on GitHub. The examples use GitHub Actions, but the same ideas apply to GitLab CI, Jenkins, and similar tools.
Why automate the push
A manual push depends on one person's laptop. That person's local build tools, cached layers, and logged-in session all affect the result. A CI job builds from a fresh checkout every time, so the image on Docker Hub always matches a known commit.
Automation also changes how you authenticate. A CI job has no human to type a password. It must log in non-interactively, using a secret stored in the CI system.
Step 1: Create an access token for CI
Never put your Docker Hub password into a CI system. Use a personal access token (PAT) instead. A PAT is a long random string that works in place of your password and can be revoked on its own.
- In Docker Hub, open Account settings, then Personal access tokens.
- Create a token with a descriptive name, such as
github-actions-myapp. - Choose the narrowest permission that works. A job that only pushes needs Read & Write. It does not need Read, Write, Delete.
- Copy the token immediately. Docker Hub shows it only once.
Create one token per pipeline. If a token leaks, you revoke that one token and every other pipeline keeps working.
Step 2: Store credentials as repository secrets
A secret is a value that the CI system keeps encrypted. It is injected into jobs at run time and masked in logs. In GitHub, open Settings, then Secrets and variables, then Actions. Add two repository secrets:
| Secret name | Value |
|---|---|
| `DOCKERHUB_USERNAME` | Your Docker Hub username |
| `DOCKERHUB_TOKEN` | The access token from Step 1 |
Your username is not sensitive, so you could store it as a plain variable instead. The workflow below uses secrets for both, which keeps the setup uniform.
Logging in from a script
Before using a prebuilt action, look at what any CI system has to do. The login command reads the token from standard input:
echo "$DOCKERHUB_TOKEN" | docker login -u YOUR_USERNAME --password-stdin--password-stdin keeps the token out of the process list, where other users on the machine could see it. Do not write docker login -p TOKEN. That form puts the secret on the command line, and Docker warns that it is insecure.
A complete script for a CI system without a dedicated action looks like this:
#!/usr/bin/env bash
set -euo pipefail
IMAGE="${DOCKERHUB_USERNAME}/myapp"
VERSION="${GIT_COMMIT:-dev}"
echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
docker build -t "$IMAGE:$VERSION" -t "$IMAGE:latest" .
docker push "$IMAGE:$VERSION"
docker push "$IMAGE:latest"
docker logoutset -euo pipefail makes the script stop on the first failed command, on an unset variable, and on a failure anywhere in a pipe. Without it, a failed login could go unnoticed and the job would appear to succeed. Passing two -t flags to one build gives the image two tags without a separate docker tag step.
Step 3: The GitHub Actions workflow
Create .github/workflows/publish.yml in your repository:
name: publish
on:
push:
branches: [main]
jobs:
push-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/myapp:latestEach part of the workflow does one job:
on.push.branches: [main]runs the workflow whenever a commit lands onmain.actions/checkout@v4copies your repository, including the Dockerfile, onto the runner.docker/login-action@v3runs the equivalent ofdocker loginwith--password-stdin. It reads the secrets and masks them in the log.docker/build-push-action@v6builds the image fromcontextand pushes it. Withpush: trueit uploads the result. Withpush: false, which is the default, it only builds.
Commit the file and push it to main. Open the Actions tab in GitHub to watch the run. When it succeeds, the latest tag in your Docker Hub repository shows a new "last pushed" time.
Tagging in CI: avoid relying only on `latest`
The workflow above overwrites latest on every push. That is convenient, but it leaves you unable to say which commit a running container came from. It also makes rollback awkward, because the previous latest no longer has a name.
A better habit is to push an immutable tag as well. Use the commit SHA, which identifies the exact source:
- uses: docker/build-push-action@v6
with:
context: .
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/myapp:latest
${{ secrets.DOCKERHUB_USERNAME }}/myapp:${{ github.sha }}Both tags point to the same image, so the second tag costs no extra upload. The registry stores layers once, as the earlier chapter on layers and digests explained.
To release versions, you can also trigger on Git tags such as v1.2.0. Add tags: ["v*"] under on.push. Then use github.ref_name in the image tag.
Pushing multi-platform images from CI
A GitHub-hosted runner is usually linux/amd64. To publish an arm64 image as well, set up QEMU, an emulator, and a Buildx builder, then list the platforms. This is the CI form of the docker buildx build --platform linux/amd64,linux/arm64 ... --push command from the previous chapter.
name: publish
on:
push:
branches: [main]
jobs:
push-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/myapp:latest
${{ secrets.DOCKERHUB_USERNAME }}/myapp:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=maxThe two cache lines store build layers in the GitHub Actions cache. A fresh runner has no local layer cache, so without them every run rebuilds every layer. Emulated arm64 builds are slow, and the cache saves the most time there.
Pull requests and forks
Do not push images from pull requests. Two reasons apply:
- Workflows triggered by pull requests from forks do not receive your secrets. The login step would fail.
- Even for branches in your own repository, an unreviewed change should not publish to a repository others pull from.
The simplest protection is the trigger itself. The examples run only on push to main. If you want pull requests to check that the image still builds, add a separate trigger and keep the push conditional:
name: build
on:
pull_request:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
if: github.event_name != 'pull_request'
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ secrets.DOCKERHUB_USERNAME }}/myapp:latestOn a pull request, the login step is skipped and the image is built but not pushed. On main, both happen.
Common CI push errors
| Symptom in the job log | Likely cause | Fix |
|---|---|---|
| `Username and password required` | A secret is missing or misspelled. Secret names are case sensitive. | Check names under **Settings, Secrets and variables, Actions**. |
| `unauthorized: incorrect username or password` | The token was revoked, expired, or pasted with extra whitespace. | Create a new token and update the secret. |
| `denied: requested access to the resource is denied` | The token has read-only scope, or the tag uses a namespace you do not own. | Use a Read & Write token. Check that the tag starts with your username or organization. |
| Login step fails only on pull requests | Fork pull requests do not receive secrets. | Skip login and push on pull requests, as shown above. |
| `repository does not exist or may require authorization` | The repository name has a typo. Also, a private repository needs an authenticated pull. | Check the spelling, and log in before pulling. |
When a run fails, read the log from the first red step. A failed login is the root cause of every later error.
Security practices
- Use a token scoped to one purpose. Do not reuse your personal login token for CI.
- Rotate tokens. Set a reminder to replace tokens on a schedule, and revoke any token that appears in a log or a screenshot.
- Pin actions.
@v3tracks a moving major version. For stricter control, pin an action to a full commit SHA. - Do not echo secrets. GitHub masks exact secret values in logs, but transformed values, such as a base64-encoded token, may not be masked.
- Prefer an organization. If a team owns the image, push to an organization namespace and use a token from a service account, not a person's account. Then the pipeline survives when someone leaves.
Summary
A CI push repeats the manual steps (log in, build, tag, push), with the token supplied from a secret. You create a Read & Write access token, store it with your username as secrets, and use docker/login-action and docker/build-push-action. Add a commit SHA tag so each image can be traced to its source. Add Buildx and QEMU for several architectures. Restrict pushes to trusted branches so that pull requests only build.
Exercise
Your workflow works on pushes to main, but on a pull request from a contributor's fork the login step fails with "Username and password required". Why does this happen, and how should you change the workflow?
Show answer
Secrets are not passed to workflows triggered by pull requests from forks, so DOCKERHUB_USERNAME and DOCKERHUB_TOKEN are empty. Do not try to expose the secrets to fork pull requests. Instead, skip the login step with if: github.event_name != 'pull_request' and set push: ${{ github.event_name != 'pull_request' }} on the build step. Pull requests then only verify that the image builds.
Exercise
A pipeline pushes only the tag myapp:latest. A bad release goes out, and you need to roll back to the previous image. What is the problem, and what change to the tags input prevents it?
Show answer
Each push overwrites latest, so the previous image no longer has a tag and is hard to find. Add an immutable tag alongside it, for example ${{ secrets.DOCKERHUB_USERNAME }}/myapp:${{ github.sha }}. Then you can redeploy the earlier commit's tag. Both tags point to the same layers, so the extra tag adds no upload cost.
Exercise
Why does echo "$DOCKERHUB_TOKEN" | docker login -u YOUR_USERNAME --password-stdin appear in scripts instead of docker login -u YOUR_USERNAME -p "$DOCKERHUB_TOKEN"?
Show answer
With -p, the token becomes a command-line argument that other users on the machine can see in the process list. It can also be recorded in logs. --password-stdin reads the token from standard input, so it does not appear in the arguments.