|
| 1 | +name: CI/CD Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, develop ] |
| 6 | + pull_request: |
| 7 | + branches: [ main ] |
| 8 | + |
| 9 | +env: |
| 10 | + PYTHON_VERSION: '3.10' |
| 11 | + |
| 12 | +jobs: |
| 13 | + test: |
| 14 | + name: Test & Validate |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Set up Python |
| 22 | + uses: actions/setup-python@v4 |
| 23 | + with: |
| 24 | + python-version: ${{ env.PYTHON_VERSION }} |
| 25 | + |
| 26 | + - name: Cache dependencies |
| 27 | + uses: actions/cache@v3 |
| 28 | + with: |
| 29 | + path: ~/.cache/pip |
| 30 | + key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} |
| 31 | + restore-keys: | |
| 32 | + ${{ runner.os }}-pip- |
| 33 | + |
| 34 | + - name: Install dependencies |
| 35 | + run: | |
| 36 | + python -m pip install --upgrade pip |
| 37 | + pip install -r requirements.txt |
| 38 | + |
| 39 | + - name: Create test database |
| 40 | + run: | |
| 41 | + # Create a test SQLite database |
| 42 | + touch kbi_production.db |
| 43 | + |
| 44 | + - name: Run linting |
| 45 | + run: | |
| 46 | + # Check code style (allow some flexibility) |
| 47 | + flake8 src/ --max-line-length=100 --exclude=__pycache__ --ignore=E402,W503 || true |
| 48 | + |
| 49 | + - name: Run tests |
| 50 | + env: |
| 51 | + DATABASE_URL: sqlite:///./kbi_production.db |
| 52 | + run: | |
| 53 | + pytest tests/ -v --cov=src --cov-report=term-missing |
| 54 | + |
| 55 | + - name: Check API starts |
| 56 | + run: | |
| 57 | + # Test that the API can start |
| 58 | + timeout 10s python run_api.py || code=$? |
| 59 | + if [ $code -eq 124 ]; then |
| 60 | + echo "✅ API started successfully (timeout expected)" |
| 61 | + else |
| 62 | + echo "❌ API failed to start" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | +
|
| 66 | + security-scan: |
| 67 | + name: Security Scan |
| 68 | + runs-on: ubuntu-latest |
| 69 | + |
| 70 | + steps: |
| 71 | + - uses: actions/checkout@v4 |
| 72 | + |
| 73 | + - name: Run security scan |
| 74 | + uses: pyupio/safety@v1 |
| 75 | + with: |
| 76 | + api-key: ${{ secrets.SAFETY_API_KEY }} |
| 77 | + continue-on-error: true |
| 78 | + |
| 79 | + deploy-staging: |
| 80 | + name: Deploy to Staging |
| 81 | + needs: [test] |
| 82 | + runs-on: ubuntu-latest |
| 83 | + if: github.ref == 'refs/heads/develop' |
| 84 | + |
| 85 | + steps: |
| 86 | + - name: Deploy to staging |
| 87 | + run: | |
| 88 | + echo "🚀 Deploying to staging environment..." |
| 89 | + # Add staging deployment here |
| 90 | +
|
| 91 | + deploy-production: |
| 92 | + name: Deploy to Production |
| 93 | + needs: [test, security-scan] |
| 94 | + runs-on: ubuntu-latest |
| 95 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 96 | + |
| 97 | + steps: |
| 98 | + - uses: actions/checkout@v4 |
| 99 | + |
| 100 | + - name: Deploy to EC2 |
| 101 | + env: |
| 102 | + EC2_HOST: ${{ secrets.EC2_HOST }} |
| 103 | + EC2_USER: ${{ secrets.EC2_USER }} |
| 104 | + EC2_KEY: ${{ secrets.EC2_SSH_KEY }} |
| 105 | + run: | |
| 106 | + # This is where we'll add EC2 deployment |
| 107 | + echo "🚀 Ready to deploy to production!" |
| 108 | + echo "Configure EC2 secrets in GitHub to enable auto-deployment" |
| 109 | + |
| 110 | + # Example deployment script (uncomment when ready): |
| 111 | + # echo "$EC2_KEY" > deploy_key.pem |
| 112 | + # chmod 600 deploy_key.pem |
| 113 | + # ssh -i deploy_key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST << 'EOF' |
| 114 | + # cd /home/ubuntu/KBILabs |
| 115 | + # git pull origin main |
| 116 | + # pip install -r requirements.txt |
| 117 | + # sudo systemctl restart kbi-api |
| 118 | + # EOF |
0 commit comments