مین مواد پر جائیں

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
LafzMatlabIs workflow me
workflowPoori automation (ek YAML file)CI
event (on)Kabshuru homain parpushyapull request
jobKaamon ka ekgroup, alag machine partest, docker-build
runnerWomachinejis par job chalti haiubuntu-latest
stepJob ka ekqadam"Install dependencies", "Run tests"
usesKisitayyar actionko istemal karoactions/checkout@v4
runApnaterminal commandchalaopytest -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
HissaKya karta hai
on: push / pull_requestmain branch parhar pushaurhar pull requestpar workflow chalao
actions/checkout@v4Tumhara code runner parutaro(warna runner khali hota hai)
actions/setup-python@v5Python 3.12lagao; cache: pip libraries ka downloadtezkarta hai
pip install -r requirements.txt -r requirements-dev.txtRuntimeaurtestingwali dono libraries install karo
pytest -qSaare tests chalao. Ek bhi fail ho tojob fail(laal)
needs: testdocker-build jobtab hichale jab test pass ho
docker build -t medical-assistant .Chapter 36 wali imagerobot ke computer parbanao
curl --fail .../healthImage se container chala kar**/health**check karo; jawab na aaye to
fail
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​

  1. Ye file apne repo me .github/workflows/ci.yml par rakho.

  2. Code commit karo aur git push karo.

  3. GitHub par apne repo ka Actions tab kholo. Tumhara workflow "CI" wahan chalta hua dikhega.

  4. Workflow par click karo: test aur docker-build jobs aur har step ki log dikhti hai.

  5. 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:

TareeqaKaise kaam karta hai
Hosting ko GitHub se jodnaRailway, Render jese platforms tumhara repo dekhte hain aur har push par Dockerfile se naya build khud deploy kar dete hain
TareeqaKaise kaam karta hai
GitHub Actions se deployWorkflow me ek aur job jo image ko registry (Docker Hub, GHCR) par bhejti aur server ko batati hai
Hugging Face SpacesChhote 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​
![CI](https://github.com/<username>/<repo>/actions/workflows/ci.yml/badge.svg)

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​

MaslaWajah aur hal
Workflow chalta hi nahiFile ka rastabilkul.github/workflows/ hona chahiye aur extension .yml ya
.yaml; branch ka naam on: se milta ho
YAML errorIndentation(spaces) galat hai. Tab ki jagahspacesuse karo; har level me 2 spaces
ModuleNotFoundError CI meLibrary requirements.txt me nahi likhi. Local par chal gaya kyunke tumhare
computer par install thi
Test local par pass, CI me failPython ya libraryversion alaghai, ya koi file repo me push hi nahi hui (maslan
model/)
Model file nahi mili.gitignore me ban gayi hai ya GitHub ki size hadd ki wajah se push nahi hui
Smoke test failContainer ko shuru hone me waqt lagta hai (sleep badhao) ya CMD / port galat
hai

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.txt me versions na dena aur kabhi kabhi CI ka toot jana.

  • Secrets ya .env ko repo me daal dena.

  • CI ke laal cross ko nazar-andaz kar ke aage code lagate rehna.

Hands-on​

  • Chapter 35 ke iris_api project ke liye .github/workflows/ci.yml banao jo sirf pytest chalaye.

  • 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/*.yml me hota hai: event , jobs , steps , runner .

  • uses tayyar actions ke liye aur run apne commands ke liye; needs jobs ki tarteeb tay karta hai.

  • Automatic testing ke liye pytest, automatic build ke liye docker build aur curl --fail wala smoke test .

  • Secrets hamesha GitHub Secrets me, kabhi code ya YAML me nahi.