Skip to content

Commit

Permalink
Add job for commenting benchmark results on the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
gruuya committed Mar 7, 2024
1 parent f51a754 commit be9b644
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 6 deletions.
69 changes: 63 additions & 6 deletions .github/workflows/pr_benchmarks.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: Run and Cache Benchmarks
name: Benchmarks

on:
pull_request:
types: [labeled, opened, reopened, synchronize]
pull_request_target:

jobs:
benchmark:
name: Run Benchmarks
runs-on: ubuntu-latest
permissions: read-all
steps:
- name: Dump GitHub context
env:
Expand All @@ -16,6 +16,8 @@ jobs:

- name: Checkout PR changes
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}

- name: Setup data and generate unique result names
run: |
Expand Down Expand Up @@ -45,10 +47,9 @@ jobs:
- name: Checkout base commit
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}
clean: false

- name: Benchmark baseline and compare
- name: Benchmark baseline and generate comparison message
env:
RESULTS_NAME: ${{ env.BASE_REF_SHA }}
run: |
Expand All @@ -59,5 +60,61 @@ jobs:
# Temporary workaround, until `RESULTS_NAME` var lands into main
mv -f results/HEAD results/${{ env.BASE_REF_SHA }}
echo ${{ github.event.pull_request.number }} > pr
pip3 install rich
./bench.sh compare ${{ env.BASE_REF_SHA }} ${{ env.HEAD_REF_SHA }}
cat > message.md <<EOF
# Benchmark results
<details>
<summary>Benchmarks comparing ${{ github.event.pull_request.base.sha }} and ${{ github.sha }}</summary>
\`\`\`
$(./bench.sh compare ${{ env.BASE_REF_SHA }} ${{ env.HEAD_REF_SHA }})
\`\`\`
</details>
EOF
cat message.md
- name: Upload benchmark comparison message
uses: actions/upload-artifact@v4
with:
name: message
path: benchmarks/message.md

- name: Upload PR number
uses: actions/upload-artifact@v4
with:
name: pr
path: benchmarks/pr

comment:
# Separate job with default write permissions, which can
# execute another workflow to actually post a PR comment
name: Post benchmarks comment
runs-on: ubuntu-latest
permissions: write-all
needs: [ benchmark ]
steps:
- name: Download comment message
uses: actions/download-artifact@v4
with:
name: message

- name: Download pr number
uses: actions/download-artifact@v4
with:
name: pr

- name: Print message and pr number
run: |
cat pr
echo "PR_NUMBER=$(cat pr)" >> "$GITHUB_ENV"
cat message.md
- name: PR comment with file
uses: thollander/actions-comment-pull-request@v2
with:
filePath: message.md
55 changes: 55 additions & 0 deletions .github/workflows/pr_comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: PR Comment

# read-write repo token
# access to secrets
on:
workflow_run:
workflows: ["Benchmarks"]
types:
- completed

jobs:
comment:
name: PR Comment
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: echo "$GITHUB_CONTEXT"

- name: Download comment message
uses: actions/download-artifact@v4
with:
name: message
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Download pr number
uses: actions/download-artifact@v4
with:
name: pr
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Print message and pr number
run: |
cat pr
echo "PR_NUMBER=$(cat pr)" >> "$GITHUB_ENV"
cat message.md
- name: Post the comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const content = fs.readFileSync('message.md', 'utf8');
github.rest.issues.createComment({
issue_number: process.env.PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
body: content,
})

0 comments on commit be9b644

Please sign in to comment.