GitLab CI & Jenkins
GitHub Actions is not the only CI system you'll meet. GitLab CI/CD is built into GitLab and common in companies that self-host; Jenkins is the long-lived, self-hosted automation server behind a large share of enterprise and game-studio pipelines. The concepts from A Model of a CI Job Graph map onto both; only the spelling changes.
Concept map
| Concept | GitHub Actions | GitLab CI/CD | Jenkins (declarative) |
|---|---|---|---|
| definition file | .github/workflows/*.yml |
.gitlab-ci.yml |
Jenkinsfile (Groovy DSL) |
| unit of work | job (steps) | job (script) |
stage with steps |
| ordering | needs |
stages (all jobs of a stage before the next) or needs (DAG) |
stage order; parallel blocks |
| matrix | strategy.matrix |
parallel: matrix |
matrix { axes { … } } |
| run only on tags | on: push: tags |
rules: - if: $CI_COMMIT_TAG |
when { buildingTag() } or when { tag 'v*' } |
| cache | actions/cache |
cache: key: files: |
plugins (or workspace reuse on persistent agents) |
| pass files between jobs | artifacts actions | artifacts: paths: (+ needs/dependencies) |
stash/unstash, archiveArtifacts |
| secrets | secrets.X |
CI/CD variables (masked, protected) | Credentials + withCredentials |
| approval | environment reviewers | when: manual jobs, protected environments with approvals |
input step |
| OIDC to clouds | permissions: id-token: write |
id_tokens: |
plugins |
| the release itself | gh release create, release actions |
release: keyword (runs glab) |
sh steps calling your tools |
GitLab CI/CD
# .gitlab-ci.yml
stages: [test, build, release]
default:
image: node:24
workflow:
rules: # when does a pipeline exist at all?
- if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+/
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
test:
stage: test
cache:
key:
files: [package-lock.json] # key = hash of the lockfile
paths: [.npm/]
script:
- npm ci --cache .npm --prefer-offline
- npm test
build:
stage: build
rules:
- if: $CI_COMMIT_TAG
parallel:
matrix:
- TARGET: [linux-x86_64, linux-aarch64, windows-x86_64]
script:
- ./scripts/package.sh "${CI_COMMIT_TAG#v}" "$TARGET"
artifacts:
paths: [dist/]
expire_in: 1 week
release:
stage: release
image: registry.gitlab.com/gitlab-org/cli:latest # provides glab, which the release keyword runs
rules:
- if: $CI_COMMIT_TAG
needs: [build]
script:
- cd dist && sha256sum * > SHA256SUMS
release:
tag_name: $CI_COMMIT_TAG
name: Release $CI_COMMIT_TAG
description: ./NOTES.md # written by an earlier job (git-cliff) and passed as an artifactGitLab specifics worth knowing:
rulesdecide whether a job is in the pipeline (if:expressions on predefined variables such as$CI_COMMIT_TAG,$CI_COMMIT_BRANCH,$CI_PIPELINE_SOURCE).workflow:rulesdecides whether a pipeline runs at all, which avoids duplicate branch + merge-request pipelines.- Protected tags and protected variables: mark
v*tags as protected, and mark signing keys as protected variables. Protected variables are only exposed to pipelines on protected branches and tags, which is GitLab's equivalent of environment secrets. - The
release:keyword needs theglabCLI in the job (theregistry.gitlab.com/gitlab-org/cliimage provides it). If the tag doesn't exist, it is created (annotated only when you givetag_message). parallel: matrixtakes a list of variable hashes; each hash is expanded as a cross product, and the total may not exceed 200 jobs. The model below checks the example from GitLab's documentation.
from itertools import product
def gitlab_matrix(entries):
jobs = []
for entry in entries:
axes = {k: (v if isinstance(v, list) else [v]) for k, v in entry.items()}
for values in product(*axes.values()):
jobs.append(dict(zip(axes, values)))
if len(jobs) > 200:
raise ValueError(f"{len(jobs)} jobs: the limit is 200")
return jobs
docs_example = [
{"PROVIDER": "aws", "STACK": ["monitoring", "app1", "app2"]},
{"PROVIDER": ["gcp", "vultr"], "STACK": ["data", "processing"]},
]
jobs = gitlab_matrix(docs_example)
for job in jobs:
print("deploystacks: [" + ", ".join(job.values()) + "]")
print(len(jobs), "jobs")
try:
gitlab_matrix([{"OS": [f"os{i}" for i in range(15)], "ARCH": [f"a{i}" for i in range(14)]}])
except ValueError as e:
print(e)deploystacks: [aws, monitoring]
deploystacks: [aws, app1]
deploystacks: [aws, app2]
deploystacks: [gcp, data]
deploystacks: [gcp, processing]
deploystacks: [vultr, data]
deploystacks: [vultr, processing]
7 jobs
210 jobs: the limit is 200
The job names in the output use GitLab's format, job: [value, value]. Unlike GitHub's include, each list entry is its own independent cross product, so "aws only with these three stacks" is just a separate entry.
Jenkins
A declarative Jenkinsfile for the same release:
// Jenkinsfile
pipeline {
agent none
options {
disableConcurrentBuilds() // like a concurrency group
timeout(time: 30, unit: 'MINUTES')
}
stages {
stage('Test') {
agent { label 'linux' }
steps {
sh 'npm ci && npm test'
}
}
stage('Build') {
when { tag pattern: 'v\\d+\\.\\d+\\.\\d+.*', comparator: 'REGEXP' }
matrix {
axes {
axis {
name 'TARGET'
values 'linux-x86_64', 'linux-aarch64', 'windows-x86_64'
}
}
agent { label "${TARGET.startsWith('windows') ? 'windows' : 'linux'}" }
stages {
stage('Package') {
steps {
sh './scripts/package.sh "${TAG_NAME#v}" "$TARGET"'
stash name: "dist-${TARGET}", includes: 'dist/**'
}
}
}
}
}
stage('Release') {
when { buildingTag() }
agent { label 'linux' }
input { message 'Publish this release?' } // a human approves
steps {
unstash 'dist-linux-x86_64'
unstash 'dist-linux-aarch64'
unstash 'dist-windows-x86_64'
sh 'cd dist && sha256sum * > SHA256SUMS'
archiveArtifacts artifacts: 'dist/*', fingerprint: true
withCredentials([string(credentialsId: 'github-token', variable: 'GH_TOKEN')]) {
sh 'gh release create "$TAG_NAME" dist/* --verify-tag --notes-file NOTES.md'
}
}
}
}
}Jenkins specifics:
- Plugins are the platform. Git, pipelines, credentials, matrix, Docker agents: all plugins. Pin and update them deliberately; a Jenkins upgrade is a release in itself.
- Agents are usually persistent machines, so workspaces and caches survive between builds (fast, but builds can leak state into each other; clean workspaces for releases).
- Multibranch pipelines discover branches and tags and set
BRANCH_NAME/TAG_NAME; tag builds may need to be enabled explicitly in the branch source configuration. fingerprint: truerecords artifact checksums so Jenkins can trace which build produced a file used later, a built-in form of provenance.- Single quotes in
sh '…'pass$TARGETto the shell; Groovy double quotes would interpolate it in Groovy first, the Jenkins version of the script-injection problem from Permissions, OIDC & Pinning.
Test yourself
How do you limit a GitLab job to tag pipelines?
only: tags: truerules: - if: $CI_COMMIT_TAGon: tagswhen: tag
What does GitLab's release: keyword need in the job's image?
- git-cliff
- The
glabCLI - Node.js
- Nothing
In a Jenkinsfile, why prefer sh '... $TARGET' (single quotes)?
- Faster
- The shell expands the variable; Groovy double quotes would interpolate untrusted values into the script text first
- Required by declarative syntax
- Double quotes are invalid