Skip to main content

GitLab CI

GitLab CI is GitLab's integrated CI/CD system, configured in a .gitlab-ci.yml file and executed by runners, with built-in environment tracking and merge request pipelines.

Understanding GitLab CI

What Is GitLab CI and How Is It Configured

GitLab CI runs pipelines defined in a .gitlab-ci.yml file at the repository root. Jobs are grouped into stages, run on registered runners, and report back to merge requests and environments. It is popular where self-hosted GitLab is required for compliance, because the whole toolchain stays inside your infrastructure.

Pipeline Flow

flowchart TD A[Push or merge request] --> B[Pipeline created] B --> C[Runner picks up job] C --> D[Job runs in container] D --> E[Artifacts and reports saved] E --> F[Environment updated] classDef trig fill:#F97316,color:#FFFFFF classDef run fill:#326CE5,color:#FFFFFF classDef out fill:#10B981,color:#FFFFFF class A,B trig class C,D run class E,F out

A Practical .gitlab-ci.yml

YAML
stages:
- build
- test
- scan
- deploy
variables:
IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
.node-base:
image: node:20-alpine
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules/
build-image:
stage: build
image: docker:24
services:
- docker:24-dind
script:
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
- docker build -t "$IMAGE" .
- docker push "$IMAGE"
unit-tests:
extends: .node-base
stage: test
script:
- npm ci
- npm test -- --coverage
artifacts:
when: always
reports:
junit: junit.xml
expire_in: 1 week
trivy-scan:
stage: scan
image:
name: aquasec/trivy:latest
entrypoint: [""]
script:
- trivy image --exit-code 1 --severity HIGH,CRITICAL "$IMAGE"
deploy-production:
stage: deploy
script:
- ./deploy.sh production "$CI_COMMIT_SHORT_SHA"
environment:
name: production
url: https://api.payments.example.in
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual

Concepts Worth Knowing

  • rules: decide when a job runs, replacing the older only and except keywords.
  • needs: builds a DAG so a job can start as soon as its dependencies finish, without waiting for the whole previous stage.
  • extends and include: reuse job templates and shared pipeline files across projects.
  • Protected environments: limit who and which branches may deploy.

Handy Commands

Bash
glab ci status
glab ci view
glab ci trace 1234567
glab ci retry 1234567

Troubleshooting

Symptom Check What to Look For
Job stuck pending Runner tags No runner with matching tags
YAML error CI Lint tool Indentation or invalid keywords
Job did not run rules: conditions Variable names and values
Cache not reused Cache key Key changes between runs
Tip

Validate with CI Lint in the project's CI/CD editor before pushing. It catches syntax and rules errors faster than push-and-see.

Remember

Use predefined variables such as $CI_COMMIT_SHA, $CI_COMMIT_SHORT_SHA, and $CI_REGISTRY_IMAGE instead of hardcoding values.

Security

Mark the production environment as protected and use protected variables, so only approved branches and users can deploy or read production secrets.

Common Mistake

Jobs without rules:, so every job runs on every pipeline, including deploys on feature branches. Gate deploy jobs to the default branch or tags explicitly.

Frequently Asked Questions

What is the benefit of GitLab CI environments tied to merge requests?

Because GitLab owns both Git and CI, a merge request can show deployment status and a review app link inline. External CI tools must surface that state through API integrations.

What is the difference between stages and needs in GitLab CI?

Stages run in order and each waits for the full previous stage. The needs keyword lets a job start as soon as the jobs it names are done, which shortens pipeline time.

How do I avoid copy-pasting job config?

Define hidden jobs starting with a dot and reuse them with extends, or share files across projects with include. Both keep pipelines consistent and easier to maintain.

Found a mistake, an outdated step or a broken link?