Lab: Build a CI/CD Pipeline with GitHub Actions

Ad Space

Lab Objective

In this intermediate lab, you will:

  • Create a GitHub repository with a sample application
  • Set up GitHub Actions workflow
  • Configure automated testing
  • Build and push Docker images
  • Deploy to a staging environment

Estimated Time: 2-3 hours

Prerequisites

  • GitHub account
  • Basic Git knowledge
  • Understanding of Docker
  • Node.js installed (for sample app)

Architecture Overview

The pipeline will include:

  • Source Control: GitHub repository
  • CI/CD Platform: GitHub Actions
  • Build Stage: Docker image creation
  • Test Stage: Automated testing
  • Deploy Stage: Container deployment

Ad Space

Lab Steps

Step 1: Create Sample Application

Create a simple Node.js application:

mkdir ci-cd-lab cd ci-cd-lab npm init -y npm install express

Create app.js:

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.json({ message: 'Hello CI/CD!' });
});

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

Step 2: Create Dockerfile

Create Dockerfile:

FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . EXPOSE 3000 CMD ["node", "app.js"]

Step 3: Create Test File

Create test.js:

const assert = require('assert');

function testApp() {
  assert.strictEqual(1 + 1, 2, 'Math should work');
  console.log('All tests passed!');
}

testApp();

Step 4: Create GitHub Actions Workflow

Create .github/workflows/ci-cd.yml:

name: CI/CD Pipeline

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

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '18'
      - run: npm install
      - run: node test.js

  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build Docker image
        run: docker build -t myapp:${{ github.sha }} .
      - name: Test Docker image
        run: docker run --rm myapp:${{ github.sha }} node test.js

Step 5: Push to GitHub

Initialize Git and push:

git init git add . git commit -m "Initial commit" git branch -M main git remote add origin https://github.com/yourusername/ci-cd-lab.git git push -u origin main

Step 6: Verify Pipeline

Go to GitHub repository → Actions tab. You should see the workflow running. Monitor the progress and verify all steps complete successfully.

Step 7: Add Deployment Step

Extend the workflow with deployment (example):

  deploy:
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Deploy to staging
        run: |
          echo "Deploying to staging environment"
          # Add your deployment commands here

Validation Checklist

  • GitHub repository created and code pushed
  • GitHub Actions workflow file created
  • Workflow triggers on push
  • Test job runs and passes
  • Build job creates Docker image
  • All jobs complete successfully
  • Workflow runs on pull requests

Ad Space

Advanced Exercises

  • Add Docker image push to registry
  • Set up environment-specific deployments
  • Add code quality checks (ESLint, etc.)
  • Configure notifications on failure
  • Add caching for faster builds

Key Takeaways

  • CI/CD automates testing and deployment
  • GitHub Actions provides built-in CI/CD
  • Workflows can have multiple jobs and steps
  • Jobs can depend on each other
  • Conditional execution enables flexible pipelines