|
| 1 | +name: CI/CD Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, develop ] |
| 6 | + pull_request: |
| 7 | + branches: [ main, develop ] |
| 8 | + |
| 9 | +jobs: |
| 10 | + # Job 1: Lint and Test |
| 11 | + test: |
| 12 | + name: Lint and Test |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + strategy: |
| 16 | + matrix: |
| 17 | + python-version: ['3.9', '3.10', '3.11'] |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Set up Python ${{ matrix.python-version }} |
| 24 | + uses: actions/setup-python@v4 |
| 25 | + with: |
| 26 | + python-version: ${{ matrix.python-version }} |
| 27 | + cache: 'pip' |
| 28 | + |
| 29 | + - name: Install dependencies |
| 30 | + run: | |
| 31 | + python -m pip install --upgrade pip |
| 32 | + pip install -r requirements.txt |
| 33 | + pip install pytest pytest-cov flake8 |
| 34 | + |
| 35 | + - name: Lint with flake8 |
| 36 | + run: | |
| 37 | + # Stop the build if there are Python syntax errors or undefined names |
| 38 | + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude=venv,env,.venv,.git,__pycache__ |
| 39 | + # Exit-zero treats all errors as warnings |
| 40 | + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude=venv,env,.venv,.git,__pycache__ |
| 41 | + |
| 42 | + - name: Run tests |
| 43 | + run: | |
| 44 | + pytest tests/ -v --cov=app --cov-report=xml --cov-report=term |
| 45 | + env: |
| 46 | + FLASK_ENV: testing |
| 47 | + SECRET_KEY: test-secret-key |
| 48 | + |
| 49 | + - name: Upload coverage to Codecov |
| 50 | + uses: codecov/codecov-action@v3 |
| 51 | + with: |
| 52 | + file: ./coverage.xml |
| 53 | + fail_ci_if_error: false |
| 54 | + |
| 55 | + # Job 2: Security Scan |
| 56 | + security: |
| 57 | + name: Security Scan |
| 58 | + runs-on: ubuntu-latest |
| 59 | + |
| 60 | + steps: |
| 61 | + - name: Checkout code |
| 62 | + uses: actions/checkout@v4 |
| 63 | + |
| 64 | + - name: Set up Python |
| 65 | + uses: actions/setup-python@v4 |
| 66 | + with: |
| 67 | + python-version: '3.11' |
| 68 | + |
| 69 | + - name: Install safety |
| 70 | + run: pip install safety |
| 71 | + |
| 72 | + - name: Run safety check |
| 73 | + run: safety check --json || true |
| 74 | + |
| 75 | + - name: Run Bandit security scan |
| 76 | + run: | |
| 77 | + pip install bandit |
| 78 | + bandit -r . -f json -o bandit-report.json || true |
| 79 | + |
| 80 | + - name: Upload security reports |
| 81 | + uses: actions/upload-artifact@v3 |
| 82 | + if: always() |
| 83 | + with: |
| 84 | + name: security-reports |
| 85 | + path: | |
| 86 | + bandit-report.json |
| 87 | +
|
| 88 | + # Job 3: Build Docker Image |
| 89 | + build: |
| 90 | + name: Build Docker Image |
| 91 | + runs-on: ubuntu-latest |
| 92 | + needs: [test, security] |
| 93 | + if: github.event_name == 'push' |
| 94 | + |
| 95 | + steps: |
| 96 | + - name: Checkout code |
| 97 | + uses: actions/checkout@v4 |
| 98 | + |
| 99 | + - name: Set up Docker Buildx |
| 100 | + uses: docker/setup-buildx-action@v3 |
| 101 | + |
| 102 | + - name: Login to Docker Hub |
| 103 | + if: github.ref == 'refs/heads/main' |
| 104 | + uses: docker/login-action@v3 |
| 105 | + with: |
| 106 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 107 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 108 | + |
| 109 | + - name: Build and push Docker image |
| 110 | + uses: docker/build-push-action@v5 |
| 111 | + with: |
| 112 | + context: . |
| 113 | + push: ${{ github.ref == 'refs/heads/main' }} |
| 114 | + tags: | |
| 115 | + ${{ secrets.DOCKER_USERNAME }}/project-management:latest |
| 116 | + ${{ secrets.DOCKER_USERNAME }}/project-management:${{ github.sha }} |
| 117 | + cache-from: type=gha |
| 118 | + cache-to: type=gha,mode=max |
| 119 | + |
| 120 | + - name: Test Docker image |
| 121 | + run: | |
| 122 | + docker build -t project-management:test . |
| 123 | + docker run -d -p 8000:8000 --name test-container project-management:test |
| 124 | + sleep 10 |
| 125 | + curl http://localhost:8000/health || exit 1 |
| 126 | + docker stop test-container |
| 127 | +
|
| 128 | + # Job 4: Deploy to Render (optional) |
| 129 | + deploy-render: |
| 130 | + name: Deploy to Render |
| 131 | + runs-on: ubuntu-latest |
| 132 | + needs: build |
| 133 | + if: github.ref == 'refs/heads/main' |
| 134 | + |
| 135 | + steps: |
| 136 | + - name: Trigger Render Deployment |
| 137 | + if: secrets.RENDER_DEPLOY_HOOK_URL != '' |
| 138 | + run: | |
| 139 | + curl -X POST "${{ secrets.RENDER_DEPLOY_HOOK_URL }}" |
| 140 | + |
| 141 | + - name: Deployment notification |
| 142 | + run: echo "Deployment triggered to Render" |
0 commit comments