Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 75 additions & 50 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -1,184 +1,210 @@
# =============================================
# Name: CodeCampus CI/CD Pipeline
# Description:
# This workflow performs CI/CD tasks including:
# - Testing and linting frontend/backend
# - Building applications
# - Building and pushing Docker images
# Triggers:
# - On push or pull request to main/sahil-dev/soro-dev branches
# - Manual trigger via workflow_dispatch
# =============================================

name: CodeCampus CI/CD Pipeline

on:
push:
branches: [ main, sahil-dev ]
branches: [ main, sahil-dev ] # Trigger on push to main or sahil-dev
pull_request:
branches: [ main, sahil-dev ]
workflow_dispatch: # Allow manual trigger
branches: [ main, sahil-dev ] # Trigger on PR to main or sahil-dev
workflow_dispatch: # Allow manual trigger from GitHub UI

env:
NODE_VERSION: '18.x'
NODE_VERSION: '18.x' # Node.js version to use in all jobs

jobs:
# =========================================================
# Job 1: Test & Quality Check (Backend)
# =========================================================
test-backend:
name: 🧪 Test & Quality Check (backend)
runs-on: ubuntu-latest

steps:
- name: 📥 Checkout code
uses: actions/checkout@v4
uses: actions/checkout@v4 # Checkout the codebase

- name: 🟢 Setup Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
uses: actions/setup-node@v4 # Setup Node.js environment
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: backend/package-lock.json

- name: 📦 Install dependencies
working-directory: backend
run: npm ci
run: npm ci # Clean install backend dependencies

- name: 🔧 TypeScript compilation check
working-directory: backend
run: npx tsc --noEmit
run: npx tsc --noEmit # Ensure TS compiles without errors

- name: 🧪 Run tests
working-directory: backend
run: npm test -- --coverage --watchAll=false
run: npm test -- --coverage --watchAll=false # Run unit tests with coverage
env:
CI: true

- name: 🔒 Security audit
working-directory: backend
run: npm audit --audit-level high
continue-on-error: true
run: npm audit --audit-level high # Perform dependency audit
continue-on-error: true # Do not fail pipeline if vulnerabilities found

# =========================================================
# Job 2: Test & Quality Check (Frontend)
# =========================================================
test-frontend:
name: 🧪 Test & Quality Check (frontend)
runs-on: ubuntu-latest

steps:
- name: 📥 Checkout code
uses: actions/checkout@v4

- name: 🟢 Setup Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: frontend/package-lock.json

- name: 📦 Install dependencies
working-directory: frontend
run: npm ci

- name: 🔍 Run ESLint
working-directory: frontend
run: npm run lint
run: npm run lint # Lint the codebase
continue-on-error: true

- name: 🔧 TypeScript compilation check
working-directory: frontend
run: npx tsc --noEmit

- name: 🧪 Run tests
working-directory: frontend
run: npm test -- --coverage --watchAll=false
env:
CI: true

- name: 🔒 Security audit
working-directory: frontend
run: npm audit --audit-level high
continue-on-error: true

# =========================================================
# Job 3: Build Applications (Backend)
# Depends on: test-backend
# =========================================================
build-backend:
name: 🏗️ Build Applications (backend)
runs-on: ubuntu-latest
needs: test-backend
needs: test-backend # Only run if backend tests pass

steps:
- name: 📥 Checkout code
uses: actions/checkout@v4

- name: 🟢 Setup Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: backend/package-lock.json

- name: 📦 Install dependencies
working-directory: backend
run: npm ci

- name: 🏗️ Build backend
working-directory: backend
run: npm run build
env:
NODE_ENV: production
NODE_ENV: production # Ensure build uses production settings

- name: 📄 Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: build-backend
path: backend/dist/
retention-days: 30
retention-days: 30 # Store build artifacts for 30 days

# =========================================================
# Job 4: Build Applications (Frontend)
# Depends on: test-frontend
# =========================================================
build-frontend:
name: 🏗️ Build Applications (frontend)
runs-on: ubuntu-latest
needs: test-frontend

steps:
- name: 📥 Checkout code
uses: actions/checkout@v4

- name: 🟢 Setup Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: frontend/package-lock.json

- name: 📦 Install dependencies
working-directory: frontend
run: npm ci

- name: 🏗️ Build frontend
working-directory: frontend
run: npm run build
env:
NODE_ENV: production
NEXT_PUBLIC_API_URL: ${{ secrets.NEXT_PUBLIC_API_URL || 'http://localhost:5000' }}
NEXT_PUBLIC_API_URL: ${{ secrets.NEXT_PUBLIC_API_URL || 'http://localhost:5000' }} # Use secret API URL or fallback

- name: 📄 Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: build-frontend
path: frontend/.next/
retention-days: 30

# =========================================================
# Job 5: Build & Push Docker Images
# Only on push to main or sahil-dev
# Depends on: build-backend, build-frontend
# =========================================================
docker:
name: 🐳 Build & Push Docker Images
runs-on: ubuntu-latest
needs: [build-backend, build-frontend]
if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/sahil-dev') && github.event_name == 'push'

strategy:
matrix:
component: [backend, frontend]
component: [backend, frontend] # Build both backend and frontend images

steps:
- name: 📥 Checkout code
uses: actions/checkout@v4

- name: 🐳 Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: 🔑 Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: 🏷️ Generate tags and labels
id: meta
uses: docker/metadata-action@v5
Expand All @@ -189,16 +215,15 @@ jobs:
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}

- name: 🏗️ Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ./${{ matrix.component }}
file: ./${{ matrix.component }}/Dockerfile
push: true
push: true # Push image to Docker Hub
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-from: type=gha # Enable caching
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64

platforms: linux/amd64,linux/arm64 # Build for multiple platforms
Loading