Sub topic 13
Automating pushes in a CI workflow
This chapter assumes you can build, tag, and push an image by hand, and that you have a Docker Hub account with a repository such as YOUR_USERNAME/myapp. It also assumes you have a GitHub repository containing the Dockerfile from earlier chapters. Here you move the same steps into a GitHub Actions workflow, so every change to main publishes a new image.
What CI needs to do
A CI (continuous integration) job runs on a fresh machine each time. It has no saved login and no local images. It must repeat every manual step: get the code, authenticate, build, and push.
The manual steps map to workflow steps like this:
| Manual step | Workflow step |
|---|---|
| Have the `Dockerfile` locally | `actions/checkout` |
| `docker login` | `docker/login-action` |
| `docker build`, `docker tag`, `docker push` | `docker/build-push-action` |
Store credentials as secrets
Never write a token in the workflow file. The file is committed to your repository, so anyone who can read the repository can read the token.
- In Docker Hub, create a personal access token. Give it the smallest permission that works. Read and write is enough for pushing.
- In your GitHub repository, open Settings, then Secrets and variables, then Actions.
- Create a secret named
DOCKERHUB_USERNAMEwith your Docker Hub username. - Create a secret named
DOCKERHUB_TOKENwith the token value.
GitHub hides secret values in job logs and does not pass them to workflows triggered from forks by default.
The workflow
Save this file as .github/workflows/publish.yml:
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 does one job:
on.push.brancheslimits the workflow to pushes onmain.docker/login-actionruns the equivalent ofdocker loginusing the two secrets. It reads the token without placing it on a visible command line.context: .tells the build action to use the repository root, where theDockerfilelives.push: truemakes the action upload the image after building. Without it, the image is built and then discarded.tagsis the full name, including your namespace. The action tags and pushes in one step, so you do not rundocker tagyourself.
Tag more than `latest`
Pushing only latest overwrites the previous image each time. You cannot tell which commit produced it, and you cannot roll back to an earlier build by name. A common fix is to add a second tag containing the commit hash. The tags input accepts several lines:
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/myapp:latest
${{ secrets.DOCKERHUB_USERNAME }}/myapp:${{ github.sha }}Both tags point to the same image. Because of layers and digests, the second tag adds almost no upload time.
Common errors
Username and password required: a secret name in the workflow does not match the name in GitHub, or the secrets were created in a different repository.denied: requested access to the resource is denied: the token lacks write permission, or the repository name intagsdoes not match your namespace.- Workflow does not start: the file is not in
.github/workflows/, or the push was to a branch other thanmain.
If you need multi-platform images from CI, the action can build them too. You add the docker/setup-buildx-action step and a platforms input, which mirror the --platform flag from the previous chapter.
Exercise
Why does the workflow use docker/login-action with secrets instead of a docker login -u YOUR_USERNAME -p PASSWORD command written in the file, and what permission should the token have?
Show answer
Anything written in the workflow file is committed to the repository and visible to its readers, and -p also exposes the value on the command line. Secrets keep the value out of the repository and mask it in logs. The token should be an access token (not your password) with only the permission it needs, which for pushing is read and write.
Exercise
Your workflow builds successfully, but nothing appears on Docker Hub. Which single input in docker/build-push-action is the most likely cause, and what value fixes it?
Show answer
The push input is missing or set to false. Set push: true so the action uploads the image after building it.