Introduction
GitLab CI/CD is the pipeline that lives where the code lives — built into GitLab itself, so there is no separate CI server to install and no second tool to learn. If your code is in GitLab, your pipeline is one file away: .gitlab-ci.yml in the root of the repository. This tutorial builds a working pipeline from zero — test, build, deploy — and explains the concepts that transfer to every other CI tool. It sits alongside our Jenkins CI/CD Pipeline Tutorial and GitHub Actions Tutorial: the three tools cover the same ideas with different syntax, and learning any two makes the third easy.
Version note (26 Nov 2026): the current GitLab release is 19.6, released 19 November 2026, with the latest patch 19.6.2 on 24 November 2026. GitLab ships a new minor release every month, so the version number moves fast; the .gitlab-ci.yml syntax in this tutorial is stable across recent releases. If you run GitLab yourself rather than using GitLab.com, check you are on a supported release — 19.6, 19.5 and 19.4 were the maintained lines in late November 2026.
What GitLab CI/CD Is
A GitLab pipeline is a set of jobs organised into stages, defined in a YAML file, and run by runners — machines that execute the jobs. Push a commit, and GitLab reads .gitlab-ci.yml, creates a pipeline, and runs the jobs in stage order. The mental model is simple: stages run in sequence (test, then build, then deploy), jobs inside a stage run in parallel, and a failed job stops the pipeline unless you say otherwise.
The reason teams choose GitLab CI/CD is integration. The repository, the issues, the merge requests, the container registry and the pipeline are one product with one login, so a merge request can show the pipeline status, the test results and the review app for that branch in one place. The trade-off is the same as any all-in-one tool: if your code lives on GitHub, GitHub Actions is the shorter path, and if you need the most flexible self-hosted automation, Jenkins still wins. Our comparison in the Jenkins tutorial covers the decision in detail.
Your First Pipeline: a .gitlab-ci.yml That Tests Code
Create a project on GitLab.com with a small Node.js application, then add a file called .gitlab-ci.yml at the repository root:
stages:
- test
- build
test-job:
stage: test
image: node:22
script:
- npm ci
- npm test
build-job:
stage: build
image: node:22
script:
- npm run build
artifacts:
paths:
- dist/
Push it, and GitLab runs the pipeline automatically. A few things in that file deserve attention. image: node:22 tells the runner which Docker image to run the job in — GitLab CI jobs run inside containers by default, which is why the Docker knowledge from our Docker Tutorial for Beginners matters here. script is the list of commands the job runs. artifacts saves the dist/ directory so later jobs — or you, from the pipeline page — can download it; without artifacts, each job starts from a clean copy of the repository and nothing carries over.
Click into the pipeline in the GitLab interface and you can see each job’s log in real time. Break a test deliberately and push again: a pipeline you have watched fail is one you will trust when it passes, and reading a failing log is the skill this tutorial is really teaching.
Stages, Jobs and Rules
Stages are the pipeline’s skeleton. A typical pipeline has three or four: test, build, deploy, and sometimes review or security in between. Jobs in the same stage run in parallel; stages run one after another. If a job fails, the pipeline stops by default — the allow_failure: true setting marks a job (such as a lint check you are still rolling out) as non-blocking.
Rules decide when a job runs. The classic example is deploying only from the main branch:
deploy-job:
stage: deploy
script:
- echo "Deploying $CI_COMMIT_SHA"
rules:
- if: $CI_COMMIT_BRANCH == "main"
GitLab provides predefined variables for every job — CI_COMMIT_SHA, CI_COMMIT_BRANCH, CI_PROJECT_NAME — and you can use them in scripts and rules without defining anything. The older only/except keywords still work, but rules is the current way to express conditions, and it is what you should learn.
Needs lets a job start as soon as the job it depends on finishes, instead of waiting for its whole stage — useful when a deploy job only needs the build job, not the test job, to finish. Dependencies and needs are the two ways to pass artifacts between jobs, and needs is the one to reach for first.
Runners: Where Jobs Actually Run
A runner is the agent that executes jobs. GitLab.com provides shared runners — free, managed, and enough for learning and small projects. Self-managed GitLab needs its own runners, installed on a server or in Kubernetes, and registered with a token. The distinction matters in interviews: “the pipeline runs on a runner” is the correct answer to “where does this actually execute?”, and the follow-up is usually about isolation — each job runs in a fresh container, so jobs cannot interfere with each other through the filesystem.
Runner tags select which runners may run a job. A job with tags: [docker] only runs on runners carrying that tag, which is how teams route GPU jobs to GPU machines or deployment jobs to runners with network access to production. For this tutorial, the shared runners need no tags at all.
Variables and Secrets
Hard-coding a password into .gitlab-ci.yml is the mistake this section exists to prevent. GitLab provides CI/CD variables, set in the project under Settings, CI/CD, Variables: plain variables for configuration, and masked and protected variables for secrets. A protected variable is only available to pipelines running on protected branches — so a deployment token is not exposed to a pipeline triggered from a random feature branch.
Use variables in scripts like any shell variable:
deploy-job:
stage: deploy
script:
- echo "Deploying to $DEPLOY_ENV"
- ./deploy.sh --token "$API_TOKEN"
rules:
- if: $CI_COMMIT_BRANCH == "main"
Two habits to build now. First, mark every secret variable as masked, so it never appears in a job log. Second, never echo a variable that might be a secret while debugging — the mask only applies to the exact value, and a transformed version of it can leak. If a secret is ever exposed in a log, rotate it; deleting the log is not enough.
Environments, Review Apps and Deploying
A GitLab environment is a named deployment target — staging, production — with a URL, so the pipeline page shows what is deployed where, and who deployed it. Declare one on the deploy job:
deploy-production:
stage: deploy
script:
- ./deploy.sh production
environment:
name: production
url: https://app.example.com
rules:
- if: $CI_COMMIT_BRANCH == "main"
For merge requests, GitLab can create a review app: a temporary environment per branch, so reviewers click a link and see the change running, not just the code. It needs a deploy script that creates a unique URL per branch — usually with a cloud provider or a Kubernetes cluster — but even the concept is worth knowing for interviews, because it is the feature that makes GitLab pipelines feel different from a Jenkins job list.
Manual jobs are the last piece: add when: manual to a deploy job and the pipeline pauses until someone clicks “play”. Production deploys that require a human approval are the standard use case, and they are also how you keep a tutorial pipeline from deploying every time you push a typo fix.
Caching, Artifacts and Speed
Pipelines get slow in predictable ways, and the fixes are predictable too. Cache node_modules between jobs so npm ci does not re-download the world on every run:
cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
- node_modules/
Cache is a best-effort speed-up; artifacts are the reliable way to pass files between jobs, because artifacts are guaranteed to be there. The rule of thumb: cache for dependencies, artifacts for build outputs. And keep images small — node:22-alpine starts faster than node:22, and a pipeline that spends two minutes pulling an image before running a ten-second test is a pipeline nobody enjoys.
Troubleshooting a Failing Pipeline
The pipeline never starts. Check the file is named exactly .gitlab-ci.yml in the repository root, and that the YAML is valid — GitLab’s CI/CD editor has a “Validate” button, and a single indentation error is the most common cause of a pipeline that never appears.
A job fails with “image not found”. The image: name is wrong or the registry needs credentials. Check the image exists on Docker Hub with that exact tag.
A job cannot find a file from an earlier job. The file was not declared as an artifact, or the job used needs without the artifact. Remember: each job starts from a clean checkout.
A secret variable is empty in the job. It is probably protected, and the pipeline ran on an unprotected branch — or it is masked and the value contains characters that break masking. Check the variable’s settings, not the script.
The pipeline is slow. Look at the job durations in the pipeline view: long image pulls mean a smaller image or a cached one, long installs mean a cache, and long tests mean parallel jobs — split the test suite across jobs in the same stage.
FAQ
GitLab CI/CD vs GitHub Actions vs Jenkins — which should I learn? The one your target employers use. The concepts are identical — stages, jobs, runners, secrets — and the syntax is the only thing that changes. Learn one deeply and skim the other two; our GitHub Actions Tutorial and Jenkins CI/CD Pipeline Tutorial cover both.
Do I need my own runner to learn GitLab CI/CD? No. GitLab.com’s shared runners are free and run everything in this tutorial. Install your own runner only when you need a specific environment, a private network, or a GPU.
What is the difference between a pipeline and a job? A pipeline is one run of the whole .gitlab-ci.yml for one commit; a job is a single unit of work inside it. One push creates one pipeline containing many jobs.
Can GitLab CI/CD deploy to Kubernetes? Yes — GitLab has first-class Kubernetes integration, including an agent for Kubernetes that lets GitLab push deployments into a cluster securely. It is a common interview follow-up, and it pairs with our Kubernetes Tutorial for Beginners.
Conclusion
GitLab CI/CD rewards a simple habit: keep the pipeline file in the repository, keep secrets out of it, and make each job do one thing. The file you built here — test, build, deploy, with variables and rules — is the shape of almost every real pipeline, and the differences between tools are details you can learn in an afternoon once the shape is familiar. Push a change, watch it run, break it on purpose, and fix it: that loop is the whole skill.
—
—


