RousselTMLEARNING

DevOps

Writing a .gitlab-ci.yml: jobs, rules, cache and artifacts

Mastering .gitlab-ci.yml syntax: stages, jobs and images, rules and workflow, needs (DAG), cache and artifacts, test reports, parallel matrices, manual jobs, retry, timeout and interruptible.

1. A complete pipeline #

Lint, tests, build.

The file #

Staged pipeline
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_BRANCH
  • default sets the image and shared settings; variables defines global variables.
  • workflow:rules decides whether a pipeline is created: here, a merge request pipeline, and no duplicate branch pipeline.
  • needs starts 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 #

ClauseExampleUse
if$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCHcondition on variables
changes[src/**/*, Dockerfile]only if these files changed
exists[Dockerfile]only if a file exists
whenmanual, delayed, never, on_failuretrigger mode
Watch out : only/except are deprecated: use rules, 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? #

Criterioncacheartifacts
Purposespeed up (downloaded dependencies)pass a result between jobs or keep it
Guaranteebest effort: may be missingalways available to later jobs
Examples.cache/pip, node_modulesbinary, 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: 15m
Best 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 creation
  • glab ci run -b main : run a pipeline
  • glab ci retry <job-id> : retry a job
  • glab 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.

Related articles

GitLab CI/CD: concepts and architecture

Discover GitLab CI/CD: the DevSecOps platform, the .gitlab-ci.yml file, pipelines, stages and jobs, runners, …

GitLab runners: types, executors, installation and security

Running GitLab jobs: instance, group and project runners, docker, kubernetes and docker-autoscaler executors,…

Building and delivering with GitLab: registry, environments and deployments

Delivering with GitLab CI/CD: building and publishing images to the project registry, package registry, envir…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!