Chapter 38: CI/CD Introduction
MODULE 6 | MLOps
Chapter 38: CI/CD Introduction
Pichle chapters me tum ne API banayi, usay Docker me band kiya, aur experiments ka record rakha. Lekin har badlav ke baad test chalana, image build karna, check karna ye sab haath se karna thaka dene wala aur galti ka sabab hai. CI/CD ye saare kaam khud ba khud karwata hai: tum code GitHub par push karti ho, aur baaki sab robot sambhal leta hai.
38.1 CI aur CD ka matlab
38.2 Pehle Git aur GitHub ka chhota sa dohrao
CI/CD GitHub par chalta hai, to project GitHub repository me hona chahiye. Agar abhi tak nahi kiya to project folder me ye karo (repo pehle GitHub par bana lo):
TERMINAL
git init
git add .
git commit -m "AI Medical Assistant: pehla version"
git branch -M main
git remote add origin https://github.com/<tumhara-username>/<repo-ka-naam>.git
git push -u origin main
38.3 GitHub Actions: workflow ka naqsha
GitHub Actions GitHub ki wo sahulat hai jo tumhare repository me robot (computer) chalati hai. Tum ek YAML file me likhti ho ke "jab ye ho, to ye kaam karo". File ki jagah hamesha ye hoti hai:
FILE KI JAGAH (folder ke naam bilkul yehi)
.github/workflows/ci.yml
| Lafz | Matlab | Is workflow me |
|---|---|---|
| workflow | Poori automation (ek YAML file) | CI |
event (on) | Kabshuru ho | main parpushyapull request |
| job | Kaamon ka ekgroup, alag machine par | test, docker-build |
| runner | Womachinejis par job chalti hai | ubuntu-latest |
| step | Job ka ekqadam | "Install dependencies", "Run tests" |
uses | Kisitayyar actionko istemal karo | actions/checkout@v4 |
run | Apnaterminal commandchalao | pytest -q |
38.4 Workflow likhna: Automatic Testing aur Build
Ab apne AI Medical Assistant project (Chapter 40) ke liye ci.yml dekhte hain. Is me do jobs hain: pehla test chalata hai, doosra (sirf pehla pass hone par) Docker image banata aur chala kar check karta hai ke API zinda hai:
.github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- name: Install dependencies
run: pip install -r requirements.txt -r requirements-dev.txt
- name: Run tests
run: pytest -q
docker-build:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build image
run: docker build -t medical-assistant .
- name: Smoke test
run: |
docker run -d -p 8000:8000 --name ma medical-assistant
sleep 10
curl --fail http://localhost:8000/health
| Hissa | Kya karta hai |
|---|---|
on: push / pull_request | main branch parhar pushaurhar pull requestpar workflow chalao |
actions/checkout@v4 | Tumhara code runner parutaro(warna runner khali hota hai) |
actions/setup-python@v5 | Python 3.12lagao; cache: pip libraries ka downloadtezkarta hai |
pip install -r requirements.txt -r requirements-dev.txt | Runtimeaurtestingwali dono libraries install karo |
pytest -q | Saare tests chalao. Ek bhi fail ho tojob fail(laal) |
needs: test | docker-build jobtab hichale jab test pass ho |
docker build -t medical-assistant . | Chapter 36 wali imagerobot ke computer parbanao |
curl --fail .../health | Image se container chala kar**/health**check karo; jawab na aaye tofail |
VERSION NOTE
Actions ke naam ke aage @v4, @v5 unke versions hain, jo waqt ke sath naye aate rehte hain. Agar GitHub kisi purane version par warning de, to GitHub Docs / Marketplace me us action ka naya version dekh kar number badal do. Python ka version (3.12) wohi rakho jo tumhare computer aur Dockerfile me hai.
38.5 Chalane ka tareeqa aur kya nazar aata hai
-
Ye file apne repo me
.github/workflows/ci.ymlpar rakho. -
Code commit karo aur
git pushkaro. -
GitHub par apne repo ka Actions tab kholo. Tumhara workflow "CI" wahan chalta hua dikhega.
-
Workflow par click karo:
testaurdocker-buildjobs aur har step ki log dikhti hai. -
Hara tick = sab theek. Laal cross = koi step fail hua; us step par click kar ke error ki log parho.
Local computer par wohi test jo robot chalayega, tum pehle khud bhi chala sakti ho (ye asli output hai):
TERMINAL
pytest -q
OUTPUT
..... [100%]
5 passed, 1 warning in 1.60s
Agar local par ye "5 passed" nahi aata, to GitHub par bhi nahi aayega. Pehle local theek karo, phir push karo.
MISAAL: Actions tab me kya dikhta hai (asal shakal alag hogi, tick ke sath)
test Set up job OK |
|---|
test Run actions/checkout@v4 OK |
test Install dependencies OK |
test Run tests OK |
docker-build Build image OK |
docker-build Smoke test OK |
38.6 CD ki taraf pehla qadam
CI ke baad agla qadam CD hai: jaise hi main par naya theek code aaye, wo khud live ho jaye . Aam tareeqay:
| Tareeqa | Kaise kaam karta hai |
|---|---|
| Hosting ko GitHub se jodna | Railway, Render jese platforms tumhara repo dekhte hain aur har push par Dockerfile se naya build khud deploy kar dete hain |
| Tareeqa | Kaise kaam karta hai |
|---|---|
| GitHub Actions se deploy | Workflow me ek aur job jo image ko registry (Docker Hub, GHCR) par bhejti aur server ko batati hai |
| Hugging Face Spaces | Chhote ML demos ke liye mashhoor; app ko Space me chalata hai |
AAM GALTIYAN
-
Free hosting ki hadd: free plans me memory (RAM) aur waqt kam hota hai. Bahut bare model (maslan 1 GB ka Random Forest) wahan load hote hi app crash ho sakti hai (502 error). Ye code ki galti nahi, plan ki hadd hai. Signup se pehle plan ki shartein dekho, aur model chhota rakho.
-
Platforms ke free plans aur shartein badalti rehti hain ; deploy karne se pehle unka taaza page dekh lo.
SAB SE ZAROORI
Secrets kabhi YAML me mat likho. Agar deploy ke liye token ya password chahiye (maslan registry ka), to usay GitHub me Settings > Secrets and variables > Actions me rakho aur workflow me ${{ secrets.NAAM }} se istemal karo. Agar key GitHub par chali gayi, to usay foran badal do (revoke karo), sirf delete karna kaafi nahi.
38.7 README par status badge
Repo ke README.md me ek chhota badge lagao jo dikhaye ke CI abhi pass ho raha hai ya fail. Apna username aur repo ka naam badal kar ye line likho:
README.md

Ye hara (passing) ya laal (failing) nishan dikhata hai. Kisi ko bhi tumhara project dekh kar ek nazar me pata chal jata hai ke wo bharosemand hai.
38.8 Aam masle aur unka hal
| Masla | Wajah aur hal |
|---|---|
| Workflow chalta hi nahi | File ka rastabilkul.github/workflows/ hona chahiye aur extension .yml ya.yaml; branch ka naam on: se milta ho |
| YAML error | Indentation(spaces) galat hai. Tab ki jagahspacesuse karo; har level me 2 spaces |
ModuleNotFoundError CI me | Library requirements.txt me nahi likhi. Local par chal gaya kyunke tumharecomputer par install thi |
| Test local par pass, CI me fail | Python ya libraryversion alaghai, ya koi file repo me push hi nahi hui (maslanmodel/) |
| Model file nahi mili | .gitignore me ban gayi hai ya GitHub ki size hadd ki wajah se push nahi hui |
| Smoke test fail | Container ko shuru hone me waqt lagta hai (sleep badhao) ya CMD / port galathai |
38.9 Aam Galtiyan aur Hands-on
AAM GALTIYAN
-
Test likhe baghair CI banana (robot chalega, lekin jaanchega kuch nahi).
-
Local par test chalaye baghair push karna.
-
requirements.txtme versions na dena aur kabhi kabhi CI ka toot jana. -
Secrets ya
.envko repo me daal dena. -
CI ke laal cross ko nazar-andaz kar ke aage code lagate rehna.
Hands-on
-
Chapter 35 ke
iris_apiproject ke liye.github/workflows/ci.ymlbanao jo sirfpytestchalaye. -
Repo ko GitHub par push karo aur Actions tab me pehla hara tick dekho.
-
Jaan boojh kar ek test galat kar do (maslan
assert res.status_code == 201
), push karo aur laal cross wali log parho. Phir theek kar do.
- README me CI ka badge lagao.
KEY TAKEAWAYS
-
CI har badlav par khud test aur build karta hai; CD theek code ko khud release karta hai.
-
GitHub Actions ka workflow
.github/workflows/*.ymlme hota hai: event , jobs , steps , runner . -
usestayyar actions ke liye aurrunapne commands ke liye;needsjobs ki tarteeb tay karta hai. -
Automatic testing ke liye
pytest, automatic build ke liyedocker buildaurcurl --failwala smoke test . -
Secrets hamesha GitHub Secrets me, kabhi code ya YAML me nahi.