Chapter 92: GitLab CI Deep Dive
92.15 Lab — a full practice repo you can paste
Story (fictional teaching scenario). Pooja from Kolhapur and Liam from Galway are learning GitLab CI without a GitLab account yet. They build the whole practice repo on their own computers, run every job's commands locally, and check the YAML. When they later get a GitLab project (a free personal one, or one your teacher gives you), they push the same files and watch the pipeline.
Files in the practice repo
practice-ci/
app.py
requirements.txt
tests/test_app.py
web/price.js
web/price.test.js
.gitlab-ci.yml
Step-by-step
- Make the folder
practice-ciand thetestsandwebfolders inside it. - In
app.pywrite a functionadd(a, b)that returnsa + b. - In
requirements.txtwrite one line:pytest. - In
tests/test_app.pyimportaddfromappand write two tests:add(2, 3) == 5andadd(-1, 1) == 0. - In
web/price.jsexportprice(qty, rate)returningqty * rate. - In
web/price.test.jsusenode:testandnode:assertto checkprice(3, 40) === 120. - Run
python -m pytest -qinpractice-ci. Expect2 passed. - Run
node --testinpractice-ci/web. Expect# pass 1. - Create
.gitlab-ci.ymlwith the content below. - Check the YAML locally with the one-line Python check from lesson 13.
- Break one test on purpose, run pytest again, and read the failure.
- Fix it. If you have a GitLab project, push and compare the pipeline with the stages below.
Practice .gitlab-ci.yml
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCH
stages: [test, build, deploy]
default:
image: python:3.13-slim
unit-test-py:
stage: test
script:
- pip install -r requirements.txt
- python -m pytest -q --junitxml=report.xml
artifacts:
when: always
reports:
junit: report.xml
expire_in: 1 week
unit-test-web:
stage: test
image: node:22-slim
script:
- cd web
- node --test
build-site:
stage: build
needs: ["unit-test-py", "unit-test-web"]
script:
- mkdir -p public
- echo "build $CI_COMMIT_SHORT_SHA" > public/version.txt
artifacts:
paths: [public/]
expire_in: 3 days
pretend-deploy:
stage: deploy
needs: ["build-site"]
environment:
name: practice
resource_group: practice
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
allow_failure: false
script:
- cat public/version.txt
- echo "pretend deploy finished - nothing real was changed"
What it covers: duplicate-free workflow:rules, stages, a default image, a Python job with a JUnit report, a Node job, needs, artifacts with expiry, an environment, resource_group and a manual deploy that changes nothing real.
What was tested for this lesson
Output (tested locally — the job's script was run on a practice computer, not in a GitLab pipeline)
python -m pytest -q -> 2 passed
node --test (in web/) -> # pass 1
YAML check -> YAML OK
The full pipeline itself was not run in GitLab for this chapter.
What good looks like
- Both test commands pass locally.
- The YAML check says OK.
- You can explain why
pretend-deploywaits for a click and why only one can run at a time. - No real secret, token or production address is anywhere in the repo.
Run every job's commands on your own computer first. If they fail there, they will fail in GitLab too, and you will fix them faster locally.
प्रत्येक job चे commands आधी स्वतःच्या computer वर चालवा. तिथे fail झाले तर GitLab मध्येही fail होतील, आणि local वर तुम्ही ते पटकन fix कराल.
हर job के commands पहले अपने computer पर चलाओ. वहाँ fail हुए तो GitLab में भी fail होंगे, और local पर आप उन्हें जल्दी fix कर लोगे.
Did you understand? Let us try it once more.