# Name of the workflow displayed in the GitHub Actions tab name: Python Compute Engine CI Pipeline # Controls when the workflow will run on: push: branches: [ main, develop ] # Run on direct pushes to main or develop pull_request: branches: [ main ] # Run on pull requests targeting main jobs: code-quality-and-analysis: # Specify the virtual machine environment runs-on: ubuntu-latest steps: # 1. Checkout code (fetch-depth: 0 is required for SonarQube blame/history data) - name: Checkout repository uses: actions/checkout@v4 with: fetch-depth: 0 # 2. Set up the Python environment - name: Set up Python uses: actions/setup-python@v5 with: python-version: '3.12' cache: 'pip' # Caches pip dependencies to speed up subsequent runs # 3. Install project dependencies and static analysis tools - name: Install dependencies run: | python -m pip install --upgrade pip pip install ruff mypy pytest coverage # Installed coverage directly instead of pytest-cov # Uncomment below if you have a requirements file # pip install -r requirements.txt # 4. Run Ruff for fast linting and code formatting checks - name: Run Ruff (Linter & Formatter) run: | ruff check . # Checks for linting errors ruff format --check . # Verifies code formatting without modifying files # 5. Run Mypy for static type checking - name: Run Mypy (Type Checking) run: | mypy . # Analyzes type annotations across the codebase # 6. Run tests via coverage.py and export XML report for SonarQube - name: Run Tests with Coverage run: | coverage run -m pytest # Executes pytest while tracking line coverage coverage xml # Generates coverage.xml from the collected data # 7. Run SonarQube Scanner for deep code quality and security analysis - name: SonarQube Scan uses: SonarSource/sonarqube-scan-action@v3 env: # Sensitive credentials stored in GitHub Secrets SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} # Non-sensitive config stored in GitHub Repository Variables SONAR_PROJECT_KEY: ${{ vars.SONAR_PROJECT_KEY }} with: args: > -Dsonar.projectKey=${{ env.SONAR_PROJECT_KEY }} -Dsonar.sources=. -Dsonar.python.coverage.reportPaths=coverage.xml