|
| 1 | +name: "Backend + API CI/CD" |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - 'backend-api/**' |
| 8 | + pull_request: |
| 9 | + branches: [main] |
| 10 | + paths: |
| 11 | + - 'backend-api/**' |
| 12 | + schedule: |
| 13 | + - cron: '32 23 * * 6' |
| 14 | + |
| 15 | +jobs: |
| 16 | + analyze: |
| 17 | + name: Security Analysis on (${{ matrix.language }}) |
| 18 | + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} |
| 19 | + permissions: |
| 20 | + security-events: write |
| 21 | + packages: read |
| 22 | + actions: read |
| 23 | + contents: read |
| 24 | + strategy: |
| 25 | + fail-fast: false |
| 26 | + matrix: |
| 27 | + language: ['python'] |
| 28 | + steps: |
| 29 | + - name: Checkout repository |
| 30 | + uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Set up Python |
| 33 | + uses: actions/setup-python@v6 |
| 34 | + with: |
| 35 | + python-version: '3.11' |
| 36 | + |
| 37 | + - name: Initialize CodeQL |
| 38 | + uses: github/codeql-action/init@v3 |
| 39 | + with: |
| 40 | + languages: ${{ matrix.language }} |
| 41 | + build-mode: ${{ matrix.build-mode }} |
| 42 | + |
| 43 | + - if: matrix.build-mode == 'manual' |
| 44 | + shell: bash |
| 45 | + run: | |
| 46 | + echo 'If you are using a "manual" build mode for one or more of the' \ |
| 47 | + 'languages you are analyzing, replace this with the commands to build' \ |
| 48 | + 'your code, for example:' |
| 49 | + echo ' make bootstrap' |
| 50 | + echo ' make release' |
| 51 | + exit 1 |
| 52 | +
|
| 53 | + - name: Perform CodeQL Analysis |
| 54 | + uses: github/codeql-action/analyze@v3 |
| 55 | + with: |
| 56 | + category: "/language:${{matrix.language}}" |
| 57 | + |
| 58 | + - name: Install Bandit |
| 59 | + run: pip install bandit |
| 60 | + |
| 61 | + - name: Run Bandit scan on backend-api |
| 62 | + run: bandit -r backend-api -f txt |
| 63 | + |
| 64 | + run-lint: |
| 65 | + name: Linting Code |
| 66 | + runs-on: ubuntu-latest |
| 67 | + steps: |
| 68 | + - name: Checkout code |
| 69 | + uses: actions/checkout@v4 |
| 70 | + with: |
| 71 | + fetch-depth: 0 |
| 72 | + |
| 73 | + - name: Lint Code Base |
| 74 | + uses: github/super-linter@v4 |
| 75 | + env: |
| 76 | + VALIDATE_ALL_CODEBASE: false |
| 77 | + DEFAULT_BRANCH: "main" |
| 78 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + VALIDATE_YAML: false |
| 80 | + VALIDATE_GITHUB_ACTIONS: false |
| 81 | + VALIDATE_PYTHON_BLACK: false |
| 82 | + VALIDATE_PYTHON_FLAKE8: false |
| 83 | + VALIDATE_PYTHON_ISORT: false |
| 84 | + VALIDATE_JAVASCRIPT_STANDARD: false |
| 85 | + VALIDATE_HTML: false |
| 86 | + VALIDATE_MARKDOWN: false |
| 87 | + VALIDATE_MARKDOWN_PRETTIER: false |
| 88 | + VALIDATE_NATURAL_LANGUAGE: false |
| 89 | + |
| 90 | + report: |
| 91 | + name: Report PR status |
| 92 | + needs: [analyze, run-lint] |
| 93 | + if: always() && github.event_name == 'pull_request' |
| 94 | + runs-on: ubuntu-latest |
| 95 | + permissions: |
| 96 | + pull-requests: write |
| 97 | + steps: |
| 98 | + - uses: actions/github-script@v7 |
| 99 | + with: |
| 100 | + script: | |
| 101 | + const analyze = '${{ needs.analyze.result }}'; |
| 102 | + const lint = '${{ needs.run-lint.result }}'; |
| 103 | +
|
| 104 | + const icon = r => ({ success: '✅', failure: '❌', cancelled: '🚫', skipped: '⏭️' }[r] ?? '❓'); |
| 105 | + const allPassed = [analyze, lint].every(r => ['success', 'skipped'].includes(r)); |
| 106 | + const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 107 | + const marker = '<!-- ci-report-backend-api -->'; // used to find and update the existing comment |
| 108 | +
|
| 109 | + const body = [ |
| 110 | + marker, |
| 111 | + `### CI: Backend API`, |
| 112 | + ``, |
| 113 | + `| Job | Result |`, |
| 114 | + `|---|---|`, |
| 115 | + `| Security analysis (CodeQL + Bandit) | ${icon(analyze)} \`${analyze}\` |`, |
| 116 | + `| Lint | ${icon(lint)} \`${lint}\` |`, |
| 117 | + ``, |
| 118 | + allPassed |
| 119 | + ? `All checks passed.` |
| 120 | + : `One or more checks failed. [View logs](${runUrl})`, |
| 121 | + ].join('\n'); |
| 122 | +
|
| 123 | + const { data: comments } = await github.rest.issues.listComments({ |
| 124 | + owner: context.repo.owner, |
| 125 | + repo: context.repo.repo, |
| 126 | + issue_number: context.issue.number, |
| 127 | + }); |
| 128 | +
|
| 129 | + const existing = comments.find(c => c.body.includes(marker)); |
| 130 | +
|
| 131 | + if (existing) { |
| 132 | + await github.rest.issues.updateComment({ |
| 133 | + owner: context.repo.owner, |
| 134 | + repo: context.repo.repo, |
| 135 | + comment_id: existing.id, |
| 136 | + body, |
| 137 | + }); |
| 138 | + } else { |
| 139 | + await github.rest.issues.createComment({ |
| 140 | + owner: context.repo.owner, |
| 141 | + repo: context.repo.repo, |
| 142 | + issue_number: context.issue.number, |
| 143 | + body, |
| 144 | + }); |
| 145 | + } |
0 commit comments