GitLab CI
GitLab CI is the pipeline engine built into GitLab. It reads .gitlab-ci.yml on every push and hands out the jobs to the runners.
The root file
GitLab only reads the file at the root. It includes those from api/ and web/:
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"| Block | Role |
|---|---|
stages | Step order: lint → test → build → deploy |
workflow.rules | A pipeline only for pushes to a branch (no duplicate for merge requests, nothing for tags) |
include | Adds the api:* and web:* jobs |
deploy-dev, deploy-prod | The deployments, explained in The pipeline |
Keywords used
| Keyword | What it does | Example in the project |
|---|---|---|
stage | Puts the job in a step | stage: test |
image | Docker image the job runs in | image: node:22-alpine |
script | The job's commands | npm run lint |
before_script / after_script | Before / after script | docker login / docker logout |
rules: - changes | Runs the job only if these files changed | web/**/* |
rules: - if | Runs the job under a condition | $CI_COMMIT_BRANCH == "main" |
tags | Picks the runner | tags: [prod] |
services | A side container during the job | docker:27.5.1-dind |
artifacts: reports | Files read by GitLab | junit: web/junit.xml |
coverage | Expression extracting the coverage % from the logs | /Lines\s*:\s*(\d+…)%/ |
cache | Keeps files from one pipeline to the next | the npm cache, keyed by package-lock.json |
environment | Links the job to a GitLab environment, with its URL | name: prod |
extends | Inherits from a job template | .web |
Variables provided by GitLab
| Variable | Example | Used to |
|---|---|---|
CI_COMMIT_BRANCH | main | choose the jobs |
CI_COMMIT_SHORT_SHA | 5a86221 | tag the images |
CI_REGISTRY | registry.gitlab.com | log into the registry |
CI_REGISTRY_IMAGE | registry.gitlab.com/epitech_gitlab/trinity-devops | name the images |
CI_REGISTRY_USER, CI_REGISTRY_PASSWORD | temporary token | log in, valid for the job's duration |
The project's secret variables (POSTGRES_PASSWORD, DJANGO_SECRET_KEY) are described in Secrets and access.
Where to see the results
| In GitLab | What you see |
|---|---|
| Build → Pipelines | Each pipeline, its stages and jobs, with the logs |
| A pipeline's Tests tab | The API and web JUnit reports |
| A merge request | Pipeline status, tests, coverage in the diff |
| Operate → Environments | The version deployed in dev and prod, and the button to redeploy an older version |
| Deploy → Container Registry | The api and web images and their tags |
rules: changes on a push
For a push to a branch, GitLab compares against the previous push. On the first push of a new branch, there is nothing to compare: every job with changes runs.