CI/CD Pipeline
Continuous Integration and Continuous Deployment (CI/CD) automate the process of testing, building, and deploying your code. This guide covers setting up a modern pipeline.
What is CI/CD?
- CI (Continuous Integration) — Automatically test and build code on every push
- CD (Continuous Deployment) — Automatically deploy validated builds to production
GitHub Actions Example
Create .github/workflows/deploy.yml:
name: CI/CD Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint
- name: Run tests
run: npm test
build:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build Docker image
run: docker build -t my-app:${{ github.sha }} .
- name: Push to registry
run: |
echo ${{ secrets.REGISTRY_TOKEN }} | docker login -u ${{ secrets.REGISTRY_USER }} --password-stdin
docker push my-app:${{ github.sha }}
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Deploy to production
run: |
# Your deployment script here
echo "Deploying version ${{ github.sha }}"Pipeline Stages
A typical CI/CD pipeline follows these stages:
Code Push → Lint → Test → Build → Deploy (Staging) → Deploy (Production)| Stage | Purpose | Failure Action |
|---|---|---|
| Lint | Code style checks | Block merge |
| Test | Unit & integration tests | Block merge |
| Build | Compile & package | Block deploy |
| Deploy Staging | Test in staging env | Block production |
| Deploy Production | Release to users | Rollback |
Best Practices
- Run tests on every PR — Never merge untested code
- Keep pipelines fast — Target under 10 minutes for feedback
- Use caching — Cache dependencies between runs
- Deploy incrementally — Use canary or blue-green deployments
- Monitor after deploy — Set up alerts for error rate spikes
- Store secrets securely — Use your CI platform’s secret management