Your first GitLab pipeline

Stages, jobs and a runner, from a green pipeline to a red one and back.

Beginner15 min · lesson 1 of 17
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (19 files, 6 KB): glbasics.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/glbasics.tar.gz && tar -xzf glbasics.tar.gz, which creates ~/lab/glbasics/. SHA-256: dd8f4d165a2d07155248b4774611a504a9383c9306405ed8d8e1a9b62ca2074a

A GitLab project has a merge request button, a pipeline badge that is green most days, and a file called .gitlab-ci.yml that one person wrote two years ago. Nobody else quite knows what runs when you push. This course starts by making that file ordinary: what a pipeline is, where its jobs run, and how to read one that failed.

Then it treats the pipeline as something worth attacking, because it is. A pipeline holds deploy credentials, builds what you ship, and runs code that anyone with push access can change. Most of the course is about keeping those three facts under control.

The lab

Everything runs in containers inside a disposable Linux VM with Docker Engine. If you took the Docker or Jenkins courses, reuse that lab VM. If not, the first lesson of Docker for beginners ("What Docker is, and why") sets one up with a script that also installs the tools these labs call. GitLab needs memory: give the VM 4 CPUs and at least 6 GB, and in this lab GitLab itself settled at about 2.5 GB. Each lesson has a download link for its files, with the command that fetches them inside the VM into ~/lab/<lesson>/. Check the tools first:

ubuntu@secopslog-cicd:~/lab/glbasics · GitLab 19.5.3
$ docker compose version --short; for t in git jq curl openssl; do command -v $t >/dev/null && echo "$t: ok" || echo "$t: MISSING"; done
5.7.0 git: ok jq: ok curl: ok openssl: ok

If a line says MISSING, install it with sudo apt-get install -y git jq curl openssl. Then start GitLab. The first start pulls about 1.6 GB and then takes several minutes while GitLab configures itself, so expect to wait:

compose.yaml
# Lab GitLab: GitLab CE and one Runner, pinned by digest, published only on the VM's loopback.
name: lab-gitlab
services:
gitlab:
image: gitlab/gitlab-ce:19.5.3-ce.0@sha256:6863d156a1a477bd6f69fc674b671d0163cd8b06bf63bb17cc4a4a803b78b27c
container_name: lab-gitlab
hostname: gitlab
shm_size: 256m
environment:
GITLAB_ROOT_PASSWORD: ${GITLAB_ROOT_PASSWORD:?run ./up.sh}
# Small-instance settings: one Puma process, few Sidekiq threads, no bundled monitoring.
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://gitlab:8929'
puma['worker_processes'] = 0
sidekiq['concurrency'] = 5
prometheus_monitoring['enable'] = false
gitlab_kas['enable'] = false
gitlab_rails['usage_ping_enabled'] = false
gitlab_rails['gitlab_shell_ssh_port'] = 2222
gitlab_rails['initial_shared_runners_registration_token'] = nil
registry['enable'] = false
ports: ["127.0.0.1:8929:8929"]
volumes:
- lab-gitlab-config:/etc/gitlab
- lab-gitlab-data:/var/opt/gitlab
- lab-gitlab-logs:/var/log/gitlab
healthcheck:
test: ["CMD", "curl", "-fs", "-o", "/dev/null", "http://localhost:8929/users/sign_in"]
interval: 10s
start_period: 900s
retries: 6
runner:
image: gitlab/gitlab-runner:alpine-v19.5.3@sha256:25adb59339aebbee89050b2d3fd0d193a30c1860c70fd4d98d1c8b1e154914a3
container_name: lab-gitlab-runner
volumes:
- lab-runner-config:/etc/gitlab-runner
# Lab only: the Docker executor starts job containers through the host's Docker socket,
# which makes the runner root on this VM. The runner isolation lesson takes this apart.
- /var/run/docker.sock:/var/run/docker.sock
volumes:
lab-gitlab-config:
lab-gitlab-data:
lab-gitlab-logs:
lab-runner-config:
ubuntu@secopslog-cicd:~/lab/glbasics · GitLab 19.5.3
$ ./up.sh
runner lab-runner: online (tags: lab,docker) GitLab 19.5.3 is up on http://127.0.0.1:8929
$ docker compose ps --format "table {{.Service}}\t{{.Status}}"
SERVICE STATUS gitlab Up 3 minutes (healthy) runner Up 2 seconds

Two containers. gitlab is GitLab Community Edition 19.5.3, the free, self-managed edition, so every feature this course uses is one you can run yourself without a licence. runner is GitLab Runner, the separate program that actually runs jobs. up.sh created a random root password and an API token for root, both in .env, readable only by you. The course scripts talk to GitLab through that token. That is a lab shortcut: real automation uses a project or group access token with the smallest scope and an expiry, never a root token, and a real server runs over HTTPS, because job and runner tokens cross that connection.

Watch out
The GITLAB_OMNIBUS_CONFIG settings shrink GitLab for a lab: one Puma process, few Sidekiq threads, no bundled monitoring. Don't copy them to a server people depend on. The runner also mounts the host's Docker socket, which makes it root on the VM. The runner isolation lesson shows what that allows, and in production a runner never shares a host with GitLab.

Open the web UI

GitLab listens only on the VM's loopback address. With Lima, open http://127.0.0.1:8929 on your computer, because Lima forwards loopback ports by default. With Multipass, forward the port over SSH as the Jenkins course shows, using port 8929. Sign in as root with the password from grep GITLAB_ROOT_PASSWORD .env. Each project has a Build menu with Pipelines and Jobs, and that's where the logs from this course live.

A first pipeline

The sample app is one function and one test. The pipeline is the file next to them:

app/.gitlab-ci.yml
# Two stages: a fast lint, then the tests. Every job runs in a fresh container from this image.
default:
image: secopslog-py:3.15
stages:
- lint
- test
lint:
stage: lint
script:
- ruff check .
test:
stage: test
script:
- python -m pytest -q

A job is a name with a script: shell commands run in order, and the first one that exits non-zero fails the job. stages put jobs in order: every job in lint must succeed before any job in test starts. default: image says which container image every job runs in, here a Python image with pytest and ruff already installed, built by up.sh so jobs don't download anything. seed.sh creates the project and pushes the folder, and the push starts the pipeline. pipeline.sh waits for it and prints each job's log, without the runner's preparation lines:

ubuntu@secopslog-cicd:~/lab/glbasics · GitLab 19.5.3
$ ./seed.sh app app/
pushed app: ba2a3be first commit
$ ./pipeline.sh app/
--- job lint (lint): success $ ruff check . All checks passed! --- job test (test): success $ python -m pytest -q . [100%] 1 passed in 0.03s pipeline #1: success

Each job ran in a fresh container. The runner keeps a working copy of the repository between jobs, but before each job it checks out the commit and deletes every file Git doesn't track (git clean -ffdx by default), so the lint job's leftovers were gone before test started. Nothing carries over from one job to the next unless you ask for it, which is the subject of the artifacts lesson. The API has the same facts in a form scripts can use:

ubuntu@secopslog-cicd:~/lab/glbasics · GitLab 19.5.3
$ ./gl.sh '/projects/root%2Fapp/pipelines?per_page=1' | jq -r '.[0] | "pipeline #\(.id) \(.status) source=\(.source) ref=\(.ref)"'; ./gl.sh '/projects/root%2Fapp/jobs?per_page=5' | jq -r '.[] | " \(.stage)/\(.name) \(.status) \(.duration | floor)s runner=\(.runner.description)"'
pipeline #1 success source=push ref=main test/test success 8s runner=lab-runner lint/lint success 16s runner=lab-runner

source=push says why the pipeline exists. Later lessons meet other sources (merge_request_event, api, schedule), and rules can treat them differently.

Break it

Change + to - and push, as someone in a hurry might:

ubuntu@secopslog-cicd:~/lab/glbasics · GitLab 19.5.3
$ sed -i 's/a + b/a - b/' app/calc.py && ./commit.sh app/ 'change add'
pushed: 4718d68 change add to main
$ ./pipeline.sh app/
--- job lint (lint): success $ ruff check . All checks passed! --- job test (test): failed $ python -m pytest -q F [100%] =================================== FAILURES =================================== ___________________________________ test_add ___________________________________ def test_add(): > assert add(2, 2) == 4 E assert 0 == 4 E + where 0 = add(2, 2) test_calc.py:5: AssertionError =========================== short test summary info ============================ FAILED test_calc.py::test_add - assert 0 == 4 + where 0 = add(2, 2) 1 failed in 0.01s ERROR: Job failed: exit code 1 pipeline #2: failed

Read a failed pipeline from the bottom up. ERROR: Job failed: exit code 1 is the runner reporting that the script failed. Above it, pytest says what failed: assert 0 == 4. lint still passed, because the code is valid Python. It is only wrong. Fix it:

ubuntu@secopslog-cicd:~/lab/glbasics · GitLab 19.5.3
$ sed -i 's/a - b/a + b/' app/calc.py && ./commit.sh app/ 'restore add' && ./pipeline.sh app/ | tail -1
pushed: 580a9db restore add to main pipeline #3: success

Hands-on

Start again with a fresh project: ./seed.sh app app/ deletes the project and its history and pushes the folder again. Add a second job, format, to the lint stage that runs ruff format --check ., and change calc.py to def add(a,b): (no space after the comma). Push both with ./commit.sh app/ "format job" and run ./pipeline.sh app/. Which jobs ran, which failed, and what happened to test? Finish with ./down.sh. A worked version is in solutions/.

What you should see: two jobs ran in the lint stage. lint passed with All checks passed!, because the code is still valid. format failed, and ruff format --check printed the diff it wants, def add(a,b): becoming def add(a, b):. test was skipped: a failed job in one stage stops the later stages, so the tests never ran. The pipeline failed even though nothing was functionally wrong, which is what a format check is for.

Remember

A job is a script in a fresh container, and a stage waits for the one before it. Read failures from the bottom up: the runner's ERROR line, then the tool's own message above it.

Quick check
01A pipeline has stages lint and test. The only lint job fails. What happens to the test jobs?
Incorrect — Separate containers do not change the order rules. A failed stage stops the stages after it.
Incorrect — Nothing is hidden. They do not run at all.
Correct — A later stage starts only after the earlier one succeeds. That is what stages are for: cheap checks first, and no time spent on tests when they already failed.
Incorrect — They do not wait. The pipeline ends as failed, and a retry starts the rest again.
02A job runs pip install -r requirements.txt and then pytest. The next job, in a later stage, runs pytest again and fails with "No module named flask". Why?
Correct — Every job starts from its image. Jobs share nothing by default. Install in each job, bake the tools into the image, or pass files on as artifacts.
Incorrect — A different runner could matter, but the cause here is simpler: nothing carries over between jobs on any runner.
Incorrect — Both jobs run the same image as the same user. The installed files were in the first job's container.
Incorrect — Nothing was cached in the first place. Caching has to be configured.
03Where do the commands in a job's script run?
Incorrect — GitLab schedules jobs. It does not run them.
Incorrect — Jobs never run on the developer's machine.
Incorrect — Runners are chosen by tags and availability, not network speed.
Correct — GitLab Runner is a separate program, and its executor decides how the job runs: here, a fresh container per job.

Related