CI/CD pipelines
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A CI/CD pipeline is a file in your repository that a machine runs on every push. Follow one commit through a real GitHub Actions workflow: a fresh runner tests it, builds a container image tagged with the commit, stores it in a registry, waits at an approval gate, and Kubernetes rolls it out one pod at a time. Then see what happens when a test goes red, and how a bad release is rolled back.
A pipeline is a file
A CI/CD pipeline is a list of stages a machine runs for every change: test, build, store, deploy. It lives in the repository as a file, here .github/workflows/ship.yml for GitHub Actions. Any red stage stops the line.
.github/workflows/ship.yml
push → test → build → store → approve → deploy
CI = continuous integration: test every push
CD = continuous delivery: every green build
is ready to deploy
a red stage stops everything after itاقرأ الدرس خطوة بخطوة
A pipeline is a file
A CI/CD pipeline is a list of stages a machine runs for every change: test, build, store, deploy. It lives in the repository as a file, here .github/workflows/ship.yml for GitHub Actions. Any red stage stops the line.
.github/workflows/ship.yml push → test → build → store → approve → deploy CI = continuous integration: test every push CD = continuous delivery: every green build is ready to deploy a red stage stops everything after itA push starts it
A developer commits a change and runs git push. The commit's ID, its SHA, starts 3f9c2ab. Line 1, on: push, tells GitHub to start the pipeline for every push. Production still runs 8d41e07.
$ git commit -m "Round totals to cents" $ git push # commit 3f9c2ab… on: push → start the workflow production now: shop:8d41e07 on 3 podsA fresh runner
A runner is a machine that runs pipeline jobs. Line 2 asks for ubuntu-latest: GitHub starts a brand-new virtual machine for this job and checks out the commit. Nothing is left over from earlier runs.
runs-on: ubuntu-latest new virtual machine, just for this job - uses: actions/checkout@v4 → the code at 3f9c2ab clean every time → the same result every timeStage 1: test
Line 3 runs the unit tests. python3 -m unittest finds every test file and exits with status 0 only if all pass. Any other exit code turns the step red, and every later stage is skipped.
$ python3 -m unittest . Ran 1 test in 0.000s OK exit 0 → next step a failing test exit 1 → stop, redStage 2: build an image
Line 4 builds a container image: the app plus everything it needs to run. $IMG names it after the commit, shop:3f9c2ab, so anyone can tell exactly which code is inside.
$ docker build -t $IMG . IMG = ghcr.io/acme/shop:${{ github.sha }} = ghcr.io/acme/shop:3f9c2ab… tag = commit SHA → image ↔ code, one to oneStage 3: store it
Line 5 pushes the image to a registry, a store for images. The runner is then thrown away; the image lives on. The registry now holds 8d41e07 and 3f9c2ab, so older versions stay available.
$ docker push $IMG ghcr.io/acme/shop shop:8d41e07 (in production) shop:3f9c2ab (new) build once: every environment gets this same imageThe approval gate
The deploy job needs build, so it waits until build is green. Line 6 names the production environment; with a required reviewer the job pauses until a person approves. That is continuous delivery: always ready, released on a decision.
deploy: needs: build wait for green environment: production wait for a reviewer continuous delivery a person presses go continuous deployment no gate: every green build goes straight outStage 4: roll it out
Line 7 asks Kubernetes to run the new image. It does a rolling update: start one new pod, wait until it is ready, remove one old pod, repeat. With 3 pods, all 3 keep serving users throughout.
$ kubectl set image deploy/shop web=$IMG $ kubectl rollout status deploy/shop RollingUpdate, 3 replicas, defaults 25% / 25%: maxSurge = ceil(0.75) = 1 extra pod maxUnavailable = floor(0.75) = 0 pods down pods: 8d41e07 → 3f9c2ab, one at a timeA red test stops it
Next, commit e5a1c90 breaks a test. Line 3 exits with status 1, the build job fails, and deploy, which needs build, is skipped. No image is built; users keep running 3f9c2ab. The developer gets the red result quickly.
commit e5a1c90 $ python3 -m unittest FAIL: test_total (test_shop.TestShop.test_total) FAILED (failures=1) exit 1 build: failed deploy: skipped (needs: build) production unchanged: shop:3f9c2abRoll back if needed
Tests cannot catch everything. If users hit a bug in 3f9c2ab, one command returns the Deployment to its previous version. The 8d41e07 image is still in the registry, so Kubernetes rolls back pod by pod.
$ kubectl rollout undo deploy/shop pods: 3f9c2ab → 8d41e07, one at a time the old image is still in the registry then fix forward: a new commit, a new run
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01In the workflow, what does on: push do?
on: is the trigger. With on: push, GitHub Actions runs the workflow for every push.
02Commit e5a1c90 makes python3 -m unittest exit with status 1. What happens to the deploy job, which has needs: build?
A non-zero exit code fails the step and the build job. If a job fails, every job that needs it is skipped, so nothing broken reaches users.
03Why tag the image with the commit SHA (shop:3f9c2ab) instead of something like shop:latest?
A tag built from the commit ties the image to exactly one commit. The same image is deployed everywhere, and older ones stay in the registry for rollback.
04What is the difference between continuous delivery and continuous deployment?
The approval gate (environment: production with a required reviewer) makes this pipeline continuous delivery. Remove the gate and it becomes continuous deployment.
05A Deployment has 3 replicas and the default rolling update settings (maxSurge 25%, maxUnavailable 25%). How does Kubernetes replace the pods?
25% of 3 is 0.75. Kubernetes rounds the surge up to 1 and the unavailable count down to 0: one extra pod at a time, none taken away before its replacement is ready.
المسرد
- CI/CD pipeline
- An automated series of stages (test, build, store, deploy) that runs for every change. Each stage must pass before the next starts.
- Continuous integration (CI)
- Building and testing every change automatically as soon as it is pushed.
- Continuous delivery / deployment (CD)
- Delivery keeps every passing build ready to release when a person decides; deployment releases every passing build automatically.
- Workflow
- In GitHub Actions, a YAML file in .github/workflows/ that says when the pipeline runs and what its jobs and steps are.
- Runner
- The machine that runs a pipeline job. GitHub-hosted runners are fresh virtual machines, one per job.
- Container image
- A packaged app with everything it needs to run, built by docker build and named by a tag such as shop:3f9c2ab.
- Registry
- A server that stores container images by name and tag, such as the GitHub Container Registry (ghcr.io).
- Rolling update
- Kubernetes' default way to change a Deployment: replace pods a few at a time so the app keeps serving. kubectl rollout undo goes back to the previous version.
الملاحظات والمصادر
- The card shows seven key lines of the full 30-line workflow (indentation trimmed). The full file also checks out the code, logs in to the GitHub Container Registry and writes the cluster credentials from a secret. Step text counts the card's line numbers, not the file's. deploy/shop is kubectl's short name for deployment/shop. The repository also holds shop.py, test_shop.py and a Dockerfile. Images on ghcr.io start private, so the cluster needs an imagePullSecret (or a public package) to pull them.
- Commits are shown by their first seven hex digits; github.sha, and so the image tag, is the full 40-digit SHA.
- RollingUpdate is the default Deployment strategy. maxSurge and maxUnavailable default to 25%; Kubernetes rounds the surge up and the unavailable count down, so 3 replicas get 1 extra pod and 0 pods down.
- Required reviewers are an environment protection rule set in the repository settings, not in the YAML file; on GitHub Free, Pro and Team plans they work only in public repositories. Other CI services (GitLab CI, Jenkins, Azure Pipelines) use their own file formats for the same idea.
- kubectl rollout undo returns to the previous revision of the Deployment; Kubernetes keeps 10 old revisions by default (revisionHistoryLimit).
- GitHub Docs: Workflow syntax for GitHub Actions
- GitHub Docs: GitHub-hosted runners
- GitHub Docs: Contexts (github.sha)
- GitHub Docs: Managing environments for deployment
- GitHub Docs: Working with the Container registry
- Docker docs: docker build
- Docker docs: docker push
- Kubernetes docs: Deployments (rolling update, rollout status, rollout undo)
- kubectl reference: resource types and short names
- Kubernetes docs: Pull an Image from a Private Registry
- Python docs: unittest command-line interface
- Google SRE book, ch. 8: Release Engineering
- Martin Fowler: Continuous Delivery
- DORA capabilities: Continuous delivery
اختبار يتاح بعد الخطوة الأخيرة.