Skip to main content

DevOps & CI/CD

What You’ll Learn

By the end of this chapter, you’ll understand:
  • What DevOps actually means - Why companies deploy software 100x faster than they did 10 years ago
  • CI/CD pipelines - How code automatically goes from your laptop to production in minutes (not weeks!)
  • GitHub Actions - How to write your first automated deployment pipeline
  • Infrastructure as Code - How to create entire cloud environments with just a text file
  • Deployment strategies - Blue-Green, Canary, Rolling deploys (and when to use each)
  • Real-world costs - Why bad deployments cost $1 million per hour

Introduction: What is DevOps? (Start Here if You’re New)

The Problem Before DevOps

Traditional software deployment (The slow, painful way):
Real Example:
  • Knight Capital Group (2012)
  • Manual deployment error
  • Lost $440 million in 45 minutes
  • Company went bankrupt

The Solution: DevOps + Automation

Modern deployment with DevOps:
DevOps Benefits (Real Numbers): Source: Google DORA (DevOps Research and Assessment) Report 2023

What is DevOps? (Simple Explanation)

DevOps = Development + Operations working together using automation Think of it like a factory assembly line: Before DevOps (Manual process):
With DevOps (Automated assembly line):
Same concept for software:
  • Automation replaces manual steps
  • Faster, more reliable, cheaper
  • Developers and Operations use the same tools/processes

What is CI/CD? (Breaking It Down)

CI/CD = Continuous Integration + Continuous Deployment 1. Continuous Integration (CI) = “Merge code often, test automatically” The Old Way (Integration Hell):
With CI (Merge small changes daily):
2. Continuous Deployment (CD) = “Deploy to production automatically” The Old Way (Manual deployment):
With CD (Automated deployment):

The Cost of NOT Using DevOps

Real-World Disasters: Example 1: Healthcare.gov Launch (2013)
  • Problem: Manual deployment, no automation, no testing
  • Result: Website crashed on day 1, couldn’t handle 250 users
  • Cost: $1.7 billion to fix
  • Prevention cost: ~$10 million (proper DevOps/CI/CD)
  • ROI of DevOps: 170x
Example 2: British Airways IT Outage (2017)
  • Problem: Manual data center migration, poor testing
  • Result: 75,000 passengers stranded, 726 flights canceled
  • Cost: $170 million
  • Prevention cost: ~$1 million (automated testing + deployment)
  • ROI of DevOps: 170x
Example 3: Amazon Prime Day 2018
  • Problem: Deployment bug in new feature
  • With DevOps: Detected in 2 minutes, rolled back automatically
  • Cost: $4 million in lost sales
  • Without DevOps (if took 1 hour): $120 million in lost sales
Average cost of downtime (Gartner 2023):
  • Small business: $8,500/hour
  • Medium business: $74,000/hour
  • Enterprise: $1,000,000+/hour

How CI/CD Actually Works (Behind the Scenes)

Real-World Example: Deploying a Node.js web app Step-by-Step Pipeline:

What is DevOps? DevOps is a combination of “Development” and “Operations.” It’s a culture and set of practices that brings together software development (Dev) and IT operations (Ops) to shorten the development lifecycle and deliver high-quality software faster. What is CI/CD?
  • CI (Continuous Integration): Automatically build and test code every time someone commits changes. Catches bugs early.
  • CD (Continuous Deployment/Delivery): Automatically deploy code to production after tests pass. Reduces manual errors.
Why DevOps Matters:
  • Before DevOps: Developers write code → Hand it to operations → Operations manually deploy → Takes days/weeks → Errors happen
  • With DevOps: Developers commit code → Pipeline automatically builds, tests, deploys → Takes minutes → Fewer errors
Real-World Analogy:
  • Old Way: Like building a house. You build it, then call inspectors, then fix issues, then move in (weeks/months).
  • DevOps Way: Like a factory assembly line. Each step is automated, tested, and quality-checked automatically (hours/minutes).
Azure CI/CD Pipeline

1. Understanding CI/CD Pipelines

What is a Pipeline? A pipeline is a series of automated steps that take your code from source control to production. Think of it as a recipe: each step (ingredient) must complete successfully before moving to the next. Pipeline Stages:
Why Automate?
  • Speed: Deploy in minutes instead of hours/days
  • Consistency: Same process every time (no human error)
  • Quality: Tests run automatically (catch bugs before production)
  • Traceability: See exactly what was deployed and when

Azure DevOps vs GitHub Actions

Which Should You Choose? | Best For | Enterprise, Microsoft stack | Open source, GitHub-first |
[!WARNING] Gotcha: Secrets in Logs Never print environment variables or secrets to the console for debugging. Once a secret is in the build logs, it is compromised forever. Use Azure Key Vault to inject secrets at runtime without exposing them.
[!TIP] Jargon Alert: Idempotency A fancy word for “safe to run twice.” Good Infrastructure as Code (IaC) is idempotent: if you deploy the same Bicep file 100 times, nothing changes after the first time. Bad scripts create 100 duplicate resources.

2. GitHub Actions: Complete Guide

What is GitHub Actions? GitHub Actions is a CI/CD platform built into GitHub. You define workflows (pipelines) using YAML files in your repository. When you push code, GitHub automatically runs your workflows. Why GitHub Actions?
  • ✅ Free for public repositories
  • ✅ 2,000 free minutes/month for private repos
  • ✅ Integrated with GitHub (no separate tool)
  • ✅ Huge marketplace of pre-built actions
  • ✅ Easy to get started
[!WARNING] Gotcha: GitHub Actions Minutes Can Run Out Free tier gives 2,000 minutes/month. If your workflows run frequently or take long, you’ll hit the limit. Self-hosted runners are free (unlimited), but you manage the infrastructure. Monitor your usage in Settings → Billing.
[!TIP] Jargon Alert: Workflow vs Action Workflow: The entire pipeline (the YAML file). Defines when to run and what jobs to execute. Action: A reusable step (like actions/checkout@v3). Think of it as a pre-built function you can call. Actions are published to the GitHub Marketplace.
[!INFO] Aside: GitHub Actions Pricing
  • Public repos: Unlimited free minutes
  • Private repos: 2,000 free minutes/month, then $0.008/minute
  • Self-hosted runners: Free (unlimited), but you pay for the VM/infrastructure

Step-by-Step: Creating Your First GitHub Actions Workflow

Let’s create a complete CI/CD pipeline from scratch:

Step 1: Create Workflow File

Where? Create .github/workflows/ directory in your repository root.
File Structure:

Step 2: Basic Workflow Structure

What Each Part Does:
What: Name of your workflow (shows in GitHub Actions tab)Example: name: CI/CD PipelineWhy: Helps identify workflows when you have multiple

Step 3: Complete CI/CD Workflow Example

Here’s a complete workflow that builds, tests, and deploys a Node.js app to Azure App Service:
What This Workflow Does:
  1. Triggers: Runs on push to main or manual trigger
  2. Build Job:
    • Checks out code
    • Installs dependencies (with caching)
    • Runs linter
    • Runs tests
    • Builds application
    • Uploads build artifacts
    • Scans for security vulnerabilities
  3. Deploy Job (only if build succeeds):
    • Downloads build artifacts
    • Logs into Azure
    • Deploys to App Service

Step 4: Configure Azure Credentials

What are Secrets? Sensitive data (passwords, API keys) stored securely in GitHub. Never commit secrets to code! How to Set Up Azure Credentials:
  1. Create Service Principal in Azure:
  1. Add Secret to GitHub:
    • Go to your GitHub repository
    • Settings → Secrets and variables → Actions
    • Click “New repository secret”
    • Name: AZURE_CREDENTIALS
    • Value: Paste the JSON from step 1
    • Click “Add secret”
Why Service Principal? It’s a “service account” that GitHub Actions uses to authenticate to Azure. More secure than using your personal credentials.

Step 5: Understanding Workflow Features

What: Run the same job with different configurations (e.g., test on Node 16, 18, 20)Example:
Result: Runs 6 jobs (3 Node versions × 2 OSes) in parallel

Step 6: Advanced: Multi-Environment Deployment

Deploy to Dev → Staging → Production:
GitHub Environments: Configure protection rules (required reviewers, wait timers) in repository settings.

3. Azure Pipelines (Alternative to GitHub Actions)

When to Use Azure Pipelines:
  • Your organization already uses Azure DevOps
  • Need advanced enterprise features (test plans, work items)
  • Want integrated project management

YAML Pipeline Example


3. Infrastructure as Code

Bicep Example

Deploy with Azure CLI


4. GitOps

Use Git as single source of truth for infrastructure and applications.

5. Deployment Strategies

Choosing the right deployment strategy can be the difference between a seamless release and a production outage.

Strategy Comparison


Blue-Green Deployment

Deploy to an identical “green” environment while “blue” runs production. Swap traffic instantly.

Azure Implementation

Using Azure App Service Deployment Slots:
Key Features:
  • Auto-Swap: Automatically swap after successful deployment.
  • Swap with Preview: Test in production config before committing.
  • Instant Rollback: If issues detected, swap back in <5 seconds.
[!WARNING] Gotcha: Database Migrations Blue-green works great for stateless apps, but database schema changes are tricky. Both blue and green must support the current schema. Use backward-compatible migrations (add columns, don’t drop).
Real-World Example: A bank deploys during business hours using blue-green. New code goes to green, runs smoke tests, then swaps. If fraud detection service fails, they swap back in 3 seconds—no customer impact.

Canary Deployment

Route a small percentage of traffic (5%) to the new version. If metrics look good, gradually increase to 100%.

AKS with Flagger

How It Works:
  1. Deploy new version (myapp-v2).
  2. Flagger routes 10% of traffic to v2.
  3. Wait 1 minute, check metrics (success rate >99%, latency <500ms).
  4. If healthy, increase to 20%, then 30%, etc.
  5. If metrics fail, automatic rollback to v1.
Azure Front Door Weighted Routing:
[!TIP] Best Practice: Canary Metrics Don’t just monitor HTTP 500s. Track business metrics like “checkout completion rate” or “login success rate.” A technically healthy service might still break user workflows.

Rolling Deployment

Update instances one at a time. If any instance fails, stop the rollout.

AKS Rolling Update

How It Works:
  1. Kubernetes creates 2 new pods (v2) while 10 old pods (v1) still run.
  2. Waits for new pods to pass readiness probe.
  3. Terminates 1 old pod, creates 1 new pod.
  4. Repeats until all 10 pods are running v2.
Rollback:
[!WARNING] Gotcha: PodDisruptionBudget Without a PDB, Kubernetes might terminate too many pods during a node upgrade, causing an outage. Always set:

Feature Flags (Feature Toggles)

Deploy code with new features disabled. Enable features gradually via configuration.

Azure App Configuration + Feature Flags

Gradual Rollout with Targeting:
Rollout Plan:
  1. Enable for internal employees (100%).
  2. Enable for beta users (100%).
  3. Enable for 10% of general users.
  4. Monitor metrics for 24 hours.
  5. Increase to 50%, then 100%.
  6. Remove feature flag from code after 2 weeks.
[!TIP] Best Practice: Feature Flag Lifecycle Feature flags are temporary. Never let them live forever. Set a TTL (time-to-live) and remove the flag once the feature is stable. Otherwise, your code becomes a graveyard of if (featureFlag) checks.

Comparison: Real-World Scenario

Scenario: Deploying a payment processing service update. Recommended Approach: Use Canary + Feature Flags. Deploy code to production with the feature flag OFF. Enable the flag for 1% of users (canary), monitor for issues, then gradually roll out to 100%.

Decision Flowchart


6. Interview Questions

Beginner Level

Answer:
  • CI (Continuous Integration): Automating the build and testing of code every time a team member commits changes to version control.
  • CD (Continuous Deployment/Delivery): Automating the release of validated code to a repository or production environment.
Answer:
  • Azure DevOps: Complete suite (Boards, Repos, Pipelines, Test Plans, Artifacts). Great for enterprise management and tracking.
  • GitHub Actions: Workflow automation engine built into GitHub. Closer to the code, massive open-source community, simpler for CI.

Intermediate Level

Answer: Managing and provisioning infrastructure through code (Bicep/Terraform) rather than manual processes. Benefits:
  • Consistency: Same environment every time.
  • Version Control: Track history of changes.
  • Speed: Deploy entire environments in minutes.
  • Disaster Recovery: Re-create environments from scratch easily.
Answer: A machine that you set up and manage to run pipeline jobs. Use Cases:
  • Build needs access to private resources (VNet).
  • Specialized software/hardware requirements.
  • Caching large dependencies (faster builds).

Advanced Level

Answer:
  1. Dependency Scanning: Check NuGet/NPM packages for vulnerabilities (GitHub Dependabot).
  2. Secret Scanning: Detect committed credentials.
  3. Container Scanning: Scan Docker images for CVEs (Trivy/Defender).
  4. Code Signing: Sign build artifacts to ensure integrity.
  5. Least Privilege: Pipeline service connections should have minimal permissions.

6. Key Takeaways

Automate Everything

If you do it twice, automate it. Manual deployments are forbidden in production.

Infrastructure as Code

Treat infrastructure like software. Use Bicep or Terraform for reproducible environments.

Shift Left

Test security and quality early in the pipeline, not after deployment.

GitOps

Git is the single source of truth. Deployment reflects the state of the main branch.

Ephemerality

Build agents and environments should be disposable. Don’t rely on snowflake servers.

Next Steps

Continue to Chapter 12

Master Azure cost optimization and FinOps strategies