.gitlab-ci.yml, Stages & Jobs
GitLab CI/CD is built into GitLab: a .gitlab-ci.yml at the repository root defines the pipeline, and runners execute its jobs. Unlike GitHub Actions, you can run GitLab pipelines on your own machine with gitlab-ci-local, so every session in this section is a real pipeline run. It uses the shell executor (jobs run in the session's shell); jobs with an image: need Docker and are shown as non-run YAML.
Note: gitlab-ci-local needs a Git repository with a commit and an
originremote to fill in variables such asCI_PROJECT_PATH. Each session sets that up in three lines at the start; nothing is ever pushed.
Structure
| Keyword | Meaning |
|---|---|
stages |
the ordered list of stages; jobs of one stage run in parallel, the next stage starts when all of them succeeded |
a top-level key with script: |
a job (any name except reserved words such as stages, variables, default, include, workflow) |
stage |
which stage the job belongs to (default: test) |
script, before_script, after_script |
the commands; after_script runs even if script failed |
variables |
environment variables, at top level or per job |
default |
defaults for every job (image, before_script, retry, tags, …) |
allow_failure |
a failure shows a warning but doesn't fail the pipeline |
tags |
which runners may take the job |
image, services |
the container image and sidecar containers (Docker/Kubernetes executors) |
A first pipeline
mkdir shop && cd shop && git init -q
git commit -q --allow-empty -m "init"
git remote add origin https://gitlab.example.com/acme/shop.git
git update-ref refs/remotes/origin/main HEAD && git remote set-head origin main
cat > .gitlab-ci.yml <<'YML'
stages: [lint, test, build]
variables:
APP_NAME: shop
default:
before_script:
- echo "job $CI_JOB_NAME in stage $CI_JOB_STAGE on $CI_COMMIT_BRANCH"
lint:
stage: lint
script:
- test -z "$(grep -rn 'print(' --include='*.py' . || true)" && echo "no debug prints"
unit:
stage: test
script:
- echo "testing $APP_NAME in project $CI_PROJECT_PATH"
style-report:
stage: test
allow_failure: true
script:
- echo "3 style warnings"; exit 1
package:
stage: build
script:
- echo "packaging $APP_NAME at ${CI_COMMIT_SHORT_SHA}"
after_script:
- echo "cleanup runs even after failures"
YML
git add . && git commit -qm "ci: first pipeline"
gitlab-ci-local --concurrency 1
echo "exit $?"parsing and downloads finished in 47 ms.
json schema validated in 204 ms
lint starting shell (lint)
lint $ echo "job $CI_JOB_NAME in stage $CI_JOB_STAGE on $CI_COMMIT_BRANCH"
lint > job lint in stage lint on main
lint $ test -z "$(grep -rn 'print(' --include='*.py' . || true)" && echo "no debug prints"
lint > no debug prints
lint finished in 15 ms
unit starting shell (test)
unit $ echo "job $CI_JOB_NAME in stage $CI_JOB_STAGE on $CI_COMMIT_BRANCH"
unit > job unit in stage test on main
unit $ echo "testing $APP_NAME in project $CI_PROJECT_PATH"
unit > testing shop in project acme/shop
unit finished in 8.63 ms
style-report starting shell (test)
style-report $ echo "job $CI_JOB_NAME in stage $CI_JOB_STAGE on $CI_COMMIT_BRANCH"
style-report > job style-report in stage test on main
style-report $ echo "3 style warnings"; exit 1
style-report > 3 style warnings
style-report finished in 9.98 ms WARN 1
package starting shell (build)
package $ echo "job $CI_JOB_NAME in stage $CI_JOB_STAGE on $CI_COMMIT_BRANCH"
package > job package in stage build on main
package $ echo "packaging $APP_NAME at ${CI_COMMIT_SHORT_SHA}"
package > packaging shop at 2b122c10
package Running after script...
package $ echo "cleanup runs even after failures"
package > cleanup runs even after failures
package finished in 18 ms
PASS lint
PASS unit
PASS package
WARN style-report pre_script
> job style-report in stage test on main
> 3 style warnings
pipeline finished in 377 ms
exit 0
What the run shows:
stagesran in order; jobs in the same stage (unit,style-report) would run in parallel on real runners (--concurrency 1serialises them here so the log is readable).default.before_scriptran before every job's script; predefined variables (CI_JOB_NAME,CI_COMMIT_BRANCH,CI_PROJECT_PATH,CI_COMMIT_SHORT_SHA) were filled in from the repository.style-reportfailed but is markedWARN, and the pipeline still succeeded (exit 0), because ofallow_failure: true.
A failing job stops the pipeline
mkdir shop && cd shop && git init -q
git commit -q --allow-empty -m "init"
git remote add origin https://gitlab.example.com/acme/shop.git
git update-ref refs/remotes/origin/main HEAD && git remote set-head origin main
cat > .gitlab-ci.yml <<'YML'
stages: [test, build]
unit:
stage: test
script:
- python3 -c 'assert 1 + 1 == 3, "math is broken"'
package:
stage: build
script:
- echo "never reached"
YML
git add . && git commit -qm "ci"
gitlab-ci-local --concurrency 1
echo "exit $?"parsing and downloads finished in 47 ms.
json schema validated in 199 ms
unit starting shell (test)
unit $ python3 -c 'assert 1 + 1 == 3, "math is broken"'
unit > Traceback (most recent call last):
unit > File "<string>", line 1, in <module>
unit > AssertionError: math is broken
unit finished in 26 ms FAIL 1
FAIL unit
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> AssertionError: math is broken
pipeline finished in 346 ms
exit 1
package never started: a failed job in one stage stops the later stages (unless a later job uses when: on_failure or when: always). gitlab-ci-local exits with 1, so you can use it in a pre-push hook.
What a real .gitlab-ci.yml adds
On GitLab's runners, jobs usually run in containers:
default:
image: python:3.13-slim # every job runs in this image unless it sets its own
interruptible: true # a newer pipeline on the same branch may cancel this one
retry:
max: 1
when: [runner_system_failure, stuck_or_timeout_failure] # retry infrastructure problems, not test failures
unit:
stage: test
script:
- pip install -r requirements.txt
- pytest -q --junitxml=report.xml
artifacts:
when: always
reports:
junit: report.xml # failed tests appear in the merge requestgitlab-ci-local --list shows what will run without running it, and gitlab-ci-local --preview prints the fully expanded configuration (with includes and extends resolved); both appear in the next lessons.
Test yourself
Two jobs are in stage test. When does a job in stage build start (without needs)?
- Immediately
- When every job in
testhas succeeded - When the first test job succeeds
- After a manual click
A job with allow_failure: true fails. What happens to the pipeline?
- It fails
- It continues and can still succeed; the job shows a warning
- It is retried
- It is cancelled
Which script section runs even when script fails?
- before_script
- after_script
- default
- variables