|
| 1 | +# IntelliWeather CI/CD Pipeline |
| 2 | +# Runs tests, builds Docker image, and optionally deploys to Kubernetes |
| 3 | + |
| 4 | +name: CI/CD Pipeline |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [main, develop] |
| 9 | + tags: ['v*'] |
| 10 | + pull_request: |
| 11 | + branches: [main] |
| 12 | + |
| 13 | +env: |
| 14 | + REGISTRY: ghcr.io |
| 15 | + IMAGE_NAME: ${{ github.repository }} |
| 16 | + |
| 17 | +jobs: |
| 18 | + # ==================== LINT & TEST ==================== |
| 19 | + test: |
| 20 | + name: Test |
| 21 | + runs-on: ubuntu-latest |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout code |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - name: Set up Python |
| 28 | + uses: actions/setup-python@v5 |
| 29 | + with: |
| 30 | + python-version: '3.12' |
| 31 | + cache: 'pip' |
| 32 | + |
| 33 | + - name: Install dependencies |
| 34 | + run: | |
| 35 | + python -m pip install --upgrade pip |
| 36 | + pip install -r requirements.txt |
| 37 | + pip install pytest pytest-cov pytest-asyncio flake8 black mypy |
| 38 | + |
| 39 | + - name: Lint with flake8 |
| 40 | + run: | |
| 41 | + # Stop the build if there are Python syntax errors or undefined names |
| 42 | + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics |
| 43 | + # Exit-zero treats all errors as warnings |
| 44 | + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=120 --statistics |
| 45 | + continue-on-error: true |
| 46 | + |
| 47 | + - name: Check formatting with black |
| 48 | + run: black --check --diff . || true |
| 49 | + continue-on-error: true |
| 50 | + |
| 51 | + - name: Type check with mypy |
| 52 | + run: mypy --ignore-missing-imports . || true |
| 53 | + continue-on-error: true |
| 54 | + |
| 55 | + - name: Run tests |
| 56 | + run: | |
| 57 | + pytest tests/ -v --cov=. --cov-report=xml --cov-report=term-missing |
| 58 | + env: |
| 59 | + DEBUG: "true" |
| 60 | + |
| 61 | + - name: Upload coverage reports |
| 62 | + uses: codecov/codecov-action@v3 |
| 63 | + with: |
| 64 | + files: ./coverage.xml |
| 65 | + fail_ci_if_error: false |
| 66 | + |
| 67 | + # ==================== BUILD DOCKER IMAGE ==================== |
| 68 | + build: |
| 69 | + name: Build |
| 70 | + runs-on: ubuntu-latest |
| 71 | + needs: test |
| 72 | + permissions: |
| 73 | + contents: read |
| 74 | + packages: write |
| 75 | + |
| 76 | + steps: |
| 77 | + - name: Checkout code |
| 78 | + uses: actions/checkout@v4 |
| 79 | + |
| 80 | + - name: Set up Docker Buildx |
| 81 | + uses: docker/setup-buildx-action@v3 |
| 82 | + |
| 83 | + - name: Log in to Container Registry |
| 84 | + if: github.event_name != 'pull_request' |
| 85 | + uses: docker/login-action@v3 |
| 86 | + with: |
| 87 | + registry: ${{ env.REGISTRY }} |
| 88 | + username: ${{ github.actor }} |
| 89 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + |
| 91 | + - name: Extract metadata |
| 92 | + id: meta |
| 93 | + uses: docker/metadata-action@v5 |
| 94 | + with: |
| 95 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 96 | + tags: | |
| 97 | + type=ref,event=branch |
| 98 | + type=ref,event=pr |
| 99 | + type=semver,pattern={{version}} |
| 100 | + type=semver,pattern={{major}}.{{minor}} |
| 101 | + type=sha,prefix= |
| 102 | + |
| 103 | + - name: Build and push Docker image |
| 104 | + uses: docker/build-push-action@v5 |
| 105 | + with: |
| 106 | + context: . |
| 107 | + push: ${{ github.event_name != 'pull_request' }} |
| 108 | + tags: ${{ steps.meta.outputs.tags }} |
| 109 | + labels: ${{ steps.meta.outputs.labels }} |
| 110 | + cache-from: type=gha |
| 111 | + cache-to: type=gha,mode=max |
| 112 | + platforms: linux/amd64,linux/arm64 |
| 113 | + |
| 114 | + # ==================== SECURITY SCAN ==================== |
| 115 | + security: |
| 116 | + name: Security Scan |
| 117 | + runs-on: ubuntu-latest |
| 118 | + needs: build |
| 119 | + if: github.event_name != 'pull_request' |
| 120 | + |
| 121 | + steps: |
| 122 | + - name: Checkout code |
| 123 | + uses: actions/checkout@v4 |
| 124 | + |
| 125 | + - name: Run Trivy vulnerability scanner |
| 126 | + uses: aquasecurity/trivy-action@master |
| 127 | + with: |
| 128 | + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} |
| 129 | + format: 'sarif' |
| 130 | + output: 'trivy-results.sarif' |
| 131 | + continue-on-error: true |
| 132 | + |
| 133 | + - name: Upload Trivy scan results |
| 134 | + uses: github/codeql-action/upload-sarif@v2 |
| 135 | + with: |
| 136 | + sarif_file: 'trivy-results.sarif' |
| 137 | + continue-on-error: true |
| 138 | + |
| 139 | + # ==================== DEPLOY TO STAGING ==================== |
| 140 | + deploy-staging: |
| 141 | + name: Deploy to Staging |
| 142 | + runs-on: ubuntu-latest |
| 143 | + needs: [build, security] |
| 144 | + if: github.ref == 'refs/heads/develop' |
| 145 | + environment: staging |
| 146 | + |
| 147 | + steps: |
| 148 | + - name: Checkout code |
| 149 | + uses: actions/checkout@v4 |
| 150 | + |
| 151 | + - name: Set up kubectl |
| 152 | + uses: azure/setup-kubectl@v3 |
| 153 | + |
| 154 | + - name: Configure kubectl |
| 155 | + run: | |
| 156 | + mkdir -p ~/.kube |
| 157 | + echo "${{ secrets.KUBE_CONFIG_STAGING }}" | base64 -d > ~/.kube/config |
| 158 | + if: ${{ secrets.KUBE_CONFIG_STAGING != '' }} |
| 159 | + |
| 160 | + - name: Deploy to staging |
| 161 | + run: | |
| 162 | + if [ -f ~/.kube/config ]; then |
| 163 | + kubectl set image deployment/intelliweather \ |
| 164 | + intelliweather=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} \ |
| 165 | + --namespace=staging || echo "Kubectl deployment skipped (no cluster configured)" |
| 166 | + else |
| 167 | + echo "Staging deployment skipped (no KUBE_CONFIG_STAGING secret)" |
| 168 | + fi |
| 169 | +
|
| 170 | + # ==================== DEPLOY TO PRODUCTION ==================== |
| 171 | + deploy-production: |
| 172 | + name: Deploy to Production |
| 173 | + runs-on: ubuntu-latest |
| 174 | + needs: [build, security] |
| 175 | + if: startsWith(github.ref, 'refs/tags/v') |
| 176 | + environment: production |
| 177 | + |
| 178 | + steps: |
| 179 | + - name: Checkout code |
| 180 | + uses: actions/checkout@v4 |
| 181 | + |
| 182 | + - name: Set up kubectl |
| 183 | + uses: azure/setup-kubectl@v3 |
| 184 | + |
| 185 | + - name: Configure kubectl |
| 186 | + run: | |
| 187 | + mkdir -p ~/.kube |
| 188 | + echo "${{ secrets.KUBE_CONFIG_PRODUCTION }}" | base64 -d > ~/.kube/config |
| 189 | + if: ${{ secrets.KUBE_CONFIG_PRODUCTION != '' }} |
| 190 | + |
| 191 | + - name: Deploy to production |
| 192 | + run: | |
| 193 | + if [ -f ~/.kube/config ]; then |
| 194 | + # Extract version from tag |
| 195 | + VERSION=${GITHUB_REF#refs/tags/} |
| 196 | + |
| 197 | + # Deploy using Helm |
| 198 | + helm upgrade --install intelliweather ./helm/intelliweather \ |
| 199 | + --namespace=production \ |
| 200 | + --set image.tag=$VERSION \ |
| 201 | + --wait --timeout=5m || echo "Helm deployment skipped" |
| 202 | + else |
| 203 | + echo "Production deployment skipped (no KUBE_CONFIG_PRODUCTION secret)" |
| 204 | + fi |
| 205 | +
|
| 206 | + # ==================== NOTIFY ==================== |
| 207 | + notify: |
| 208 | + name: Notify |
| 209 | + runs-on: ubuntu-latest |
| 210 | + needs: [test, build] |
| 211 | + if: always() |
| 212 | + |
| 213 | + steps: |
| 214 | + - name: Notify on success |
| 215 | + if: needs.test.result == 'success' && needs.build.result == 'success' |
| 216 | + run: | |
| 217 | + echo "✅ Pipeline succeeded!" |
| 218 | + echo "Tests: ${{ needs.test.result }}" |
| 219 | + echo "Build: ${{ needs.build.result }}" |
| 220 | + |
| 221 | + - name: Notify on failure |
| 222 | + if: needs.test.result == 'failure' || needs.build.result == 'failure' |
| 223 | + run: | |
| 224 | + echo "❌ Pipeline failed!" |
| 225 | + echo "Tests: ${{ needs.test.result }}" |
| 226 | + echo "Build: ${{ needs.build.result }}" |
0 commit comments