CI/CD concepts & branching strategies
Design a pipeline: lint, test, build, scan, deploy, with promotion between environments.
By Invicly Tech, , in Full DevOps with Python. About 12 minutes to read.
In short
CI merges and tests every change automatically; CD keeps the result ready to release or ships it straight to production. A good pipeline fails fast on cheap checks and promotes one build through each environment.
Explained simply
CI/CD is a set of habits plus automation. CI means continuous integration: everyone merges small changes into the main branch often, and a robot checks each change right away. CD means continuous delivery or continuous deployment: the checked change is packaged and pushed towards production automatically.
Think of a pipeline as a factory line. Your code goes in at one end. Each station does one job: check the style, run the tests, build the image, scan it for known security holes, deploy it to a test server. If a station fails, the line stops and you get a message within minutes.
Delivery and deployment differ in one place. With continuous delivery, a person presses the final button to release to production. With continuous deployment, there is no button: if every check passes, the change goes live by itself.
Branching strategy is how a team uses Git branches. Trunk-based development keeps branches tiny and merges them into main every day. GitFlow keeps long-lived develop and release branches for planned releases. Most modern web teams choose trunk-based, because CI works best when changes are small.
Press Play on a diagram to walk through it one step at a time.
- Merge small changes often
- Lint and test every push
- Broken builds fixed fast
- Every build is releasable
- Auto deploy to staging
- Human approves production
- No manual gate
- Passing builds go to prod
- Needs strong tests and monitoring
- 1Lint
Seconds; catches style and obvious errors.
- 2Test
Unit and integration tests with pytest.
- 3Build
One image, tagged with the git SHA.
- 4Scan
Check image and dependencies for known CVEs.
- 5Deploy staging
Automatic, followed by smoke tests.
- 6Promote to prod
Same image, after approval or checks.
- 1Small change
A branch that lives hours, not weeks
- 2Automated checks
Lint, tests, build, scan
- 3Merge and deploy
Same image promoted through environments
- 4Monitor
Errors, latency, user impact
- 5Learn
Fix or improve in the next small change
- Back to step 1
In depth
Why small, frequent merges matter
The core idea of CI is that integration pain grows faster than the size of the change. Two developers who each work alone for three weeks will have a huge, risky merge. Two developers who merge every day have tiny merges that rarely conflict, and if a test breaks, the cause is in the last few hours of work.
This is why CI is a practice, not just a tool. Running Jenkins on a branch that nobody merges for a month is not continuous integration. The rules teams follow are simple: merge to main at least daily, keep main green (passing), and if main breaks, fixing it is the top priority before new work.
The DORA research program measures delivery performance with four key metrics: deployment frequency, lead time for changes (commit to production), change failure rate, and time to restore service after a failure. Teams that deploy small changes often tend to score better on all four, including stability.
- Merge to main at least once a day
- A red main branch is fixed before anything else
- Small changes are easier to review, test and roll back
Trunk-based development vs GitFlow
In trunk-based development, main (the trunk) is always releasable. Developers create short-lived branches, open a pull request, and merge within a day or two. Unfinished features are hidden behind feature flags, so code can be merged before the feature is switched on. Releases are cut from main, often many times a day.
GitFlow, described by Vincent Driessen in 2010, uses more branches. develop collects finished features. A release branch is cut from develop to stabilise a version, then merged into main and tagged. Hotfix branches start from main for urgent fixes and are merged back into both main and develop. It fits software shipped in versions, like desktop apps or libraries with several supported releases.
GitHub Flow is a light middle ground: main plus feature branches and pull requests, with deploys from main. Driessen himself later noted that GitFlow is often too heavy for continuously delivered web apps.
- main plus branches that live 1 to 2 days
- Feature flags hide unfinished work
- Deploy from main many times a day
- main plus feature branches
- Every change goes through a PR
- Deploy after merge to main
- main, develop, feature, release, hotfix
- Release branches stabilise a version
- Fits versioned, scheduled releases
Designing the stages
A good pipeline gives fast feedback and builds trust. Put cheap checks first: formatting and lint (ruff) take seconds, type checks (mypy) take a little longer, unit tests next, then slower integration tests that need a database. Run independent checks in parallel jobs to save time. A common target is feedback on a pull request in under ten minutes.
The build stage creates one artifact, for us a Docker image tagged with the git commit SHA. Then scanning checks dependencies and the image for known vulnerabilities (CVEs) with tools like pip-audit or Trivy, and can block high-severity findings.
After the build, nothing is rebuilt. The same image goes to staging, gets smoke tests (a few quick requests to check it starts and answers), and is then promoted to production. Only configuration changes between environments, through environment variables or secrets. This way, production runs exactly the bytes you tested.
- Fast and cheap checks first, slow ones later
- Pull requests run checks; only main builds and deploys
- Build once, promote the same image everywhere
Promotion, approvals and safe releases
Promotion means moving one tested build from one environment to the next: dev, then staging, then production. Each step can have a gate. A gate can be automatic (smoke tests pass, error rate is normal) or manual (a named person approves). GitHub Actions supports this with environments that have required reviewers; Jenkins uses an input step.
How you switch traffic also matters. A rolling deployment replaces old instances a few at a time. Blue-green runs a full new copy next to the old one and switches all traffic at once, so rollback is just switching back. A canary release sends a small share of traffic, say 5 percent, to the new version and watches metrics before going further.
Always plan the rollback before you deploy. With immutable, SHA-tagged images, rolling back is simply deploying the previous tag. Database migrations need extra care: make them backward compatible so the old version still works if you roll back.
- 1Build api:3f9c2ab
Created once on merge to main
- 2Dev
Deployed automatically
- 3Staging
Smoke tests must pass
- 4Approval gate
Reviewer or automated checks
- 5Production
Same image, prod config
Learn it in 3 short steps
About 40m each. Track them with a timer in your plan.
- 1Learn CI vs continuous delivery vs deployment~40m
- 2Compare trunk-based and GitFlow branching~40m
- 3Sketch stages from lint to deploy~40m
Key points
- Order stages from fastest and cheapest to slowest so failures show up early.
- Build the artifact once and promote that same image through every environment.
- Trunk-based development uses short branches merged to main daily.
- GitFlow adds develop, release and hotfix branches for scheduled releases.
Key terms
Tap a card to flip it. 0/5 known
Examples
Sketch a pipeline for the FastAPI service
Step 1: on every pull request, run ruff and pytest in parallel jobs; these must pass before merge. Step 2: on merge to main, build the image once and tag it with the short git SHA. Step 3: scan it with Trivy and fail on CRITICAL findings. Step 4: deploy it to staging and call /health as a smoke test. Step 5: a reviewer approves, and the same tag is deployed to production. The outline below is tool-neutral; any CI system can run it.
on pull_request:
lint: ruff check . (~10 s)
test: pytest -q (~2 min)
on push to main:
build: docker build -t api:$SHA .
scan: trivy image --exit-code 1 --severity CRITICAL api:$SHA
push: docker push registry/api:$SHA
deploy staging -> curl -f https://staging.example.com/health
manual approval
deploy production (same api:$SHA, prod config)Merge unfinished work safely with a feature flag
You are building a new export endpoint that will take a week. With trunk-based development you still merge daily. Step 1: wrap the new route in a flag read from an environment variable, off by default. Step 2: merge each small piece to main; tests cover both flag states. Step 3: turn the flag on in staging and test. Step 4: turn it on in production. Step 5: once stable, delete the flag and the old code path, so flags do not pile up.
import os
from fastapi import FastAPI, HTTPException
app = FastAPI()
EXPORT_ENABLED = os.getenv("FEATURE_EXPORT", "false") == "true"
@app.get("/export")
def export():
if not EXPORT_ENABLED:
raise HTTPException(status_code=404)
return {"status": "export started"}Quick reference
- CI: merge small changes often and test each one automatically
- Continuous delivery keeps a human approval before prod; continuous deployment does not
- Order stages from fastest to slowest: lint, unit tests, integration tests, build, scan
- Tag images with the git SHA and promote that exact image; never rebuild per environment
- Keep branches short-lived; use feature flags for work that is not ready
- DORA metrics: deploy frequency, lead time, change failure rate, time to restore
- Rollback with immutable images is just deploying the previous tag
Common mistakes
- Rebuilding the image for each environment, so prod runs something never tested.
- Long-lived branches that turn every merge into a painful conflict.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
For a small Python API, list the checks you would run on a pull request and the extra steps you would run only on main. Put them in order.
- Exercise 2medium
Your PR pipeline takes 25 minutes. Lint 1 min, unit tests 6 min, integration tests 12 min, docker build 6 min, all in one sequential job. Redesign it to give feedback faster.
- Exercise 3hard
Design a rollback plan for a release that adds a new NOT NULL column to the orders table and code that writes it. The rollback must work without data loss.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is CI/CD a tool?
- No. CI/CD is a set of practices: small frequent merges, automated checks and automated releases. Tools like GitHub Actions, GitLab CI and Jenkins run the automation, but the habits matter more.
- Should I use GitFlow or trunk-based development?
- For a web service deployed continuously, trunk-based or GitHub Flow is usually simpler and faster. GitFlow suits products released in numbered versions that you must support in parallel.
- How long should a CI pipeline take?
- Aim for pull request feedback in about 10 minutes or less. Longer pipelines push people to batch changes and skip waiting, which defeats the point of CI.
- What is the difference between a deploy and a release?
- A deploy puts new code on servers. A release makes a feature available to users. Feature flags let you deploy code now and release the feature later.