GitLab CI/CD
Pipelines defined in .gitlab-ci.yml with stages, jobs, runners and environments, tightly integrated with GitLab's registry and security tools.
Intermediate · about 7 hours · difficulty 2/5
Learn first: Continuous Integration and Delivery
GitLab CI/CD ships inside GitLab, whether on gitlab.com or self-managed, and is common in companies that host their own source control.
Pipeline structure
- A single
.gitlab-ci.ymlat the repo root defines stages (build, test, deploy) and jobs assigned to them. - Jobs in the same stage run in parallel; stages run in order. needs builds a directed graph so jobs can start early.
rulesdecide when a job runs (branch, tag, merge request, changed paths).imagesets the container a job runs in;servicesstart sidecars such as a test database.cachespeeds up dependency installs;artifactspass build outputs to later jobs.
Runners
GitLab Runner executes jobs using executors such as Docker, Kubernetes or shell. Shared runners are fine to start; register your own for special hardware or network access.
Delivery features
- Environments and deployments track what is running where, with manual jobs (
when: manual) for approvals. - Built-in container registry and package registry.
- CI/CD variables at project, group or instance level, with masking and protection for protected branches.
includeand CI/CD components share pipeline templates across projects.- Security scanning templates (SAST, dependency, container) plug straight in.
Good enough to move on: you can write a multi-stage pipeline with rules, caching, a service container for tests and a manual production deploy, and share part of it through an include.
Key concepts
- .gitlab-ci.yml stages and jobs
- rules and needs
- Images and services
- Cache vs artifacts
- GitLab Runner executors
- Environments and manual jobs
- include and CI/CD components
Other options for this step
- GitHub Actions: Build pipelines as YAML workflows triggered by repository events, using jobs, reusable actions, secrets and OIDC to the cloud.
- Jenkins: The long-standing self-hosted automation server, with pipelines as code in Jenkinsfiles and a huge plugin ecosystem.
Mini project
Build a GitLab pipeline that tests against a Postgres service container, builds an image into the GitLab registry, and deploys to staging automatically and to production through a manual job.
Check yourself
What is the difference between cache and artifacts in GitLab CI, and which would you use for a compiled binary needed by the deploy job?
Free resources
- GitLab CI/CD documentation (GitLab)
- CI/CD YAML syntax reference (GitLab)