CI/CD Pipelines

Learn to build robust CI/CD pipelines for automated testing and deployment. Master popular tools like Jenkins, GitHub Actions, and GitLab CI.

What is CI/CD?

CI/CD stands for Continuous Integration and Continuous Delivery/Deployment. It is a set of practices that enable development teams to deliver code changes more frequently and reliably. CI/CD is one of the cornerstone practices of DevOps.

Continuous Integration (CI)

Continuous Integration is the practice of merging all developers' working copies to a shared mainline several times a day. Each integration triggers an automated build and test process to detect integration errors as quickly as possible.

  • Developers commit code frequently (at least daily)
  • Each commit triggers an automated build
  • Automated tests run on every build
  • Bugs are detected and fixed quickly

Continuous Delivery vs Continuous Deployment

Continuous Delivery (CD)

Continuous Delivery ensures that code is always in a deployable state. Every change that passes all stages of the production pipeline is ready to be released to customers. However, the final deployment to production is a manual decision.

Continuous Deployment

Continuous Deployment goes one step further - every change that passes all stages of the production pipeline is automatically released to customers. There is no human intervention.

CI/CD Pipeline Stages

  1. ๐Ÿ“ฅ Source - Code commit triggers the pipeline
  2. ๐Ÿ”จ Build - Compile code, create artifacts
  3. ๐Ÿงช Test - Run unit, integration, and e2e tests
  4. ๐Ÿ“Š Analysis - Code quality, security scans
  5. ๐Ÿ“ฆ Package - Create deployable artifacts (Docker images)
  6. ๐Ÿš€ Deploy - Deploy to staging/production
  7. ๐Ÿ‘๏ธ Monitor - Track application health

Popular CI/CD Tools

  • Jenkins - Open-source automation server
  • GitHub Actions - Native CI/CD for GitHub
  • GitLab CI/CD - Built into GitLab
  • CircleCI - Cloud-native CI/CD
  • Travis CI - Hosted CI service
  • Azure DevOps - Microsoft's CI/CD platform
  • AWS CodePipeline - AWS native CI/CD

CI/CD Best Practices

  • Keep pipelines fast (under 10 minutes if possible)
  • Run tests in parallel
  • Fail fast - stop pipeline on first failure
  • Use feature flags for gradual rollouts
  • Implement proper monitoring and alerting
  • Practice infrastructure as code for environments
๐Ÿ’ป GitHub Actions CI/CD Workflow
name: CI/CD Pipeline

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run tests
        run: pytest tests/ -v --cov=src
      
  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build Docker image
        run: docker build -t myapp:${{ github.sha }} .
      - name: Push to registry
        run: |
          docker tag myapp:${{ github.sha }} registry/myapp:latest
          docker push registry/myapp:latest

  deploy:
    needs: build
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to production
        run: kubectl apply -f k8s/
Output
โœ“ test (2m 15s) โœ“ Set up Python โœ“ Install dependencies โœ“ Run tests - 45 passed โœ“ build (1m 30s) โœ“ Build Docker image โœ“ Push to registry โœ“ deploy (45s) โœ“ Deploy to production Pipeline completed successfully!
๐Ÿ’ก This GitHub Actions workflow demonstrates a complete CI/CD pipeline with test, build, and deploy stages. The deploy stage only runs on the main branch.
๐ŸŽฏ

Test Your Knowledge

Answer these questions to check your understanding

1 What is the main difference between Continuous Delivery and Continuous Deployment?
๐Ÿ’ก Continuous Delivery requires manual approval for production deployment, while Continuous Deployment automatically deploys every change that passes all tests.