Skip to content

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 ​

JobRunnerWhen it runs
api:lint, api:testCI (docker)if api/ changed, on any branch
web:lint, web:testCI (docker)if web/ changed, on any branch
api:build, web:build, docs:buildCI (docker)on every push to develop or main
deploy-devdev deploy (shell, tag dev)on every push to develop
deploy-prodprod 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…linttestbuilddeploy
feature/x, only touches web/web:lintweb:test——
feature/x, touches api/ and web/bothboth——
develop (after a merge request)depends on filesdepends on filesbothdeploy-dev
main (after a merge request)depends on filesdepends on filesbothdeploy-prod

Triggers ​

yaml
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:

ReportWhere 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 ​

yaml
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 ​

  1. The trinity-dev shell runner has already checked out the repository at the right commit.
  2. docker compose up -d --build rebuilds the dev images and recreates the containers that changed.
  3. Two : curl on the API's /health and 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 ​

  1. Log into the registry with the job's temporary token.
  2. docker stack deploy --detach=false --with-registry-auth updates the stack and waits for Swarm to finish.
  3. Checks that both api and web services run the commit's image. If Swarm rolled back, the image is the old one and the job fails.
  4. Smoke tests on the public addresses.
  5. Log out of the registry, even if a step failed (after_script).

The details are in Zero-downtime deployment.

Security in the pipeline ​

MeasureWhere
No secret in the files: $POSTGRES_PASSWORD, $DJANGO_SECRET_KEY come from CI/CD variablesdev 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 machineshell runners
Non-root, multi-stage imagesprod Dockerfiles

All the details: Secrets and access.