ENع
→CI/CD
الدرس 12 · التسليم

CI/CD pipelines

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 12 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. A 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 pods
  3. A 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 time
  4. Stage 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, red
  5. Stage 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 one
  6. Stage 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 image
  7. The 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 out
  8. Stage 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 time
  9. A 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:3f9c2ab
  10. Roll 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

اختبار يتاح بعد الخطوة الأخيرة.