Ravindra BagaleCourses & study guides मराठी

Chapter 8: CI/CD Concepts

DevOps

8.8b Metrics of a healthy pipeline (plain English)

Why: the problem

A manager at an Infosys client asks: "Is our delivery getting better or worse?" The team says "I think it is fine". That is a feeling, not an answer. Google's DORA research team studied thousands of companies and found four simple numbers that show the health of delivery. How often do we deploy? How long from commit to production? How often does a deploy cause a problem? How fast do we recover? Add two pipeline numbers: how long the pipeline takes and how often it is red. With these six numbers, a team can see if it is getting faster and safer, or not. You do not need a fancy tool to start. Your CI tool already has the data. You will pull it out with a few commands.

What we will build

You will measure six numbers for the badge API: deployment frequency, lead time for changes, change failure rate, time to restore, pipeline duration and pipeline success rate. You will get them from the GitHub CLI and GitLab API with small scripts, and put them in a weekly table.

What: every term in plain English

  • Deployment frequency — How often you deploy to production. Daily is good; weekly is ok for many teams.
  • Lead time for changes — Time from commit to running in production.
  • Change failure rate — Percent of deploys that cause a problem and need a fix or rollback.
  • Time to restore (MTTR) — How long it takes to fix production after a problem.
  • Pipeline duration — How long one run takes. Under 10 minutes is a good target.
  • Success rate — Percent of runs that are green.
  • DORA — DevOps Research and Assessment, a research team at Google that defined the four key metrics.
  • Median — The middle value. Better than the average when a few runs are very slow.
List recent runs as JSON Count success rate Get pipeline durations Count prod deploys this week Log incidents Make the weekly table
Six numbers: frequency, lead time, failure rate, restore, duration, success

How: step by step

  1. List recent runs as JSON

    gh run list --workflow cicd.yml --limit 50 --json conclusion,createdAt,updatedAt,headSha > runs.json
    

    What it does: Raw data from your CI tool.

    Output

    (file runs.json)
    
  2. Count success rate

    jq "[.[]|select(.conclusion==\"success\")]|length" runs.json
    

    What it does: How many of the last 50 are green.

    Output

    44
    
  3. Get pipeline durations

    python3 metrics.py runs.json
    

    What it does: Example 1 script prints median duration.

    Output

    median duration: 6.4 min
    
  4. Count prod deploys this week

    gh api repos/ravi/badge-api/deployments -q "[.[]|select(.environment==\"production\")]|length"
    

    What it does: Each prod deploy job makes a deployment record.

    Output

    9
    
  5. Log incidents

    Add one line to incidents.csv for each prod problem
    

    What it does: Needed for change failure rate and time to restore.

    Output

    2026-10-21,a3f2c1d,35
    
  6. Make the weekly table

    python3 weekly.py
    

    What it does: Example 3 prints all six numbers.

    Output

    deploys 9  lead 3.1h  cfr 11%  mttr 35m
    

Example 1: Pipeline duration and success rate from CI data

Stack. GitHub CLI, Python

Goal. Pipeline duration and success rate from CI data.

metrics.py

import json, sys, statistics
from datetime import datetime

def t(s):
    return datetime.fromisoformat(s.replace("Z", "+00:00"))

runs = json.load(open(sys.argv[1]))
mins = [(t(r["updatedAt"]) - t(r["createdAt"])).total_seconds() / 60 for r in runs]
ok = sum(1 for r in runs if r["conclusion"] == "success")
print(f"runs: {len(runs)}")
print(f"success rate: {100 * ok / len(runs):.0f}%")
print(f"median duration: {statistics.median(mins):.1f} min")
print(f"slowest: {max(mins):.1f} min")

What each line does

  • json.load — Reads the list from gh run list.
  • updatedAt - createdAt — Time for one run.
  • statistics.median — The middle value, not fooled by one very slow run.
  • success rate — Green runs divided by all runs.

Green output

$ python3 metrics.py runs.json
runs: 50
success rate: 88%
median duration: 6.4 min
slowest: 19.2 min

Expected result. 88 percent green and 6.4 minutes is healthy. If success falls under 80 percent, look for flaky tests.

Example 2: Lead time: commit time to deploy time

Stack. GitHub CLI, GitLab API, Bash

Goal. Lead time: commit time to deploy time.

lead_time.sh

#!/usr/bin/env bash
set -euo pipefail
for sha in $(gh api repos/ravi/badge-api/deployments -q '.[] | select(.environment=="production") | .sha' | head -10); do
  c=$(git show -s --format=%ct "$sha")
  d=$(gh api repos/ravi/badge-api/deployments -q ".[] | select(.sha==\"$sha\") | .created_at" | head -1)
  d=$(date -d "$d" +%s)
  echo "$sha $(( (d - c) / 60 )) min"
done

GitLab version

curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
  "https://gitlab.example.com/api/v4/projects/42/deployments?environment=production&per_page=20" \
  | jq -r '.[] | [.sha[0:7], .created_at] | @tsv'

What each line does

  • git show -s --format=%ct — Commit time in seconds.
  • created_at — When the prod deploy happened.
  • (d - c) / 60 — Lead time in minutes.
  • GITLAB_TOKEN — A token in your shell env, never written in the script.

Green output

a3f2c1d 182 min
91be0aa 95 min
7c1e0b2 240 min
median lead time: 182 min (about 3 hours)

Expected result. Under one day is strong. If most time is waiting for approval, that is the place to improve.

Example 3: A weekly six-number table

Stack. Python

Goal. A weekly six-number table.

incidents.csv

date,sha,minutes_to_restore
2026-10-21,a3f2c1d,35

weekly.py

import csv
deploys = 9
lead_hours = 3.1
failed = list(csv.DictReader(open("incidents.csv")))
cfr = 100 * len(failed) / deploys
mttr = sum(int(r["minutes_to_restore"]) for r in failed) / max(len(failed), 1)
print("| metric | this week |")
print("|---|---|")
print(f"| deploys to prod | {deploys} |")
print(f"| lead time | {lead_hours} h |")
print(f"| change failure rate | {cfr:.0f}% |")
print(f"| time to restore | {mttr:.0f} min |")
print("| pipeline median | 6.4 min |")
print("| success rate | 88% |")

What each line does

  • incidents.csv — One line per prod problem caused by a deploy.
  • cfr — Failed deploys divided by all deploys.
  • mttr — Average minutes to restore.
  • max(len, 1) — Avoids divide by zero in a quiet week.

Green output

| metric | this week |
|---|---|
| deploys to prod | 9 |
| lead time | 3.1 h |
| change failure rate | 11% |
| time to restore | 35 min |
| pipeline median | 6.4 min |
| success rate | 88% |

Expected result. Paste this in the weekly team note. Watch the trend over weeks, not one number.

Verify: did it work?

  1. Check numbers by hand once

    Count prod deploys in the CI Environments page
    

    What it does: Should match the script.

    Output

    9 deployments this week
    
  2. Check one lead time

    Compare commit time and deploy time for one sha
    

    What it does: Should match the script within a minute.

    Output

    a3f2c1d: 182 min
    

When it is red 1

gh: Not Found (HTTP 404)

Cause. Wrong owner or repo name, or the token cannot read deployments.

Fix, step by step

  1. Check gh repo view works.
  2. Run gh auth status and check the token scopes.

When it is red 2

ZeroDivisionError: division by zero

Cause. No deploys or no runs in the period.

Fix, step by step

  1. Guard with max(count, 1).
  2. Or print "no data this week".

When it is red 3

Success rate 40 percent

Cause. Many red runs, often flaky tests or a broken main.

Fix, step by step

  1. Group failures by job name.
  2. Fix the top failing job first.

Cleanup

  1. Remove raw data files

    rm -f runs.json
    

    What it does: Keep only the weekly table.

    Output

    (no output)
    

Practice tasks

Easy Count green runs

Why: Start simple.

What to do: Count green runs of the last 20.

How:
  1. Use gh run list.

Check: You have a percent.

Medium Find the slowest job

Why: Fix the right thing.

What to do: List job durations of one run.

How:
  1. gh run view RUN_ID --json jobs

Check: You name the slowest job.

Hard Trend for four weeks

Why: See direction.

What to do: Make the table for four weeks.

How:
  1. Plot lead time.

Check: You can say better or worse.

Easy Say pipeline health metrics in two lines

Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.

What to do: Write two short lines about pipeline health metrics for a fresher who knows only Linux, Git and Docker basics.

How:
  1. Write the problem it solves.
  2. Write the one pipeline line or command you use for it.
  3. Read it aloud to a friend.

Check: A fresher understands it without asking a question.

Easy Keep a throwaway lab repo

Why: Practice needs a repo you can break. If it gets messy, you delete it and start again.

What to do: Make a small practice repo called badge-api with one Python file, one test and one Dockerfile.

How:
  1. mkdir badge-api and cd into it
  2. git init -b main
  3. Add app.py, test_app.py and Dockerfile from this chapter, then commit.

Check: git log shows one commit and pytest prints 1 passed.

Medium Do it in a second CI tool

Why: CI/CD is the idea. The tool is only the syntax. The same stages must work in GitHub Actions, GitLab CI and Jenkins.

What to do: Write the pipeline from this lesson in a second CI tool.

How:
  1. If you used GitHub Actions, write .gitlab-ci.yml or a Jenkinsfile now.
  2. Keep the same stage names and the same commands.
  3. Only the wrapper syntax should change.

Check: Same green result, same stage names, different tool.

Medium Break it on purpose

Why: You must know the red log by face. In a real outage there is no time to read slowly.

What to do: Cause one error from the "When it is red" part of this lesson and fix it.

How:
  1. Follow the error section.
  2. Copy the exact red line from the job log into your notes.
  3. Write the fix in one line.

Check: You fixed it using only the steps, without searching the web.

Medium Write a one-page runbook

Why: At 2 a.m. nobody remembers pipeline menus. A short page saves the on-call person.

What to do: Write a one-page runbook for this lesson.

How:
  1. Title, when to use, exact steps or pipeline lines, how to check, how to undo.

Check: A teammate can follow it alone.

Hard Explain it like an interview

Why: Interviewers want real stories, not definitions.

What to do: Record a 60 second answer about this lesson.

How:
  1. Say the problem, the pipeline lines you wrote, and one error you hit.

Check: Your answer has a real pipeline line and a real error message.

Hard Draw the flow top to bottom

Why: A drawing shows the order of work and which stage waits for which.

What to do: Draw this lesson as boxes from top to bottom.

How:
  1. Start at git push, go through each stage, end at the green run or the deployed app.

Check: Each box matches a real line in your job log.

Homework: try at home

Try at home

At home, pull the last 50 runs of your repo, compute success rate and median duration, and write a six-number table for one week.

Recap

  • Four DORA metrics plus pipeline time and success rate.
  • The data is already in your CI tool.
  • Use the median, watch trends.
  • Log incidents to get failure rate and restore time.
  • Improve the slowest part first.

FAQ

Is more deploys always better?

Small, frequent deploys are safer, but only with good tests.

Do I need a dashboard tool?

No. Start with a script and a table.

Who should see these numbers?

The whole team. They are not for blaming people.

What is elite performance?

DORA reports call on-demand deploys, under a day lead time and under an hour restore elite.

Should I track lines of code?

No. It says nothing about delivery health.

Interview questions

  1. What are DORA metrics?

    Deployment frequency, lead time for changes, change failure rate and time to restore service.

  2. How do you measure lead time?

    Time from commit to that commit running in production, using commit and deployment timestamps.

  3. How would you improve lead time?

    Smaller changes, faster pipeline, fewer manual waits, automatic deploys to staging.

  4. What does a high change failure rate tell you?

    Tests or reviews miss problems. Add tests, canaries and smaller changes.

  5. Why use median instead of average?

    Outliers like one stuck run distort averages.

Quick quiz

Q1. Name the four DORA metrics.

Deployment frequency, lead time, change failure rate, time to restore.

Q2. Why use the median?

One very slow run does not distort it.

Q3. What is change failure rate?

Percent of deploys that cause a problem.

Q4. What is a good pipeline time?

Under about 10 minutes.

Q5. Where does the data come from?

The CI tool runs and deployments APIs.

Did you understand? Let us try it once more.

Ravi Sir's Cards for this lesson

मराठी cards