Generate Benchmark Report #208
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Generate Benchmark Report | |
| on: | |
| # Trigger on push when cost.json is changed (all branches) | |
| push: | |
| paths: | |
| - 'benchmark/cost.json' | |
| # Trigger on pull request when cost.json is changed | |
| pull_request: | |
| paths: | |
| - 'benchmark/cost.json' | |
| # Daily schedule at UTC 16:00 (Beijing Time 00:00) - only runs on default branch (main) | |
| schedule: | |
| - cron: '0 16 * * *' | |
| # Allow manual trigger | |
| workflow_dispatch: | |
| permissions: | |
| contents: write # Allow committing report to main branch | |
| pull-requests: write # Allow commenting on PRs | |
| jobs: | |
| generate-report: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Generate benchmark report | |
| run: npm run benchmark:calculate | |
| - name: Check for changes | |
| id: git-check | |
| run: | | |
| git diff --exit-code benchmark/report.json benchmark/report.md || echo "changed=true" >> $GITHUB_OUTPUT | |
| - name: Commit and push if changed | |
| if: steps.git-check.outputs.changed == 'true' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') | |
| run: | | |
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
| git config --local user.name "github-actions[bot]" | |
| git add benchmark/report.json benchmark/report.md | |
| git commit -m "chore: update benchmark cost report [skip ci]" | |
| git push | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Upload reports as artifacts | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: benchmark-reports | |
| path: | | |
| benchmark/report.json | |
| benchmark/report.md | |
| retention-days: 30 | |
| - name: Comment on PR (if applicable) | |
| # Security: Only comment on PRs from the same repository (not forks) | |
| if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const reportPath = 'benchmark/report.md'; | |
| if (fs.existsSync(reportPath)) { | |
| const report = fs.readFileSync(reportPath, 'utf8'); | |
| const body = `## 📊 Benchmark Cost Report\n\n${report}`; | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: body | |
| }); | |
| } | |