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,14 +1,34 @@
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
@ -16,45 +36,83 @@ jobs:
- 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'
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 # Navigate to deployment directory
cd /media/stephen/Storage_Linux/infrastructure/services/apartment-api cd ${{ secrets.DEPLOY_PATH }}
# Stop existing container if running # Pull latest code
docker compose down || true git fetch origin main
git reset --hard origin/main
# Start new container # Rebuild and restart container
docker compose build --no-cache
docker compose up -d docker compose up -d
# Clean up old images # Clean up old images
docker image prune -f 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 - name: Verify deployment
run: | uses: appleboy/ssh-action@v1.0.3
# Wait for container to start with:
sleep 15 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")
# Check if container is running if [ "$HTTP_CODE" = "200" ]; then
docker ps | grep apartment-api echo "Health check passed (HTTP $HTTP_CODE)"
else
# Test health endpoint echo "WARNING: Health check returned HTTP $HTTP_CODE"
curl -f http://localhost:3000/health || echo "API may still be starting..." echo "Container may still be initializing..."
fi
# Test API endpoint
curl -f http://localhost:3000/api/daily-summary || echo "API may still be loading data..."