The pipeline
GitLab starts the pipeline on every push to a branch. It has four stages. What runs depends on the branch and on the files changed.
The full diagram
| Job | Runner | When it runs |
|---|---|---|
api:lint, api:test | CI (docker) | if api/ changed, on any branch |
web:lint, web:test | CI (docker) | if web/ changed, on any branch |
api:build, web:build, docs:build | CI (docker) | on every push to develop or main |
deploy-dev | dev deploy (shell, tag dev) | on every push to develop |
deploy-prod | prod deploy (shell, tag prod) | on every push to main |
A stage only starts if all the jobs of the previous stage succeeded. A failing test blocks the build and the deployment.
What runs, by branch
| Push to… | lint | test | build | deploy |
|---|---|---|---|---|
feature/x, only touches web/ | web:lint | web:test | — | — |
feature/x, touches api/ and web/ | both | both | — | — |
develop (after a merge request) | depends on files | depends on files | both | deploy-dev |
main (after a merge request) | depends on files | depends on files | both | deploy-prod |
Triggers
stages:
- lint
- test
- build
- deploy
workflow:
rules:
- if: $CI_COMMIT_BRANCH
include:
- local: api/.gitlab-ci.yml
- local: web/.gitlab-ci.yml
- local: docs/.gitlab-ci.yml
deploy-dev:
stage: deploy
tags:
- dev
environment:
name: dev
url: https://dev.trinity.jostud.com
script:
- docker compose -p trinity -f infra/docker-compose.dev.yml up -d --build
- curl -fsS --retry 20 --retry-delay 3 --retry-all-errors https://api.dev.trinity.jostud.com/health
- curl -fsS --retry 20 --retry-delay 3 --retry-all-errors https://dev.trinity.jostud.com/api/health
rules:
- if: $CI_COMMIT_BRANCH == "develop"
deploy-prod:
stage: deploy
tags:
- prod
environment:
name: prod
url: https://trinity.jostud.com
before_script:
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
script:
- docker stack deploy --detach=false --with-registry-auth -c infra/docker-compose.prod.yml trinity
- test "$(docker service ls --format '{{.Image}}' | grep -c "$CI_COMMIT_SHORT_SHA")" = 3
- curl -fsS https://api.trinity.jostud.com/health
- curl -fsS https://trinity.jostud.com/api/health
- curl -fsS -o /dev/null https://docs.trinity.jostud.com/
after_script:
- docker logout "$CI_REGISTRY"
rules:
- if: $CI_COMMIT_BRANCH == "main"workflow only accepts branch pipelines. A merge request therefore has no separate pipeline: it shows its source branch's. A Git tag starts nothing.
The lint and test stages
The api:* and web:* jobs, defined in api/.gitlab-ci.yml and web/.gitlab-ci.yml. They run in a clean image (python:3.11-slim, node:22-alpine) and publish their reports:
| Report | Where to see it in GitLab |
|---|---|
JUnit (api/report.xml, web/junit.xml) | the Tests tab of the pipeline and the merge request |
Cobertura (api/coverage.xml, web/coverage/cobertura-coverage.xml) | line-by-line coverage in the merge request diff |
Percentage (web:test) | next to the job, and in the merge request |
Reports are published even if tests fail (when: always): that is exactly when you need them.
The build stage
api:build, web:build and docs:build build the production images and push them to the with the commit . How Docker-in-Docker works is in Docker, Compose and Swarm.
Why build on every push to develop and main, even without changes?
Prod runs api:<sha> and web:<sha> for the same commit. If only the web had changed and the api:<sha> image did not exist, the deployment would fail. This was not the case at first: see History.
The deploy stage
stages:
- lint
- test
- build
- deploy
workflow:
rules:
- if: $CI_COMMIT_BRANCH
include:
- local: api/.gitlab-ci.yml
- local: web/.gitlab-ci.yml
- local: docs/.gitlab-ci.yml
deploy-dev:
stage: deploy
tags:
- dev
environment:
name: dev
url: https://dev.trinity.jostud.com
script:
- docker compose -p trinity -f infra/docker-compose.dev.yml up -d --build
- curl -fsS --retry 20 --retry-delay 3 --retry-all-errors https://api.dev.trinity.jostud.com/health
- curl -fsS --retry 20 --retry-delay 3 --retry-all-errors https://dev.trinity.jostud.com/api/health
rules:
- if: $CI_COMMIT_BRANCH == "develop"
deploy-prod:
stage: deploy
tags:
- prod
environment:
name: prod
url: https://trinity.jostud.com
before_script:
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
script:
- docker stack deploy --detach=false --with-registry-auth -c infra/docker-compose.prod.yml trinity
- test "$(docker service ls --format '{{.Image}}' | grep -c "$CI_COMMIT_SHORT_SHA")" = 3
- curl -fsS https://api.trinity.jostud.com/health
- curl -fsS https://trinity.jostud.com/api/health
- curl -fsS -o /dev/null https://docs.trinity.jostud.com/
after_script:
- docker logout "$CI_REGISTRY"
rules:
- if: $CI_COMMIT_BRANCH == "main"deploy-dev
- The
trinity-devshell runner has already checked out the repository at the right commit. docker compose up -d --buildrebuilds the dev images and recreates the containers that changed.- Two :
curlon the API's/healthand the web's/api/health, through Traefik and HTTPS, with up to 20 attempts 3 s apart (time for the containers to start).
deploy-prod
- Log into the registry with the job's temporary token.
docker stack deploy --detach=false --with-registry-authupdates the stack and waits for Swarm to finish.- Checks that both
apiandwebservices run the commit's image. If Swarm rolled back, the image is the old one and the job fails. - Smoke tests on the public addresses.
- Log out of the registry, even if a step failed (
after_script).
The details are in Zero-downtime deployment.
Security in the pipeline
| Measure | Where |
|---|---|
No secret in the files: $POSTGRES_PASSWORD, $DJANGO_SECRET_KEY come from CI/CD variables | dev and prod Compose |
| Temporary registry token, valid for the job's duration | $CI_REGISTRY_PASSWORD |
docker login --password-stdin: the password does not show in the process list | *:build jobs, deploy-prod |
| No SSH deploy key: the runner is on the machine | shell runners |
| Non-root, multi-stage images | prod Dockerfiles |
All the details: Secrets and access.