Chapter 38: CI/CD Introduction
MODULE 6 | MLOps
Chapter 38: CI/CD Introduction
In the earlier chapters you built an API, packed it in Docker, and kept a record of experiments. But running tests, building the image and checking after every change by hand is tiring and a cause of mistakes. CI/CD gets all of this done automatically : you push the code to GitHub , and a robot takes care of everything else.
TOPIC CHECKLIST (tick them off as you go)
-
☐ The meaning of CI and CD
-
☐ The basic GitHub Actions workflow (event, job, step, runner)
-
Automatic testing (
pytest) -
☐
-
Automatic build (Docker image + smoke test)
-
☐
-
☐ The way of CD and the importance of
Secrets -
☐ Common problems and a status badge on the README
38.1 The Meaning of CI and CD
38.2 A Quick Refresher on Git and GitHub
CI/CD runs on GitHub, so the project must be in a GitHub repository . If you have not done this yet, do this in the project folder (create the repo on GitHub first):
TERMINAL
git init
git add .
git commit -m "AI Medical Assistant: pehla version"
git branch -M main
git remote add origin https://github.com/<your-username>/<your-repo-name>.git
git push -u origin main
38.3 GitHub Actions: Map of a Workflow
GitHub Actions is the GitHub facility that runs a robot (computer) in your repository. You write in a YAML file : "when this happens, do this work". The file's location is always this:
FILE LOCATION (the folder names must be exactly these)
.github/workflows/ci.yml
| Term | Meaning | In this workflow |
|---|---|---|
| workflow | The whole automation (one YAML file) | CI |
event (on) | Whenit starts | Pushorpull requeston main |
| job | Agroupof tasks, on a separate machine | test, docker-build |
| runner | Themachineon which the job runs | ubuntu-latest |
| step | Onestepof a job | "Install dependencies", "Run tests" |
uses | Use aready-made action | actions/checkout@v4 |
run | Run your ownterminal command | pytest -q |
38.4 Writing the Workflow: Automatic Testing and Build
Now let's look at ci.yml for our AI Medical Assistant project (Chapter 40). It has two jobs : the first runs the tests , and the second (only if the first passes) builds the Docker image and runs it to check that the API is alive:
.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
| Part | What it does |
|---|---|
on: push / pull_request | Run the workflow onevery pushandevery pull requeston the mainbranch |
actions/checkout@v4 | Downloadyour code onto the runner (otherwise the runner is empty) |
actions/setup-python@v5 | Set upPython 3.12; cache: pip makes downloading librariesfaster |
pip install -r requirements.txt -r requirements-dev.txt | Install both theruntimeandtestinglibraries |
pytest -q | Run all the tests. If even one fails, thejob fails(red) |
needs: test | The docker-build job runsonly whentest passes |
docker build -t medical-assistant . | Build the Chapter 36 imageon the robot's computer |
curl --fail .../health | Run the container from the image and check**/health; if there is noresponse, itfails** |
VERSION NOTE
The @v4, @v5 after the names of actions are their versions , and new ones keep coming over time. If GitHub warns about an old version, look up the new version of that action in the GitHub Docs / Marketplace and change the number. Keep the Python version (3.12) the same as on your computer and in the Dockerfile.
38.5 How to Run It and What You See
-
Put this file in your repo at
.github/workflows/ci.yml. -
Commit the code and
git push. -
Open the Actions tab of your repo on GitHub. Your workflow "CI" will appear running there.
-
Click the workflow: you will see the
testanddocker-buildjobs and the log of every step . -
Green tick = all good. Red cross = some step failed; click that step and read the error log .
You can first run on your local computer the same test that the robot will run (this is the real output ):
TERMINAL
pytest -q
OUTPUT
..... [100%]
5 passed, 1 warning in 1.60s
If you do not get "5 passed" locally, you will not get it on GitHub either. Fix it locally first, then push.
EXAMPLE: What you see in the Actions tab (the real look will differ, with ticks)
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 The First Step Toward CD
After CI, the next step is CD : as soon as new good code arrives on main, it goes live by itself . Common ways:
| Way | How it works |
|---|---|
| Connecting hosting to GitHub | Platforms like Railway and Render watch your repo andautomatically deploya new build from the Dockerfileon every push |
| Deploying with GitHub Actions | Another job in the workflow that sends the image to a registry (Docker Hub, GHCR) and informs the server |
| Hugging Face Spaces | Popular for small ML demos; it runs the app in a Space |
MOST IMPORTANT
Never write secrets in YAML. If you need a token or password for deploying (for example for a registry), keep it in GitHub under Settings > Secrets and variables > Actions and use it in the workflow as ${{ secrets.NAME }}. If a key ever reached GitHub, change it immediately (revoke it); just deleting it is not enough.
38.7 A Status Badge on the README
Put a small badge in the repo's README.md that shows whether CI is currently passing or failing. Change the username and repo name and write this line:
README.md

It shows a green (passing) or red (failing) mark. Anyone looking at your project can tell at a glance that it is reliable.
38.8 Common Problems and Their Solutions
| Problem | Cause and solution |
|---|---|
| The workflow does not run at all | The file path must beexactly.github/workflows/ and the extension .yml or.yaml; the branch name must match on: |
| YAML error | Theindentation(spaces) is wrong. Usespacesinstead of tabs; 2 spaces per level |
ModuleNotFoundError in CI | The library is not written in requirements.txt. It worked locally because it wasinstalled on your computer |
| Test passes locally, fails in CI | The Python or libraryversion is different, or some file was never pushed to the repo (for example model/) |
| Model file not found | It got into .gitignore, or was not pushed because of GitHub's size limit |
| Smoke test fails | The container takes time to start (increase sleep), or the CMD / port is wrong |
38.9 Common Mistakes and Hands-on
Hands-on
-
For the
iris_apiproject of Chapter 35, make a.github/workflows/ci.ymlthat runs onlypytest. -
Push the repo to GitHub and see the first green tick in the Actions tab.
-
Deliberately make one test wrong (for example
assert res.status_code == 201
), push, and read the log with the red cross . Then fix it.
- Add a CI badge to the README.
KEY TAKEAWAYS
-
CI does tests and a build automatically on every change; CD releases good code automatically .
-
A GitHub Actions workflow lives in
.github/workflows/*.yml: event , jobs , steps , runner . -
usesis for ready-made actions andrunis for your own commands;needsdecides the order of jobs. -
pytestfor automatic testing,docker buildfor automatic build, and the smoke test withcurl --fail. -
Secrets always go in GitHub Secrets, never in code or YAML.