A simple TypeScript/Express.js REST API with a complete CI/CD pipeline demonstrating DevOps best practices including security scanning, containerization, and Kubernetes deployment.
- Overview
- Technology Stack
- Project Structure
- Local Development
- Docker Usage
- CI/CD Pipeline
- Kubernetes Deployment
- API Documentation
- GitHub Secrets Configuration
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)
- Language: TypeScript
- Runtime: Node.js 18
- Framework: Express.js
- Testing: Jest
- Linting: ESLint
- Container: Docker
- Orchestration: Kubernetes (Kind)
- CI/CD: GitHub Actions
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
- Node.js 18 or higher
- npm or yarn
-
Clone the repository
git clone <repository-url> cd devops-ci-cd-project
-
Install dependencies
npm install
-
Run in development mode
npm run dev
-
Build the project
npm run build
-
Run the compiled application
npm start
-
Run tests
npm test -
Run linting
npm run lint
The API will be available at http://localhost:3000
docker build -t devops-todo-api:latest .docker run -p 3000:3000 devops-todo-api:latestcurl http://localhost:3000/api/healthThe CI pipeline (.github/workflows/ci.yml) includes:
- Checkout: Retrieve source code
- Setup Runtime: Install Node.js 18
- Linting: Run ESLint to enforce coding standards
- SAST: CodeQL analysis for security vulnerabilities
- SCA: npm audit for dependency vulnerabilities
- Unit Tests: Run Jest tests with coverage
- Build: Compile TypeScript to JavaScript
- Docker Build: Create container image
- Image Scan: Trivy vulnerability scanning
- Runtime Test: Validate container functionality
- Registry Push: Push image to DockerHub
The CD pipeline (.github/workflows/cd.yml) includes:
- Trigger: Runs after successful CI completion
- Setup Kind: Create Kubernetes cluster in GitHub Actions
- Deploy: Deploy application to Kubernetes
- DAST: Dynamic Application Security Testing
- Health Check: Verify deployment health
- Push to
masterormainbranch - Pull requests to
masterormain - Manual trigger via
workflow_dispatch
This project uses Kind (Kubernetes in Docker) for free Kubernetes deployment in GitHub Actions.
- β 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
If you want to test Kubernetes locally:
-
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
-
Create cluster
kind create cluster --name devops-project
-
Load Docker image
kind load docker-image devops-todo-api:latest --name devops-project
-
Deploy
kubectl apply -f k8s/deployment.yaml kubectl apply -f k8s/service.yaml
-
Port forward and test
kubectl port-forward svc/devops-todo-api-service 3000:3000 curl http://localhost:3000/api/health
http://localhost:3000/api
GET /api/healthResponse:
{
"success": true,
"message": "API is healthy",
"timestamp": "2024-01-01T00:00:00.000Z"
}GET /api/todosResponse:
{
"success": true,
"data": [
{
"id": "1",
"title": "Todo Title",
"description": "Todo Description",
"completed": false,
"createdAt": "2024-01-01T00:00:00.000Z"
}
],
"count": 1
}GET /api/todos/:idResponse:
{
"success": true,
"data": {
"id": "1",
"title": "Todo Title",
"description": "Todo Description",
"completed": false,
"createdAt": "2024-01-01T00:00:00.000Z"
}
}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"
}PATCH /api/todos/:id/toggleResponse:
{
"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 /api/todos/:idResponse:
{
"success": true,
"message": "Todo deleted successfully"
}To run the CI/CD pipelines, you need to configure the following GitHub Secrets:
-
DOCKERHUB_USERNAME
- Your DockerHub username
- Settings β Secrets and variables β Actions β New repository secret
-
DOCKERHUB_TOKEN
- Your DockerHub access token (not password)
- Create at: https://hub.docker.com/settings/security
- Settings β Secrets and variables β Actions β New repository secret
- Go to your GitHub repository
- Navigate to Settings β Secrets and variables β Actions
- Click New repository secret
- Add each secret with the exact names above
Before deploying, update k8s/deployment.yaml:
image: YOUR_DOCKERHUB_USERNAME/devops-todo-api:latestReplace YOUR_DOCKERHUB_USERNAME with your actual DockerHub username.
npm testnpm run test:cinpm run lint- Linting: Prevents technical debt, ensures code consistency
- SAST (CodeQL): Detects code-level security vulnerabilities (OWASP Top 10)
- SCA (npm audit): Identifies vulnerable dependencies in package.json
- Unit Tests: Prevents regressions, validates business logic
- Build: Compiles TypeScript, validates types, creates deployable artifact
- Docker Build: Packages application for consistent deployment
- Image Scan (Trivy): Prevents shipping containers with known vulnerabilities
- Runtime Test: Ensures container starts and responds correctly
- Registry Push: Enables downstream CD pipeline
- Kubernetes Deployment: Demonstrates production-like deployment
- DAST: Tests running application for runtime security issues
MIT
DevOps CI/CD Project for SST