1. A complete pipeline #
Lint, tests, build.
The file #
stages: [lint, test, build]
default:
image: python:3.12-slim
interruptible: true
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCH
lint:
stage: lint
script: python -m compileall -q .
test:
stage: test
cache:
key:
files: [requirements-dev.txt]
paths: [.cache/pip]
script:
- pip install -r requirements-dev.txt
- pytest --junitxml=rapport.xml
artifacts:
when: always
reports:
junit: rapport.xml
build:
stage: build
needs: [test]
script: tar czf boutique-$CI_COMMIT_SHORT_SHA.tar.gz app.py Dockerfile
artifacts:
paths: [boutique-*.tar.gz]
expire_in: 1 week
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCHdefaultsets the image and shared settings;variablesdefines global variables.workflow:rulesdecides whether a pipeline is created: here, a merge request pipeline, and no duplicate branch pipeline.needsstarts a job as soon as its dependencies finish, without waiting for the whole stage (directed acyclic graph, DAG).
2. Rules and conditions #
Running the right job at the right time.
rules #
| Clause | Example | Use |
|---|---|---|
| if | $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH | condition on variables |
| changes | [src/**/*, Dockerfile] | only if these files changed |
| exists | [Dockerfile] | only if a file exists |
| when | manual, delayed, never, on_failure | trigger mode |
Watch out :only/exceptare deprecated: userules, clearer and more powerful. Rules are evaluated in order and the first match wins.
3. Cache, artifacts and parallelism #
Going fast without mistakes.
Cache or artifact? #
| Criterion | cache | artifacts |
|---|---|---|
| Purpose | speed up (downloaded dependencies) | pass a result between jobs or keep it |
| Guarantee | best effort: may be missing | always available to later jobs |
| Examples | .cache/pip, node_modules | binary, JUnit report, coverage |
test:
parallel:
matrix:
- PYTHON: ["3.12", "3.13"]
image: python:$PYTHON-slim
script: [pip install -r requirements-dev.txt, pytest]
retry: { max: 2, when: [runner_system_failure, stuck_or_timeout_failure] }
timeout: 15mBest practice : Only retry infrastructure failures automatically: retrying a flaky test hides the problem instead of fixing it.
Commands to know #
Commands to know
glab ci lint --dry-run --ref main: simulate pipeline creationglab ci run -b main: run a pipelineglab ci retry <job-id>: retry a jobglab ci artifact main build: download artifacts
Sources #
Checked in November 2026: CI/CD platforms change every month, check versions and plans in the vendor's documentation.
