Ravindra BagaleCourses & study guides मराठी

Chapter 81: DevOps Projects Part 11: GitHub Actions GitLab CI Node Matrix Docker OIDC Terraform Runner Releases

81.6 Project 82: GitLab CI pipeline with stages

Friends, GitLab CI uses .gitlab-ci.yml stages. Today we write build test deploy stages, use a cache, and read the pipeline graph.

What you will build in this project

  1. Create .gitlab-ci.yml.
  2. Define stages build test deploy.
  3. Cache node_modules.
  4. Run a pipeline and read jobs.

1. Start from a real company story

At Mindtree in Bengaluru, freestyle scripts on a laptop were the only CI and a Friday deploy skipped tests.

Rough cost: about ₹8 lakh in weekend hotfixes.

Kavya moved the team to GitLab stages with test before deploy.

Today we write that file.

stagesbuild test deploybuildnpm citestnpm testdeployonly main
Picture 1: .gitlab-ci.yml → stages build→test→deploy → pipeline graph

Ravindra Bagale's Tip

Students put deploy in the same stage as test so a failed test still deploys when jobs are parallel. Keep deploy in a later stage. Keep that habit.

2. Start and prepare

Step 1: Why GitLab CI

Why: Stages are clear. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: echo why. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 1: a1

Type this:

echo 'GitLab CI reads .gitlab-ci.yml and runs jobs in stages'

You will see:

GitLab CI reads .gitlab-ci.yml and runs jobs in stages

What it means: Why.

Example 2: a2

Type this:

mkdir -p /tmp/gl82/app && echo 'Today: stages, cache, rules for main deploy'

You will see:

Today: stages, cache, rules for main deploy

What it means: Scope.

Example 3: a3

Type this:

echo 'Later stage never starts if an earlier stage fails'

You will see:

Later stage never starts if an earlier stage fails

What it means: Gate.

Step 2: Stages header

Why: Order matters. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: printf. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 4: b1

Type this:

printf '%s\n' 'stages:' '  - build' '  - test' '  - deploy' > /tmp/gl82/app/.gitlab-ci.yml && cat /tmp/gl82/app/.gitlab-ci.yml

You will see:

stages:

What it means: Stages.

Example 5: b2

Type this:

echo 'Jobs in the same stage can run in parallel'

You will see:

Jobs in the same stage can run in parallel

What it means: Parallel.

Example 6: b3

Type this:

echo 'deploy stage waits for all test jobs'

You will see:

deploy stage waits for all test jobs

What it means: Wait.

Step 3: Build job

Why: npm ci. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: job. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 7: c1

Type this:

printf '%s\n' 'build_app:' '  stage: build' '  image: node:20-alpine' '  script:' '    - npm ci' '  artifacts:' '    paths: [ node_modules/ ]' '    expire_in: 1 hour' >> /tmp/gl82/app/.gitlab-ci.yml && tail -8 /tmp/gl82/app/.gitlab-ci.yml

You will see:

build_app:

What it means: Build.

Example 8: c2

Type this:

echo 'Artifacts pass node_modules to later jobs when needed'

You will see:

Artifacts pass node_modules to later jobs when needed

What it means: Artifacts.

Example 9: c3

Type this:

echo 'Prefer cache for node_modules; artifacts for build outputs'

You will see:

Prefer cache for node_modules; artifacts for build outputs

What it means: Prefer.

3. Build the main pieces

Step 4: Test job

Why: npm test. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: job. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 10: d1

Type this:

printf '%s\n' 'unit_test:' '  stage: test' '  image: node:20-alpine' '  script:' '    - npm ci' '    - npm test' >> /tmp/gl82/app/.gitlab-ci.yml && tail -6 /tmp/gl82/app/.gitlab-ci.yml

You will see:

unit_test:

What it means: Test.

Example 11: d2

Type this:

echo 'Add a second job lint in stage test for parallel linting'

You will see:

Add a second job lint in stage test for parallel linting

What it means: Lint tip.

Example 12: d3

Type this:

echo 'image: pins the Node major like setup-node did in Actions'

You will see:

image: pins the Node major like setup-node did in Actions

What it means: Image.

Step 5: Deploy job

Why: main only. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: rules. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 13: e1

Type this:

printf '%s\n' 'deploy_prod:' '  stage: deploy' '  image: alpine:3.20' '  script:' '    - echo Deploying $CI_COMMIT_SHA' '  rules:' '    - if: \'$CI_COMMIT_BRANCH == "main"\'' >> /tmp/gl82/app/.gitlab-ci.yml && tail -8 /tmp/gl82/app/.gitlab-ci.yml

You will see:

deploy_prod:

What it means: Deploy.

Example 14: e2

Type this:

echo 'rules replace older only/except keywords'

You will see:

rules replace older only/except keywords

What it means: rules.

Example 15: e3

Type this:

echo 'Add environment: production for the environments page'

You will see:

Add environment: production for the environments page

What it means: Env.

Step 6: Cache

Why: Speed installs. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: cache key. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 16: f1

Type this:

printf '%s\n' 'cache:' '  key: { files: [ package-lock.json ] }' '  paths: [ node_modules/ ]' > /tmp/gl82/CACHE.txt && cat /tmp/gl82/CACHE.txt

You will see:

key: { files: [ package-lock.json ] }

What it means: Cache.

Example 17: f2

Type this:

echo 'Put cache: at default or job level'

You will see:

Put cache: at default or job level

What it means: Where.

Example 18: f3

Type this:

echo 'Lockfile change busts the cache automatically'

You will see:

Lockfile change busts the cache automatically

What it means: Bust.

4. Automate and verify

Step 7: Pipeline graph

Why: UI. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: read. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 19: g1

Type this:

printf '%s\n' 'CI/CD -> Pipelines -> click the pipeline' 'Graph shows build then test then deploy columns' > /tmp/gl82/UI.txt && cat /tmp/gl82/UI.txt

You will see:

Graph shows build then test then deploy columns

What it means: UI.

Example 20: g2

Type this:

echo 'Red job stops later stages'

You will see:

Red job stops later stages

What it means: Red.

Example 21: g3

Type this:

printf '%s\n' 'Acceptance: test green then deploy on main' > /tmp/gl82/ACCEPT.txt && cat /tmp/gl82/ACCEPT.txt

You will see:

Acceptance: test green then deploy on main

What it means: Gate.

Step 8: Variables

Why: CI vars. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: settings. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 22: h1

Type this:

echo 'Settings -> CI/CD -> Variables for tokens'

You will see:

Settings -> CI/CD -> Variables for tokens

What it means: Vars.

Example 23: h2

Type this:

echo 'Mask and protect production variables'

You will see:

Mask and protect production variables

What it means: Mask.

Example 24: h3

Type this:

echo 'Never print masked variables in script echo'

You will see:

Never print masked variables in script echo

What it means: Habit.

Step 9: Needs tip

Why: DAG pipelines. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: optional. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 25: i1

Type this:

echo 'needs: [build_app] can start a job before the whole stage finishes'

You will see:

needs: [build_app] can start a job before the whole stage finishes

What it means: needs.

Example 26: i2

Type this:

echo 'Learn stages first; add needs when pipelines get wide'

You will see:

Learn stages first; add needs when pipelines get wide

What it means: Order.

Example 27: i3

Type this:

echo 'Parent-child pipelines come later for monorepos'

You will see:

Parent-child pipelines come later for monorepos

What it means: Later.

5. Harden and accept

Step 10: Story Mindtree

Why: Friday skip. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: story. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 28: j1

Type this:

printf '%s\n' 'At Mindtree in Bengaluru, Friday deploy skipped tests on a laptop script.' 'Kavya moved the team to GitLab stages with test before deploy.' > /tmp/gl82/STORY.txt && wc -l /tmp/gl82/STORY.txt

You will see:

2 /tmp/gl82/STORY.txt

What it means: Story.

Example 29: j2

Type this:

echo 'Rough cost: about ₹8 lakh in weekend hotfixes'

You will see:

Rough cost: about ₹8 lakh in weekend hotfixes

What it means: Cost.

Example 30: j3

Type this:

echo 'Kavya: PASS'

You will see:

Kavya: PASS

What it means: Sign-off.

Step 11: Proof

Why: File. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: grep stages. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 31: k1

Type this:

grep -E 'stage:|stages:' /tmp/gl82/app/.gitlab-ci.yml | head -10

You will see:

stages:\n  stage: build\n  stage: test\n  stage: deploy

What it means: Stages lines.

Example 32: k2

Type this:

test -f /tmp/gl82/ACCEPT.txt && echo 'accept ok'

You will see:

accept ok

What it means: accept.

Example 33: k3

Type this:

echo 'Next: GitLab runner on EC2 project 83'

You will see:

Next: GitLab runner on EC2 project 83

What it means: Bridge.

Step 12: Acceptance

Why: Staged pipeline green. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: final. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 34: z1

Type this:

echo 'Three stages defined'

You will see:

Three stages defined

What it means: Stages.

Example 35: z2

Type this:

echo 'Deploy rules main only'

You will see:

Deploy rules main only

What it means: Rules.

Example 36: z3

Type this:

echo 'Cache keyed on lockfile'

You will see:

Cache keyed on lockfile

What it means: Cache.

Check yourself

Q1. Who fixed Mindtree Friday skip?

Kavya

Q2. File name?

.gitlab-ci.yml

Q3. Three stages?

build test deploy

Q4. Cache key tip?

lockfile

Q5. Deploy branch?

main

Q6. Test before deploy?

yes

Friends, you ran a GitLab CI staged pipeline. Did you understand? Let us try it once more. Add a lint job in the test stage and prove it runs beside npm test.

Lab: try at home

  1. Write gitlab-ci.yml.
  2. Three stages.
  3. Cache.
  4. Pipeline graph.

Recap

  • Stages order jobs.
  • Cache speeds installs.
  • Deploy only main.
  • Rules beat only/except.
  • Artifacts between stages.