Ravindra BagaleCourses & study guides मराठी

Chapter 92: GitLab CI Deep Dive

DevOps

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

  1. Make the folder practice-ci and the tests and web folders inside it.
  2. In app.py write a function add(a, b) that returns a + b.
  3. In requirements.txt write one line: pytest.
  4. In tests/test_app.py import add from app and write two tests: add(2, 3) == 5 and add(-1, 1) == 0.
  5. In web/price.js export price(qty, rate) returning qty * rate.
  6. In web/price.test.js use node:test and node:assert to check price(3, 40) === 120.
  7. Run python -m pytest -q in practice-ci. Expect 2 passed.
  8. Run node --test in practice-ci/web. Expect # pass 1.
  9. Create .gitlab-ci.yml with the content below.
  10. Check the YAML locally with the one-line Python check from lesson 13.
  11. Break one test on purpose, run pytest again, and read the failure.
  12. 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-deploy waits 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.

Did you understand? Let us try it once more.