name: CI on: push: branches: [main, develop] pull_request: branches: [main, develop] schedule: # Weekly security scan on Mondays at 00:00 UTC - cron: '0 0 * * 1' jobs: # ── Stage 1: Lint (gates everything else) ──────────────────────────────── lint: name: Lint runs-on: ubuntu-latest permissions: contents: read steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Python 3.10 uses: actions/setup-python@v5 with: python-version: '3.10' - name: Cache pip packages uses: actions/cache@v4 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('backend/requirements.txt') }} restore-keys: ${{ runner.os }}-pip- - name: Install linting tools run: | python -m pip install --upgrade pip pip install black isort flake8 pylint cd backend && pip install -r requirements.txt - name: Black – format check run: black --check backend/app - name: isort – import order check run: isort --check-only backend/app - name: Flake8 run: flake8 backend/app - name: Pylint run: pylint backend/app continue-on-error: true # ── Stage 2 (parallel): Test, Security, CodeQL, Dependency Review ──────── test: name: Test runs-on: ubuntu-latest needs: lint permissions: contents: read steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Python 3.10 uses: actions/setup-python@v5 with: python-version: '3.10' - name: Cache pip packages uses: actions/cache@v4 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('backend/requirements.txt') }} restore-keys: ${{ runner.os }}-pip- - name: Install dependencies run: | python -m pip install --upgrade pip cd backend && pip install -r requirements.txt - name: Run tests with coverage run: | cd backend pytest --cov=app --cov-report=xml --cov-report=term-missing - name: Upload coverage to Codecov uses: codecov/codecov-action@v4 with: file: ./backend/coverage.xml flags: unittests name: codecov-umbrella fail_ci_if_error: false security: name: Security Scan runs-on: ubuntu-latest needs: lint permissions: contents: read steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Python 3.10 uses: actions/setup-python@v5 with: python-version: '3.10' - name: Cache pip packages uses: actions/cache@v4 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('backend/requirements.txt') }} restore-keys: ${{ runner.os }}-pip- - name: Install security tools run: | python -m pip install --upgrade pip pip install bandit pip-audit cd backend && pip install -r requirements.txt - name: Bandit – Python security linter run: bandit -r backend/app -f json -o bandit-report.json continue-on-error: true - name: Upload Bandit report uses: actions/upload-artifact@v4 if: always() with: name: bandit-security-report path: bandit-report.json - name: pip-audit – dependency vulnerability check run: pip-audit --requirement backend/requirements.txt continue-on-error: true codeql: name: CodeQL Analysis runs-on: ubuntu-latest needs: lint # CodeQL is only meaningful on real pushes/PRs, not the weekly schedule alone if: github.event_name != 'schedule' permissions: actions: read contents: read security-events: write steps: - name: Checkout repository uses: actions/checkout@v4 - name: Initialize CodeQL uses: github/codeql-action/init@v3 with: languages: python queries: security-and-quality - name: Autobuild uses: github/codeql-action/autobuild@v3 - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v3 with: category: /language:python dependency-review: name: Dependency Review runs-on: ubuntu-latest needs: lint if: github.event_name == 'pull_request' permissions: contents: read steps: - name: Checkout code uses: actions/checkout@v4 - name: Dependency Review uses: actions/dependency-review-action@v4 with: fail-on-severity: moderate # ── Stage 3: Docker Build & Publish (only on main push) ────────────────── docker: name: Docker Build & Publish runs-on: ubuntu-latest needs: [test, security] if: github.event_name == 'push' && github.ref == 'refs/heads/main' permissions: contents: read packages: write steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GitHub Container Registry uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata for Docker id: meta uses: docker/metadata-action@v5 with: images: ghcr.io/${{ github.repository }} tags: | type=ref,event=branch type=sha,prefix= type=raw,value=latest,enable={{is_default_branch}} - name: Build and push Docker image uses: docker/build-push-action@v6 with: context: ./backend file: ./backend/Dockerfile push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max