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:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
env:
NODE_VERSION: '20'
jobs:
deploy:
# ============================================================
# Test Job - Runs first, blocks deployment if tests fail
# ============================================================
test:
name: Run Tests
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:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build Docker image
run: |
docker build -t apartment-api:${{ github.sha }} .
docker tag apartment-api:${{ github.sha }} apartment-api:latest
- name: Deploy to server
run: |
# Copy files to deployment directory
mkdir -p /media/stephen/Storage_Linux/infrastructure/services/apartment-api
cp -r . /media/stephen/Storage_Linux/infrastructure/services/apartment-api/
# Navigate to deployment directory
cd /media/stephen/Storage_Linux/infrastructure/services/apartment-api
# Stop existing container if running
docker compose down || true
# Start new container
docker compose up -d
# Clean up old images
docker image prune -f
- name: Verify deployment
run: |
# Wait for container to start
sleep 15
# Check if container is running
docker ps | grep apartment-api
# Test health endpoint
curl -f http://localhost:3000/health || echo "API may still be starting..."
# Test API endpoint
curl -f http://localhost:3000/api/daily-summary || echo "API may still be loading data..."
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test -- --runInBand
env:
MONGO_URI: mongodb://localhost:27017
JWT_SECRET: test-jwt-secret-for-ci
NODE_ENV: test
# ============================================================
# Deploy Job - Only runs on main branch after tests pass
# ============================================================
deploy:
name: Deploy to Production
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy via SSH
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
# Navigate to deployment directory
cd ${{ secrets.DEPLOY_PATH }}
# Pull latest code
git fetch origin main
git reset --hard origin/main
# Rebuild and restart container
docker compose build --no-cache
docker compose up -d
# Clean up old images
docker image prune -f
# Wait for container to be healthy
echo "Waiting for container to start..."
sleep 10
# Verify container is running
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