Overview and What You Will Learn
CI/CD is the practice of automatically building, testing, and delivering every code change through a pipeline, so releases become routine instead of risky events. Continuous Integration (CI) merges and verifies changes often, Continuous Delivery keeps every verified change ready to release, and Continuous Deployment releases it to production automatically with no manual approval.
A pipeline is the automated path a commit follows. It is made of stages and jobs, started by a trigger, and it produces artifacts such as a container image. Ordering the cheapest checks first, called fail-fast, is what keeps feedback quick.
By the end of this topic you will be able to:
- Explain the difference between CI, Continuous Delivery, and Continuous Deployment
- Read a GitHub Actions workflow and identify its triggers, jobs, dependencies, and artifacts
- Order pipeline stages using the fail-fast principle
- Build a container image once and promote the same image to every environment
- Diagnose a failing run and measure pipeline speed and reliability
Why This Matters in Production
Pipeline speed decides how engineers work. When feedback takes 45 minutes, people switch tasks, forget the context, and batch up changes, which makes each release bigger and riskier. When feedback takes about 10 minutes, engineers fix problems while the change is still fresh and ship small changes often.
Small, frequent releases are easier to review, easier to test, and easier to roll back. Teams that ship many times a day, such as those at Shopify, Spotify, or Razorpay, depend on automated pipelines to make that safe. Slow or flaky pipelines push teams back toward big, infrequent, high-risk releases.
Core Principles
The three terms describe how far automation goes. Each stage adds trust in the change before it reaches users.
Continuous Deployment is the same flow with the approval gate removed. It needs strong test coverage, fast rollback, and feature flags before it is safe.
How a Pipeline Is Built
A pipeline has five parts:
- Trigger: the event that starts it, such as a push, a pull request, a schedule, or a manual dispatch.
- Stage: a phase such as test or deploy. In GitHub Actions the same idea is expressed through job dependencies.
- Job: a unit of work that runs on its own runner. Jobs without dependencies run in parallel.
- Step: one command or action inside a job.
- Artifact: a file or image a job produces for later jobs or for release.
The Fail-Fast Principle
Run the cheapest, fastest checks first so a broken change is rejected in seconds, not after a long build. Run independent checks in parallel.
- Lint and format (about 10 seconds): catches typos and style errors.
- Unit tests (about 2 minutes): catches logic errors.
- Build the container image (about 3 minutes): only worth doing once tests pass.
- Integration tests and security scan (about 5 minutes, in parallel): need a built artifact.
- Deploy to staging (about 2 minutes): runs only after every gate passes.
RememberBuild the artifact once and promote that exact image, by digest or commit SHA, through staging and production. Rebuilding per environment means production runs something that was never tested.
Detailed Step-by-Step Practical Lab
Step 1 - Read a Complete Pipeline Definition
This workflow follows fail-fast ordering. Lint and unit tests run in parallel, the image is built and pushed only after both pass and only on main, and staging deploys last.
name: Payment API CI/CD on: push: branches: [main] pull_request: branches: [main] # Default to read-only; jobs ask for more only when they need itpermissions: contents: read # New pushes cancel older PR runs, but never cancel a run on mainconcurrency: group: ci-${{ github.ref }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} env: ECR_REGISTRY: 123456789012.dkr.ecr.ap-south-1.amazonaws.com jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 20 cache: npm # reuse the npm cache between runs - run: npm ci && npm run lint unit-test: # no needs: runs in parallel with lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 20 cache: npm - run: npm ci && npm test build-push: needs: [lint, unit-test] # both must pass first if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest permissions: contents: read id-token: write # lets the job request an OIDC token outputs: image-tag: ${{ steps.meta.outputs.tag }} steps: - uses: actions/checkout@v4 - uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::123456789012:role/gha-payment-api-build-role aws-region: ap-south-1 - uses: aws-actions/amazon-ecr-login@v2 - id: meta run: echo "tag=${GITHUB_SHA}" >> "$GITHUB_OUTPUT" - run: | docker build -t "$ECR_REGISTRY/payment-api:${GITHUB_SHA}" . docker push "$ECR_REGISTRY/payment-api:${GITHUB_SHA}" deploy-staging: needs: build-push # must list build-push to read its outputs runs-on: ubuntu-latest environment: staging permissions: contents: read id-token: write env: IMAGE_TAG: ${{ needs.build-push.outputs.image-tag }} steps: - uses: actions/checkout@v4 - run: ./deploy.sh staging "$IMAGE_TAG"Common MistakeReading
needs.build.outputs.*from a job that does not listbuildin its ownneeds. Theneedscontext only contains the jobs you directly depend on, so the value comes back empty.
Step 2 - Trace a Commit Through the Pipeline
Here is what happens from git push to staging, with realistic timings.
git add payments/processor.jsgit commit -m "fix: handle timeout in payment processor"git push origin main1. GitHub receives the push and starts the workflow within seconds2. lint (23s) and unit-test (2m 14s, 847 tests) run in parallel on two runners3. build-push starts only after both pass: builds and pushes payment-api:abc12344. deploy-staging runs helm upgrade with image.tag=abc1234 and checks pod health5. A Slack notification reports the result Total: about 8 minutes from push to stagingStep 3 - Control When the Pipeline Runs With Triggers
Triggers decide which events start a workflow. Path filters keep documentation-only changes from running a full build.
on: push: branches: [main] paths-ignore: # skip runs for docs-only changes - '**.md' - 'docs/**' pull_request: branches: [main] types: [opened, synchronize, reopened] schedule: - cron: '0 21 * * *' # cron is UTC: 21:00 UTC is 02:30 IST workflow_dispatch: # manual run from the Actions tab or gh CLI inputs: environment: type: choice options: [staging, production] required: trueTipUse either
pathsorpaths-ignoreon one event, not both. If a required status check is skipped by a path filter, the pull request can get stuck waiting for it, so test the combination with branch protection before relying on it.
Step 4 - Pass Artifacts Between Jobs
Jobs run on separate machines, so files must be uploaded as artifacts to be reused. This lets tests run against the exact build output.
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: npm ci && npm run build - uses: actions/upload-artifact@v4 with: name: dist-files path: dist/ retention-days: 7 test: needs: build runs-on: ubuntu-latest steps: - uses: actions/download-artifact@v4 with: name: dist-files path: dist/ - run: npm test - uses: actions/upload-artifact@v4 if: always() # keep the report even when tests fail with: name: test-results path: test-results/junit.xmlStep 5 - Diagnose a Failing Pipeline
Work from the run list down to the failing step, then reproduce locally. The gh CLI shows each level.
# 1. Find the failed rungh run list --workflow=ci.yaml --limit 5 # 2. See which job and step failedgh run view 9876543 # 3. Print only the logs of failed stepsgh run view 9876543 --log-failed # 4. Reproduce locally with just the failing testnpm test -- src/payments/processor.test.js # 5. Fix, push, and re-run only the failed jobs if the cause was transientgit commit -am "fix: mock timer in processor timeout test"git pushgh run rerun 9876543 --failedStep 6 - Measure Pipeline Health
Track speed and reliability, since a pipeline that is slow or often red gets ignored. These commands use the GitHub CLI and jq.
# Pass and fail counts over the last 50 runsgh run list --workflow=ci.yaml --limit 50 --json conclusion \ | jq 'group_by(.conclusion) | map({result: .[0].conclusion, count: length})' # Duration of each job in one run, in secondsgh run view 9876543 --json jobs \ | jq '.jobs[] | {name: .name, seconds: ((.completedAt|fromdate) - (.startedAt|fromdate))}'A test that passes on re-run with no code change is flaky. Track how often runs need a re-run, and quarantine or fix those tests.
Production Best Practices & Common Pitfalls
| Mistake | Problem | Fix |
|---|---|---|
| One giant job with 20 steps | No parallelism, slow feedback | Split into jobs with needs |
| Slow checks before fast ones | Typos cost minutes to find | Order by cost, cheapest first |
| Rebuilding the image per environment | Production runs an untested build | Build once, promote the same image |
No retention-days on artifacts |
Storage fills up | Set retention on every artifact |
| Ignoring flaky tests | Failures stop meaning anything | Quarantine, fix, or delete them |
| Broad default token permissions | A compromised step can write to the repo | Set top-level permissions: contents: read |
- Set time budgets per stage. For example build under 5 minutes, tests under 8, deploy under 3. Treat a breach as a bug to fix.
- Cache dependencies. Caching
node_modulesor the package manager cache often saves minutes on every run. - Keep pipelines as code. Workflow files live in the repo, so changes to the pipeline are reviewed like any other change.
- Gate production. Use GitHub Environments with required reviewers for the approval step in Continuous Delivery.
Common MistakeTreating a slow green pipeline as healthy. A pipeline that passes after 45 minutes still fails its purpose, which is fast feedback.
RememberThe pipeline verifies good code, it does not replace code review. If reviewers lean on the pipeline to catch every edge case, the test suite grows slow and flaky.
SecurityNever print secrets or run
envin pipeline logs. Masking only matches exact strings, so derived or encoded values can leak. Register derived values with::add-mask::.
Quick Reference & Troubleshooting Commands
| Task | Command |
|---|---|
| List recent runs | gh run list --workflow=ci.yaml |
| View run details | gh run view RUN_ID |
| View failed logs only | gh run view RUN_ID --log-failed |
| Re-run failed jobs | gh run rerun RUN_ID --failed |
| Cancel a run | gh run cancel RUN_ID |
| Trigger manually | gh workflow run ci.yaml |
| Watch a run live | gh run watch RUN_ID |
| Symptom | Likely Cause | Fix |
|---|---|---|
| Job output is empty | Job missing from needs |
Add the producing job to needs |
| Deploy job never starts | An earlier job failed or was skipped | Check needs and if conditions |
| Runs on README edits | No path filter | Add paths-ignore |
| Artifact name conflict | Two jobs upload the same name | Use unique artifact names per run |
| Required check stays pending | Path filter skipped the workflow | Adjust filters or branch protection |