Skip to main content

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
TermMeaningIn this workflow
workflowThe whole automation (one YAML file)CI
event (on)Whenit startsPushorpull requeston main
jobAgroupof tasks, on a separate machinetest, docker-build
runnerThemachineon which the job runsubuntu-latest
stepOnestepof a job"Install dependencies", "Run tests"
usesUse aready-made actionactions/checkout@v4
runRun your ownterminal commandpytest -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
PartWhat it does
on: push / pull_requestRun the workflow onevery pushandevery pull requeston the main
branch
actions/checkout@v4Downloadyour code onto the runner (otherwise the runner is empty)
actions/setup-python@v5Set upPython 3.12; cache: pip makes downloading librariesfaster
pip install -r requirements.txt -r requirements-dev.txtInstall both theruntimeandtestinglibraries
pytest -qRun all the tests. If even one fails, thejob fails(red)
needs: testThe docker-build job runsonly whentest passes
docker build -t medical-assistant .Build the Chapter 36 imageon the robot's computer
curl --fail .../healthRun the container from the image and check**/health; if there is no
response, it
fails**
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​

  1. Put this file in your repo at .github/workflows/ci.yml.

  2. Commit the code and git push.

  3. Open the Actions tab of your repo on GitHub. Your workflow "CI" will appear running there.

  4. Click the workflow: you will see the test and docker-build jobs and the log of every step .

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

WayHow it works
Connecting hosting to GitHubPlatforms like Railway and Render watch your repo andautomatically deploya
new build from the Dockerfileon every push
Deploying with GitHub ActionsAnother job in the workflow that sends the image to a registry (Docker Hub, GHCR) and informs the server
Hugging Face SpacesPopular 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​
![CI](https://github.com/<username>/<repo>/actions/workflows/ci.yml/badge.svg)

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​

ProblemCause and solution
The workflow does not run at allThe file path must beexactly.github/workflows/ and the extension .yml or
.yaml; the branch name must match on:
YAML errorTheindentation(spaces) is wrong. Usespacesinstead of tabs; 2 spaces per level
ModuleNotFoundError in CIThe library is not written in requirements.txt. It worked locally because it was
installed on your computer
Test passes locally, fails in CIThe Python or libraryversion is different, or some file was never pushed to the
repo (for example model/)
Model file not foundIt got into .gitignore, or was not pushed because of GitHub's size limit
Smoke test failsThe 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_api project of Chapter 35, make a .github/workflows/ci.yml that runs only pytest .

  • 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 .

  • uses is for ready-made actions and run is for your own commands; needs decides the order of jobs.

  • pytest for automatic testing, docker build for automatic build, and the smoke test with curl --fail.

  • Secrets always go in GitHub Secrets, never in code or YAML.