24.1. Introduction to CI/CD
Table of Contents
Why CI/CD Matters for Backend Developers
When you work on backend applications, you will change code often. Each change needs to be tested, built, and deployed to a server. Doing this manually is slow and error prone. Continuous Integration and Continuous Delivery (CI/CD) is about automating these steps so that:
- your code is tested every time you push
- your application is built in a repeatable way
- deployments become predictable and less stressful
CI/CD is not a specific tool. It is a set of practices plus tools that help you move code from your laptop to production safely and quickly.
Key idea: CI/CD automates the path from code change to running in production, with automatic checks at every step.
In this chapter you will learn what CI and CD are, how they fit into backend development, and how they connect to later topics like Docker, testing, and deployment.
Core Concepts: CI, CD, and Pipelines
Continuous Integration in One Sentence
Continuous Integration (CI) means merging small code changes into a main branch frequently, and automatically checking that they work.
In practice, CI usually involves:
- pushing code to a shared repository such as GitHub or GitLab
- automatically running:
- tests
- linters and formatters
- static analysis or type checking
- getting fast feedback when something breaks
Typical CI tools:
| Provider | CI Solution |
|---|---|
| GitHub | GitHub Actions |
| GitLab | GitLab CI/CD |
| Bitbucket | Bitbucket Pipelines |
| Self-hosted | Jenkins, Drone CI, etc. |
As a backend developer, CI answers the question:
“Is the code that I just pushed safe to integrate with the main branch?”
Continuous Delivery vs Continuous Deployment
CD is often used for two related ideas:
- Continuous Delivery
The application is always in a deployable state. After CI passes, the system can automatically prepare a release. A human usually decides when to deploy. - Continuous Deployment
Every change that passes the pipeline is automatically deployed to production, without a manual approval step.
You can think of them like this:
| Concept | What is automated? | Manual step? |
|---|---|---|
| Continuous Integration | Build and tests on code changes | Merge decisions |
| Continuous Delivery | Build, tests, packaging, staging deploy | Final production deploy |
| Continuous Deployment | Build, tests, packaging, all deployments | None (fully automated) |
Most backend teams start with CI, then add Continuous Delivery, and later decide whether they want full Continuous Deployment.
What Is a CI/CD Pipeline?
A CI/CD pipeline is a defined sequence of automated steps that run when your code changes. You usually describe it in a configuration file in your repository.
A very simple backend pipeline might:
- Trigger when you push to
mainor create a pull request. - Check out the code.
- Install dependencies.
- Run tests.
- Build a Docker image.
- Push the image to a registry.
- Deploy it to a staging or production environment.
You can imagine it as a production line for your software:
| Stage | Example actions for a Python backend |
|---|---|
| Source | Code pushed to GitHub/GitLab |
| Build | Install dependencies, create build artifacts |
| Test | Run pytest, linting, type checks |
| Package | Build a Docker image |
| Deploy | Deploy container to server or Kubernetes |
| Verify | Health checks, smoke tests after deploy |
Important rule: A good pipeline is repeatable and fully defined in code, not in someone’s memory or on one person’s laptop.
CI/CD in the Backend Development Workflow
Where CI/CD Fits in a Typical Workflow
Here is a simple workflow for a backend developer:
- You create a feature branch from
main. - You implement changes locally and run tests.
- You push the branch to the remote repository.
- A CI pipeline runs automatically:
- installs dependencies
- runs tests
- builds the app
- You open a pull request or merge request.
- The pipeline must be green (successful) before the branch can be merged.
- After merging to
main, a CD pipeline might: - build and push a Docker image
- deploy to staging or production
- run post-deploy checks
Without CI/CD, steps 4 and 7 are often done manually or inconsistently.
Benefits Specific to Backend Projects
For backend development, CI/CD helps with:
- Maintaining API stability
Automated tests can verify that endpoints respond as expected, that HTTP status codes are correct, and that contracts are preserved. - Database safety
Integration tests can run against test databases, and migration steps can be automated and checked before production. - Security
Pipelines can run security scanners on dependencies and code, and block merges when high risk vulnerabilities are found. - Performance and scalability regression checks
Later in this course, you will see how load tests and performance checks can be integrated into pipelines.
Simple Example: A Minimal CI Pipeline
Imagine you have a small FastAPI backend. A minimal CI pipeline could:
- Run whenever code is pushed or a pull request is opened.
- Install Python and dependencies.
- Run unit tests with
pytest. - Fail the pipeline if any test fails.
If tests fail, the code is not merged. This alone can prevent many bugs from reaching production.
How CI/CD Is Defined as Code
Pipeline Configuration Files
CI/CD systems usually use a YAML configuration file that lives in your repository. For example:
- GitHub Actions uses
.github/workflows/*.yml - GitLab CI uses
.gitlab-ci.yml
These files define:
- triggers (for example, “on push to main”)
- jobs or steps
- environments (for example, staging, production)
- secrets and environment variables references
You will learn concrete syntax in the tool-specific chapters, but the pattern is similar everywhere: you declare the pipeline as code, commit it, and version it along with your application.
Common Pipeline Stages for Backends
Although the syntax differs, backend pipelines usually share similar stages:
| Stage | Purpose | Example checks |
|---|---|---|
| Lint | Enforce code style and basic static checks | flake8, black, isort |
| Test | Run automated tests | pytest, coverage |
| Build | Create an artifact (jar, Docker image, zip, etc.) | docker build |
| Pre-deploy | Run database migrations in a safe way | Alembic migrations |
| Deploy | Roll out new version | Restart containers or services |
| Post-deploy | Smoke tests and health checks | Hitting /health endpoint |
Not every project needs all of these, but as you move to production, more stages become important.
The Role of Tests in CI/CD
Tests as a Gatekeeper
In earlier chapters you will learn about testing in detail. For CI/CD, tests are the gate that controls whether a change can progress.
A simple rule is:
A change must not be merged or deployed if the tests fail.
For backend development, you typically run:
- unit tests for functions and classes
- API tests for endpoints
- integration tests for the database and external services (where possible)
As your project grows, you may also add:
- contract tests for external APIs
- smoke tests that run right after a deployment
Fast Feedback versus Full Coverage
In CI/CD, you often balance speed and depth:
- on every push, you may run a fast subset of tests
- on main branch merges or nightly, you may run a full test suite
For example:
- Quick CI job:
- run unit tests
- run basic linters
- Extended job:
- run integration tests with a real database
- run slow end‑to‑end tests
The goal is that developers receive feedback quickly, usually within a few minutes.
Environments and Deployment Flow
From Development to Production
CI/CD pipelines usually involve multiple environments. For a backend service, you might see:
| Environment | Purpose | Who uses it |
|---|---|---|
| Local | Development on your machine | Developers |
| Test | Automated tests, QA | Developers, QA |
| Staging | Looks like production, final validation | Developers, product |
| Production | Real users and real data | End users |
A simple flow:
- Developer pushes branch.
CI runs tests on an isolated environment (for example, ephemeral container, test database). - Merge to
main.
CD pipeline builds a Docker image and deploys it to a staging environment. - Manual or automatic checks on staging.
If all looks good, another step deploys the same build to production.
Immutable Artifacts
Modern pipelines often create an immutable artifact, for example a Docker image, from your code. The same artifact is then deployed to staging and production.
This helps to avoid situations where staging and production are different because someone built them differently.
In later Docker and Deployment chapters, you will see how CI/CD pipelines build and publish Docker images and then pull them on servers.
CI/CD and Other Topics in This Course
You will see CI/CD ideas appear again and again in later chapters:
- Testing
You will learn unit, integration, and API testing that will plug into your CI pipelines. - Docker
Pipelines will build and push Docker images for your backend. - Deployment
CI/CD will run deployment scripts, run database migrations, and restart application servers. - Security
Pipelines can run security scans on dependencies, Docker images, and code. - Monitoring and Logging
After deployment, monitoring tools check whether the new version is healthy. Pipelines can include smoke tests and health checks.
You do not need to master all of this now. For this chapter, the key goal is to understand that CI/CD connects your Git workflow, tests, builds, and deployments into one automated path.
Practical Starting Point for Beginners
You can introduce CI/CD gradually. For a first backend project, a simple plan might be:
- Use GitHub or GitLab for your repository.
- Add a very simple CI workflow:
- trigger on push and pull requests
- install Python
- install dependencies from
requirements.txt - run
pytest - Treat a red (failing) pipeline as a blocker:
- fix tests
- push again until the pipeline is green
- When you start using Docker, extend the pipeline:
- build a Docker image
- push it to a registry (for example, Docker Hub)
- When you learn deployment, add a deployment step:
- deploy the new Docker image to a server
- run a basic health check
At each step you make more of your workflow reproducible and less manual.
Summary
- CI/CD automates the journey from code change to running backend service.
- Continuous Integration focuses on integrating code often and running automated checks.
- Continuous Delivery and Continuous Deployment automate packaging and deployment.
- Pipelines are defined as code in configuration files and consist of stages like lint, test, build, and deploy.
- Tests are the main gatekeeper that control whether a change can move forward.
- Multiple environments such as test, staging, and production help you deploy safely.
- Over the rest of this course, you will connect CI/CD with Docker, testing, deployment, and monitoring to build a complete, professional backend workflow.
Views: 9
KAHIBARO