Skip to content

Repository files navigation

DevOps CI/CD Project - Todo API

A simple TypeScript/Express.js REST API with a complete CI/CD pipeline demonstrating DevOps best practices including security scanning, containerization, and Kubernetes deployment.

πŸ“‹ Table of Contents

🎯 Overview

This project implements a Todo List REST API using TypeScript and Express.js, with a complete CI/CD pipeline that includes:

  • Code Quality: ESLint for linting
  • Security: CodeQL (SAST), npm audit (SCA), Trivy (container scanning), DAST
  • Testing: Jest unit tests
  • Containerization: Multi-stage Docker build
  • Deployment: Kubernetes deployment using Kind (free, runs in GitHub Actions)

πŸ›  Technology Stack

  • Language: TypeScript
  • Runtime: Node.js 18
  • Framework: Express.js
  • Testing: Jest
  • Linting: ESLint
  • Container: Docker
  • Orchestration: Kubernetes (Kind)
  • CI/CD: GitHub Actions

πŸ“ Project Structure

project-root/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       β”œβ”€β”€ ci.yml          # CI pipeline
β”‚       └── cd.yml          # CD pipeline
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ index.ts            # Application entry point
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── api.ts          # API routes
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   └── todoController.ts
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── todoService.ts
β”‚   └── models/
β”‚       └── todo.ts
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ todoService.test.ts
β”‚   └── todoController.test.ts
β”œβ”€β”€ k8s/
β”‚   β”œβ”€β”€ deployment.yaml
β”‚   └── service.yaml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ jest.config.js
└── README.md

πŸš€ Local Development

Prerequisites

  • Node.js 18 or higher
  • npm or yarn

Setup

  1. Clone the repository

    git clone <repository-url>
    cd devops-ci-cd-project
  2. Install dependencies

    npm install
  3. Run in development mode

    npm run dev
  4. Build the project

    npm run build
  5. Run the compiled application

    npm start
  6. Run tests

    npm test
  7. Run linting

    npm run lint

The API will be available at http://localhost:3000

🐳 Docker Usage

Build Docker Image

docker build -t devops-todo-api:latest .

Run Docker Container

docker run -p 3000:3000 devops-todo-api:latest

Test Container

curl http://localhost:3000/api/health

πŸ”„ CI/CD Pipeline

CI Pipeline Stages

The CI pipeline (.github/workflows/ci.yml) includes:

  1. Checkout: Retrieve source code
  2. Setup Runtime: Install Node.js 18
  3. Linting: Run ESLint to enforce coding standards
  4. SAST: CodeQL analysis for security vulnerabilities
  5. SCA: npm audit for dependency vulnerabilities
  6. Unit Tests: Run Jest tests with coverage
  7. Build: Compile TypeScript to JavaScript
  8. Docker Build: Create container image
  9. Image Scan: Trivy vulnerability scanning
  10. Runtime Test: Validate container functionality
  11. Registry Push: Push image to DockerHub

CD Pipeline Stages

The CD pipeline (.github/workflows/cd.yml) includes:

  1. Trigger: Runs after successful CI completion
  2. Setup Kind: Create Kubernetes cluster in GitHub Actions
  3. Deploy: Deploy application to Kubernetes
  4. DAST: Dynamic Application Security Testing
  5. Health Check: Verify deployment health

Pipeline Triggers

  • Push to master or main branch
  • Pull requests to master or main
  • Manual trigger via workflow_dispatch

☸️ Kubernetes Deployment

This project uses Kind (Kubernetes in Docker) for free Kubernetes deployment in GitHub Actions.

Why Kind?

  • βœ… 100% Free - No cloud account or credit card required
  • βœ… Runs in GitHub Actions - No external infrastructure needed
  • βœ… Full Kubernetes API - Complete K8s functionality
  • βœ… Perfect for CI/CD - Ephemeral clusters for each run

Local Kubernetes Testing (Optional)

If you want to test Kubernetes locally:

  1. Install Kind

    curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
    chmod +x ./kind
    sudo mv ./kind /usr/local/bin/kind
  2. Create cluster

    kind create cluster --name devops-project
  3. Load Docker image

    kind load docker-image devops-todo-api:latest --name devops-project
  4. Deploy

    kubectl apply -f k8s/deployment.yaml
    kubectl apply -f k8s/service.yaml
  5. Port forward and test

    kubectl port-forward svc/devops-todo-api-service 3000:3000
    curl http://localhost:3000/api/health

πŸ“š API Documentation

Base URL

http://localhost:3000/api

Endpoints

Health Check

GET /api/health

Response:

{
  "success": true,
  "message": "API is healthy",
  "timestamp": "2024-01-01T00:00:00.000Z"
}

Get All Todos

GET /api/todos

Response:

{
  "success": true,
  "data": [
    {
      "id": "1",
      "title": "Todo Title",
      "description": "Todo Description",
      "completed": false,
      "createdAt": "2024-01-01T00:00:00.000Z"
    }
  ],
  "count": 1
}

Get Todo by ID

GET /api/todos/:id

Response:

{
  "success": true,
  "data": {
    "id": "1",
    "title": "Todo Title",
    "description": "Todo Description",
    "completed": false,
    "createdAt": "2024-01-01T00:00:00.000Z"
  }
}

Create Todo

POST /api/todos
Content-Type: application/json

{
  "title": "New Todo",
  "description": "Todo description"
}

Response:

{
  "success": true,
  "data": {
    "id": "1",
    "title": "New Todo",
    "description": "Todo description",
    "completed": false,
    "createdAt": "2024-01-01T00:00:00.000Z"
  },
  "message": "Todo created successfully"
}

Toggle Todo Status

PATCH /api/todos/:id/toggle

Response:

{
  "success": true,
  "data": {
    "id": "1",
    "title": "Todo Title",
    "description": "Todo Description",
    "completed": true,
    "createdAt": "2024-01-01T00:00:00.000Z"
  },
  "message": "Todo status updated successfully"
}

Delete Todo

DELETE /api/todos/:id

Response:

{
  "success": true,
  "message": "Todo deleted successfully"
}

πŸ” GitHub Secrets Configuration

To run the CI/CD pipelines, you need to configure the following GitHub Secrets:

Required Secrets

  1. DOCKERHUB_USERNAME

    • Your DockerHub username
    • Settings β†’ Secrets and variables β†’ Actions β†’ New repository secret
  2. DOCKERHUB_TOKEN

How to Set Secrets

  1. Go to your GitHub repository
  2. Navigate to Settings β†’ Secrets and variables β†’ Actions
  3. Click New repository secret
  4. Add each secret with the exact names above

Update Kubernetes Deployment

Before deploying, update k8s/deployment.yaml:

image: YOUR_DOCKERHUB_USERNAME/devops-todo-api:latest

Replace YOUR_DOCKERHUB_USERNAME with your actual DockerHub username.

πŸ§ͺ Testing

Run All Tests

npm test

Run Tests with Coverage

npm run test:ci

Run Linting

npm run lint

πŸ“Š CI/CD Pipeline Explanation

Why Each Stage Exists

  1. Linting: Prevents technical debt, ensures code consistency
  2. SAST (CodeQL): Detects code-level security vulnerabilities (OWASP Top 10)
  3. SCA (npm audit): Identifies vulnerable dependencies in package.json
  4. Unit Tests: Prevents regressions, validates business logic
  5. Build: Compiles TypeScript, validates types, creates deployable artifact
  6. Docker Build: Packages application for consistent deployment
  7. Image Scan (Trivy): Prevents shipping containers with known vulnerabilities
  8. Runtime Test: Ensures container starts and responds correctly
  9. Registry Push: Enables downstream CD pipeline
  10. Kubernetes Deployment: Demonstrates production-like deployment
  11. DAST: Tests running application for runtime security issues

πŸ“ License

MIT

πŸ‘€ Author

DevOps CI/CD Project for SST

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages