|
| 1 | +# This workflow can be used to trigger a rebuild of checks for a certain PR. |
| 2 | +# |
| 3 | +# Automatic triggering is based on PR labeled event. |
| 4 | +# The workflow will check if the new label is "rebuild". |
| 5 | +# It triggers up to 3 check runs taken from the PR and triggers all their jobs again, independent |
| 6 | +# of the previous status. (3 is an arbitrary number, it was just necessary to have more than one in |
| 7 | +# case there are different relevant workflows, e.g. build and codeql. This could be limited to |
| 8 | +# checks marked as "required" or "failed", but it seems better to trigger all). |
| 9 | +# The label "rebuild" is removed after the rebuild is triggered. |
| 10 | +# |
| 11 | +# It can be triggered manually, referencing a PR number (this is mainly for testing purposes). |
| 12 | +name: Rebuild PR |
| 13 | + |
| 14 | +on: |
| 15 | + pull_request_target: |
| 16 | + types: [labeled] |
| 17 | + workflow_dispatch: |
| 18 | + inputs: |
| 19 | + pr: |
| 20 | + description: 'PR number to rebuild' |
| 21 | + required: false |
| 22 | + default: '0' |
| 23 | + label: |
| 24 | + description: 'Label to trigger rebuild' |
| 25 | + required: false |
| 26 | + default: 'rebuild' |
| 27 | + |
| 28 | +# grant permission |
| 29 | +# - actions:write to allow rerun |
| 30 | +# - PR:write to allow removing the label |
| 31 | +# - checks:write to allow setting build status |
| 32 | +permissions: |
| 33 | + actions: write |
| 34 | + checks: write |
| 35 | + pull-requests: write |
| 36 | +env: |
| 37 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + PR_NUMBER: ${{ github.event.number || inputs.pr }} |
| 39 | + LABEL: ${{ github.event.label.name || inputs.label }} |
| 40 | + BASE: ${{ github.event.pull_request.base.repo.full_name || github.event.repository.full_name }} |
| 41 | + |
| 42 | +jobs: |
| 43 | + rebuild: |
| 44 | + runs-on: ubuntu-latest |
| 45 | + steps: |
| 46 | + - name: Info |
| 47 | + env: |
| 48 | + GITHUB: ${{ toJson(github) }} |
| 49 | + run: | |
| 50 | + echo "$GITHUB" |
| 51 | +
|
| 52 | + - name: Checkout |
| 53 | + if: ${{ github.head_ref == '' && env.LABEL == 'rebuild' }} |
| 54 | + uses: actions/checkout@v4 |
| 55 | + with: |
| 56 | + persist-credentials: false |
| 57 | + |
| 58 | + - name: Checkout merge |
| 59 | + if: ${{ github.head_ref != '' && env.LABEL == 'rebuild' }} |
| 60 | + uses: actions/checkout@v4 |
| 61 | + with: |
| 62 | + ref: refs/pull/${{github.event.pull_request.number}}/merge |
| 63 | + persist-credentials: false |
| 64 | + |
| 65 | + - name: List Jobs |
| 66 | + if: ${{ env.LABEL == 'rebuild' }} |
| 67 | + run: | |
| 68 | + echo "Label: ${{ env.LABEL }}" |
| 69 | + echo "PR: ${{ env.PR_NUMBER }}" |
| 70 | + NUMBER=$(gh pr checks ${{ env.PR_NUMBER }} -R ${{ env.BASE }}|grep -v "^rebuild"| sed -E 's#.*/([0-9]+)/job/([0-9]+)#\1#'|grep -ve '[[:alpha:]]'|sort -nu|head -n3|xargs|tr '\n' ' ') |
| 71 | + echo "Number of last 3 check jobs: $NUMBER" |
| 72 | + echo "NUMBER=$NUMBER">>$GITHUB_ENV |
| 73 | +
|
| 74 | + - name: Trigger Rebuild |
| 75 | + if: ${{ env.LABEL == 'rebuild' }} |
| 76 | + env: |
| 77 | + PR: ${{ env.PR_NUMBER }} |
| 78 | + NUMBER: ${{ env.NUMBER }} |
| 79 | + run: | |
| 80 | + if [ -z "$NUMBER" ]; then |
| 81 | + echo "Error: Previous check run not found, cannot rebuild" |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + for i in $(echo $NUMBER|tr ' ' '\n'); do |
| 85 | + echo "Rebuilding PR #$i" |
| 86 | + gh run rerun $i || true |
| 87 | + done |
| 88 | +
|
| 89 | + - name: Remove Label |
| 90 | + if: ${{ always() && env.LABEL == 'rebuild' }} |
| 91 | + env: |
| 92 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 93 | + PR: ${{ env.PR_NUMBER }} |
| 94 | + run: | |
| 95 | + gh pr edit $PR --remove-label rebuild |
0 commit comments