Skip to content

Commit a4821c8

Browse files
authored
tools: add option for benchmark.yml to post comment on PR
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #64395 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 892976d commit a4821c8

1 file changed

Lines changed: 64 additions & 2 deletions

File tree

.github/workflows/benchmark.yml

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,43 @@ on:
2525
type: number
2626
default: 30
2727
description: How many times to repeat each benchmark
28+
post-comment:
29+
type: boolean
30+
description: Post a comment linking to the run, with the aggregated result once known.
31+
token:
32+
type: string
33+
description: A GitHub token to post comments cross repository (not recommended)
2834

2935
permissions:
3036
contents: read
3137

3238
jobs:
39+
post-comment:
40+
if: inputs.post-comment
41+
outputs:
42+
body: ${{ steps.comment.outputs.COMMENT_BODY }}
43+
url: ${{ steps.comment.outputs.COMMENT_URL }}
44+
runs-on: ubuntu-slim
45+
permissions:
46+
pull-requests: write
47+
steps:
48+
- name: Mark token input as sensitive
49+
if: inputs.token != ''
50+
run: echo "::add-mask::${{ inputs.token }}"
51+
- name: Add link to the current run
52+
id: comment
53+
run: |
54+
FILTER_IF_SET=
55+
[ -z "$FILTER" ] || FILTER_IF_SET=" / $FILTER"
56+
COMMENT_BODY="Benchmark GHA (${CATEGORIES}${FILTER_IF_SET}): ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
57+
echo "COMMENT_BODY=$COMMENT_BODY" >> "$GITHUB_OUTPUT"
58+
echo "COMMENT_URL=$(gh pr comment -R "${REPO:-$GITHUB_REPOSITORY}" "$PR_ID" --body "$COMMENT_BODY")" >> "$GITHUB_OUTPUT"
59+
env:
60+
CATEGORIES: ${{ inputs.category }}
61+
FILTER: ${{ inputs.filter }}
62+
GH_TOKEN: ${{ inputs.token || github.token }}
63+
REPO: ${{ inputs.repo }}
64+
PR_ID: ${{ inputs.pr_id }}
3365
build:
3466
strategy:
3567
fail-fast: true
@@ -155,9 +187,13 @@ jobs:
155187
path: ${{ matrix.system }}.csv
156188

157189
aggregate-results:
158-
needs: build
190+
needs: [build, post-comment]
191+
if: ${{ always() && needs.build.result == 'success' && needs.post-comment.result == (inputs.post-comment && 'success' || 'skipped') }}
159192
name: Aggregate benchmark results
160193
runs-on: ubuntu-slim
194+
permissions:
195+
contents: read
196+
pull-requests: write
161197
steps:
162198
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
163199
with:
@@ -200,4 +236,30 @@ jobs:
200236
echo "> [!WARNING] "
201237
echo "> Do not take GHA benchmark results as face value, always confirm them"
202238
echo "> using a dedicated machine, e.g. Jenkins CI."
203-
' | tee /dev/stderr >> "$GITHUB_STEP_SUMMARY"
239+
' | tee /dev/stderr ${{ inputs.post-comment && 'body.txt' || '' }} >> "$GITHUB_STEP_SUMMARY"
240+
- name: Mark token input as sensitive
241+
if: inputs.token != ''
242+
run: echo "::add-mask::${{ inputs.token }}"
243+
- name: Edit comment
244+
if: inputs.post-comment
245+
run: |
246+
{
247+
echo "$COMMENT_BODY"
248+
echo
249+
echo '<details><summary>Results</summary>'
250+
echo
251+
cat body.txt
252+
echo
253+
echo '</details>'
254+
} | jq -Rrcs '{ body: . }' | gh api \
255+
--method PATCH \
256+
-H "Accept: application/vnd.github+json" \
257+
-H "X-GitHub-Api-Version: 2026-03-10" \
258+
"/repos/${REPO:-$GITHUB_REPOSITORY}/issues/comments/${COMMENT_URL##*-}" \
259+
--input -
260+
env:
261+
GH_TOKEN: ${{ inputs.token || github.token }}
262+
REPO: ${{ inputs.repo }}
263+
COMMENT_BODY: ${{ needs.post-comment.outputs.body }}
264+
COMMENT_URL: ${{ needs.post-comment.outputs.url }}
265+
PR_ID: ${{ inputs.pr_id }}

0 commit comments

Comments
 (0)