A complete CI/CD pipeline
Git chapter 6 introduced workflows. Here you build the whole pipeline a real team runs: every push and pull request is tested, every merge to main builds a Docker image and pushes it to a registry, and the new version is deployed to a server, with secrets kept out of the code and a manual approval before production.
The plan
- Test on every push and pull request. Nothing else happens if this fails.
- Build and push a Docker image, but only for
main, tagged with the commit's SHA so every version is traceable. - Deploy that exact image to the server, after a person approves it.
The repository holds the app, its tests, a Dockerfile, and the workflow in .github/workflows/pipeline.yml. The server holds a compose.yaml that runs the image.
The whole workflow
name: pipeline
on:
push:
branches: [main]
pull_request:
permissions:
contents: read # the default for every job: read the code, nothing more
concurrency:
group: pipeline-${{ github.ref }}
cancel-in-progress: true # a newer push cancels the older run of the same branch
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12" # quoted: see YAML in 15 minutes
cache: pip
- run: pip install -r requirements-dev.txt
- run: pytest -q
build:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write # allowed to push to GitHub Container Registry
steps:
- uses: actions/checkout@v4
- name: Image name (registries need lowercase)
run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
push: true
tags: ${{ env.IMAGE }}:${{ github.sha }},${{ env.IMAGE }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
needs: build
runs-on: ubuntu-latest
environment: production # waits for approval if the environment requires it
steps:
- name: Deploy over SSH
env:
SSH_KEY: ${{ secrets.SSH_KEY }}
HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
TAG: ${{ github.sha }}
run: |
mkdir -p ~/.ssh
printf '%s\n' "$SSH_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H "$HOST" >> ~/.ssh/known_hosts
ssh "$DEPLOY_USER@$HOST" "cd /srv/app && echo TAG=$TAG > .env && docker compose pull && docker compose up -d"
Stage by stage
- test uses
cache: pipso dependencies aren't downloaded from scratch every run.requirements-dev.txtholds the app's packages plus test tools like pytest. - build runs only after
testpasses (needs) and only onmain(if). The image goes to GHCR (GitHub Container Registry) using the automaticGITHUB_TOKEN, so there's no password to manage. Tagging withgithub.shameans you can always tell which commit is running, and roll back to any of them. Theghacache reuses Docker layers between runs. - deploy logs in to the server over SSH and tells Docker Compose to pull and start the new tag. Writing the tag into
.envon the server means a later manualdocker compose upkeeps the same version.
On the server, /srv/app/compose.yaml uses that tag:
services:
web:
image: ghcr.io/you/app:${TAG:-latest}
restart: unless-stopped
ports:
- "80:8000"
New GHCR packages are private, so log the server in once with a GitHub token that only has read:packages: docker login ghcr.io -u you. Or make the package public in its settings on GitHub.
Secrets and environments
- Add secrets under the repository's Settings, then Secrets and variables, then Actions. Workflows read them as
${{ secrets.NAME }}, and GitHub masks them in logs. Neverechothem anyway. - The deploy key is a separate SSH key pair made just for CI. Its public half goes into
~/.ssh/authorized_keysof a dedicateddeployuser on the server; the private half (including the BEGIN and END lines) goes into theSSH_KEYsecret. - Under Settings, then Environments, create
productionand add required reviewers (available on public repositories, and on private ones with paid plans). The deploy job then waits for a click before it runs. Environments can also hold their own secrets, so a staging job can't see production's. ssh-keyscantrusts whatever host answers the first time. For more safety, store the server's known_hosts line as a secret and write that instead.- Deploying to a cloud? Prefer OIDC over stored access keys: with
permissions: id-token: write, theaws-actions/configure-aws-credentialsaction exchanges a short-lived GitHub token for temporary AWS credentials, so there's no long-lived key to leak (Cloud chapter 4 covers roles).
Keeping main green
Under Settings, then Branches, add a branch protection rule (or ruleset) for main that requires the test job to pass before a pull request can be merged. Combined with the pipeline above, broken code can't reach main, and main is always deployable.
Rolling back
Because every image is tagged with its commit, rolling back is just deploying an older tag. Either re-run the old workflow run from the Actions tab, or add a manual trigger:
on:
workflow_dispatch:
inputs:
tag:
description: "Image tag (commit SHA) to deploy"
required: true
and use ${{ inputs.tag }} instead of github.sha in the deploy step.
Deploying to Kubernetes instead
The test and build jobs stay exactly the same. The deploy step changes to something like kubectl set image deployment/web web=$IMAGE:$TAG or helm upgrade --install, with the cluster credentials coming from a secret or OIDC. Bigger teams often go one step further with GitOps: the pipeline only updates the image tag in a Git repository, and a tool inside the cluster such as Argo CD notices and applies it.
Common failures
denied: permission_denied: write_package: the job is missingpackages: write, or the package already exists and isn't linked to this repository (fix it in the package's settings).repository name must be lowercase: use the lowercase step shown above.Host key verification failed:known_hostswasn't written, or the server was rebuilt and its key changed.Permission denied (publickey): wrong user, the public key isn't inauthorized_keys, or the secret lost its first or last line.- The workflow never starts: the file isn't in
.github/workflows/, the branch name inon:is wrong, or the YAML is invalid (the Actions tab shows the error). - Secrets are empty: pull requests from forks don't get secrets, on purpose. Run the deploy only on pushes to
main, as above.