SECTION 04 · 25 MIN READ & EXPLORE
Understand pipelines, jobs, and stages
Read pipeline configuration and predict scheduling, variables, services, and artifact movement.
GitLab CI/CD
Pipelines, Jobs, Stages
- Pipeline: The top-level component of continuous integration, delivery, and deployment. It comprises:
- Jobs: Instructions that define what to do (e.g., compile code, run tests).
- Stages: Define when to run the jobs (e.g., build stage runs before test stage).
Example:
workflow:
name: "CAN 2025"
stages:
- build
- test
- deploy
build-job:
stage: build
script:
- echo "Compiling the code..."
test-job:
stage: test
script:
- echo "Running unit tests..."
deploy-job:
stage: deploy
script:
- echo "Deploying application..."
Scripts, before_script, after_script
- script: The only required keyword in a job. It defines the shell commands to execute.
- before_script: Commands that run before each job’s
script. Useful for setup (e.g., installing dependencies). - after_script: Commands that run after each job, regardless of success or failure. Useful for cleanup.
Example:
default:
before_script:
- echo "Global setup (e.g., install git)"
- apt update && apt install -y git
job1:
script:
- echo "Main script execution"
- ./run.sh
after_script:
- echo "Cleanup temporary files"
- ./cleanup.sh
Variables
Variables allow you to pass values to pipelines and jobs.
- Predefined Variables: Provided by GitLab (e.g.,
CI_COMMIT_SHA,CI_PROJECT_DIR,CI_PIPELINE_ID). - Custom Variables: Defined by you in the YAML or in the project settings (Settings > CI/CD > Variables).
- Where to define:
- Global: In the
.gitlab-ci.yml(available to all jobs). - Job-level: Inside a specific job.
- Project/Group Settings: For secrets and shared values.
- Global: In the
Example:
variables:
GLOBAL_VAR: "I am available everywhere"
job1:
variables:
JOB_VAR: "I am only for job1"
script:
- echo "Commit: $CI_COMMIT_SHA"
- echo "Global: $GLOBAL_VAR"
- echo "Job: $JOB_VAR"
Services
Services are additional Docker containers that run alongside your job’s container. They are linked to the job, allowing network access (e.g., a database for testing).
Example:
test-job:
image: python:3.9
services:
- postgres:13
variables:
POSTGRES_DB: test_db
POSTGRES_USER: user
POSTGRES_PASSWORD: password
script:
- python run_tests.py # Can connect to 'postgres' hostname
rules, retry, allow_failure
- rules: Determines when a job should be added to a pipeline (replaces
only/except). - retry: Automatically retries a job if it fails.
- allow_failure: If
true, the pipeline continues even if this job fails.
Example:
deploy-job:
script: deploy.sh
rules:
- if: $CI_COMMIT_BRANCH == "main"
retry: 2 # Retry up to 2 times
allow_failure: false
needs, dependencies
- needs: Executes jobs out-of-order (Directed Acyclic Graph). A job can start as soon as its
needsare finished, ignoring stage ordering. - dependencies: Restricts which artifacts are passed to a job. By default, all artifacts from previous stages are passed.
Example:
build-a:
stage: build
script: echo "Building A"
artifacts:
paths: [bin/a]
test-a:
stage: test
needs: ["build-a"] # Starts immediately after build-a finishes
dependencies: ["build-a"] # Only downloads artifacts from build-a
script: echo "Testing A"
Pause and predict
If one test job needs a database, should every job install and start that database?
Try your own explanation first. These notes stay on this page and are not saved or graded.
Show explanation
Declare a service for the job that needs it. Keep unit jobs independent where possible.
CI/CD: the course whiteboard
Experiment freely. Changes disappear when you reset or refresh.
Take a moment to explain the idea in your own words.