GitLab CI reference

GitLab CI reference

GitLab CI configuration lives in a single `.gitlab-ci.yml` at the repo root. Top-level keys cover global behaviour (`stages`, `variables`, `cache`, `default`, `workflow`, `include`); every other top-level key is a job. Each job has a `stage:`, a `script:` (the actual commands), and optional `rules:`, `needs:`, `cache:`, `artifacts:`, and `services:`.

Expression syntax uses `$CI_*` predefined variables and shell-style `$VAR` interpolation. Conditions in `rules.if` use a small DSL with `==`, `!=`, `||`, `&&`, and regex matchers `=~`.

Top-level keys

KeyPurpose
stagesOrdered list of stages; jobs run stage-by-stage.
variablesPipeline-level environment variables.
imageDefault Docker image for jobs.
servicesDefault sidecar containers (database, etc.).
defaultDefault values for image, before_script, after_script, cache, tags, retry, etc.
workflowPipeline-level rules deciding whether the pipeline runs.
includePull other YAML files (local, remote, project-level, template).
cacheJob-level (or global) cache definition.
<job>.scriptList of shell commands to run in the job.
<job>.before_script / after_scriptLines that run before / after the main script.
<job>.rulesWhen the job runs (replaces the older only/except).
<job>.parallelNumeric or matrix parallelism.
<job>.needsDAG-style cross-stage dependencies.
<job>.artifactsPaths to upload + retention.

Minimal example

stages: [test]

test:
  stage: test
  image: node:20
  cache:
    paths: [node_modules/]
  script:
    - npm ci
    - npm test

Context variables

The same concept goes by a different name on every platform. Use this table when porting expressions and conditions.

ConceptGitHub ActionsGitLab CICircleCIBitbucket
Branch namegithub.ref_name$CI_COMMIT_REF_NAME<< pipeline.git.branch >>$BITBUCKET_BRANCH
Commit SHAgithub.sha$CI_COMMIT_SHA<< pipeline.git.revision >>$BITBUCKET_COMMIT
Tag namegithub.ref_name$CI_COMMIT_TAG<< pipeline.git.tag >>$BITBUCKET_TAG
Repository sluggithub.repository$CI_PROJECT_PATH<< pipeline.project.git_url >>$BITBUCKET_REPO_FULL_NAME
Run / pipeline idgithub.run_id$CI_PIPELINE_ID<< pipeline.id >>$BITBUCKET_BUILD_NUMBER
Triggering eventgithub.event_name$CI_PIPELINE_SOURCE<< pipeline.trigger_source >>(implicit; check pipelines.* section)
Actor / usergithub.actor$GITLAB_USER_LOGIN$CIRCLE_USERNAME$BITBUCKET_STEP_TRIGGERER_UUID
Workspace pathgithub.workspace$CI_PROJECT_DIR$CIRCLE_WORKING_DIRECTORY$BITBUCKET_CLONE_DIR
PR / MR source branchgithub.head_ref$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME$CIRCLE_BRANCH (head ref)$BITBUCKET_PR_DESTINATION_BRANCH (target only)
PR / MR target branchgithub.base_ref$CI_MERGE_REQUEST_TARGET_BRANCH_NAME(not directly exposed)$BITBUCKET_PR_DESTINATION_BRANCH

Related Tools