Add proper CI/CD pipeline with testing
Some checks failed
CI/CD Pipeline - Apartment API / Run Tests (push) Failing after 12s
CI/CD Pipeline - Apartment API / Deploy to Production (push) Has been skipped

- Separate test and deploy jobs
- Tests run first and block deployment on failure
- Uses MongoDB service container for tests
- SSH-based deployment for security and flexibility
- Health check verification after deployment
- Requires secrets: SSH_HOST, SSH_USER, SSH_PRIVATE_KEY, DEPLOY_PATH
This commit is contained in:
2026-01-22 14:33:59 -07:00
parent ccda2be551
commit 94bbacb3a2

View File

@ -1,60 +1,118 @@
name: Deploy Apartment API name: CI/CD Pipeline - Apartment API
on: on:
push: push:
branches: [ main ] branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch: workflow_dispatch:
env:
NODE_VERSION: '20'
jobs: jobs:
deploy: # ============================================================
# Test Job - Runs first, blocks deployment if tests fail
# ============================================================
test:
name: Run Tests
runs-on: ubuntu-latest runs-on: ubuntu-latest
services:
mongodb:
image: mongo:7
ports:
- 27017:27017
options: >-
--health-cmd "mongosh --eval 'db.runCommand(\"ping\").ok'"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Setup Node.js - name: Setup Node.js
uses: actions/setup-node@v4 uses: actions/setup-node@v4
with: with:
node-version: '18' node-version: ${{ env.NODE_VERSION }}
cache: 'npm' cache: 'npm'
- name: Install dependencies - name: Install dependencies
run: npm ci run: npm ci
- name: Build Docker image - name: Run tests
run: | run: npm test -- --runInBand
docker build -t apartment-api:${{ github.sha }} . env:
docker tag apartment-api:${{ github.sha }} apartment-api:latest MONGO_URI: mongodb://localhost:27017
JWT_SECRET: test-jwt-secret-for-ci
NODE_ENV: test
- name: Deploy to server # ============================================================
run: | # Deploy Job - Only runs on main branch after tests pass
# Copy files to deployment directory # ============================================================
mkdir -p /media/stephen/Storage_Linux/infrastructure/services/apartment-api deploy:
cp -r . /media/stephen/Storage_Linux/infrastructure/services/apartment-api/ name: Deploy to Production
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
# Navigate to deployment directory steps:
cd /media/stephen/Storage_Linux/infrastructure/services/apartment-api - name: Checkout code
uses: actions/checkout@v4
# Stop existing container if running - name: Deploy via SSH
docker compose down || true uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT || 22 }}
script: |
set -e
# Start new container # Navigate to deployment directory
docker compose up -d cd ${{ secrets.DEPLOY_PATH }}
# Clean up old images # Pull latest code
docker image prune -f git fetch origin main
git reset --hard origin/main
- name: Verify deployment # Rebuild and restart container
run: | docker compose build --no-cache
# Wait for container to start docker compose up -d
sleep 15
# Check if container is running # Clean up old images
docker ps | grep apartment-api docker image prune -f
# Test health endpoint # Wait for container to be healthy
curl -f http://localhost:3000/health || echo "API may still be starting..." echo "Waiting for container to start..."
sleep 10
# Test API endpoint # Verify container is running
curl -f http://localhost:3000/api/daily-summary || echo "API may still be loading data..." if docker compose ps | grep -q "Up"; then
echo "Container is running successfully"
else
echo "ERROR: Container failed to start"
docker compose logs --tail=50
exit 1
fi
- name: Verify deployment
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT || 22 }}
script: |
# Test health endpoint via Traefik
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://apartments.maverickapplications.com/api/health || echo "000")
if [ "$HTTP_CODE" = "200" ]; then
echo "Health check passed (HTTP $HTTP_CODE)"
else
echo "WARNING: Health check returned HTTP $HTTP_CODE"
echo "Container may still be initializing..."
fi