Skip to content

Commit 40d9d66

Browse files
authored
Add support for periodically (monthly) rebuilding a PR (#858)
* Add a workflow to rerun jobs labeled "periodic rebuild" once a week * Change to monthly
1 parent 8658484 commit 40d9d66

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
# yamllint disable rule:line-length
3+
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
4+
name: Periodic PR Rebuilds
5+
6+
on: # yamllint disable-line rule:truthy
7+
schedule:
8+
- cron: '14 3 1 * *' # Monthly, 1st of the month, 03:14 UTC
9+
workflow_dispatch:
10+
11+
jobs:
12+
find-and-rerun-prs:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Set up jq
17+
run: sudo apt-get update && sudo apt-get install -y jq
18+
19+
- name: Set up GitHub CLI
20+
uses: cli/cli-action@v2
21+
22+
- name: Print repository context
23+
run: |
24+
echo "Repository: ${{ github.repository }}"
25+
echo "Event: ${{ github.event_name }}"
26+
27+
- name: List open PRs with label 'periodic rebuild'
28+
id: list_prs
29+
run: |
30+
echo "Listing PRs with label 'periodic rebuild'..."
31+
gh pr list --state open --label "periodic rebuild" --json number,title,headRefName | tee prs.json
32+
cat prs.json | jq -r '.[] | "PR #" + (.number|tostring) + ": " + .title + " (branch: " + .headRefName + ")"'
33+
cat prs.json | jq '.[].number' > prs.txt
34+
echo "Total PRs found:" $(wc -l < prs.txt)
35+
env:
36+
GH_TOKEN: ${{ secrets.REBUILD_PAT }}
37+
38+
- name: Rerun last build workflow for each PR
39+
run: |
40+
set -e
41+
if [ ! -s prs.txt ]; then
42+
echo "No PRs to process."
43+
exit 0
44+
fi
45+
46+
while read pr_number; do
47+
echo "----------------------"
48+
echo "Processing PR #$pr_number"
49+
50+
pr_branch=$(jq -r '.[] | select(.number=='"$pr_number"') | .headRefName' prs.json)
51+
echo "PR branch: $pr_branch"
52+
53+
echo "Finding latest workflow run for 'Builds' on branch '$pr_branch'..."
54+
55+
workflow_run=$(gh api repos/${{ github.repository }}/actions/runs \
56+
--paginate \
57+
-F "event=pull_request" \
58+
-F "branch=$pr_branch" \
59+
--jq '.workflow_runs[] | select(.name=="Builds")' | jq -s 'sort_by(.created_at) | reverse | .[0]')
60+
61+
if [ "$workflow_run" = "null" ] || [ -z "$workflow_run" ]; then
62+
echo "No 'Builds' workflow run found for PR #$pr_number (branch: $pr_branch)"
63+
continue
64+
fi
65+
66+
run_id=$(echo "$workflow_run" | jq -r '.id')
67+
run_status=$(echo "$workflow_run" | jq -r '.status')
68+
run_conclusion=$(echo "$workflow_run" | jq -r '.conclusion')
69+
run_created_at=$(echo "$workflow_run" | jq -r '.created_at')
70+
echo "Found workflow run:"
71+
echo " ID: $run_id"
72+
echo " Status: $run_status"
73+
echo " Conclusion: $run_conclusion"
74+
echo " Created At: $run_created_at"
75+
76+
echo "Rerunning workflow run ID $run_id..."
77+
gh api -X POST repos/${{ github.repository }}/actions/runs/$run_id/rerun
78+
echo "Rerun triggered for run ID $run_id."
79+
done < prs.txt
80+
env:
81+
GH_TOKEN: ${{ secrets.REBUILD_PAT }}
82+
83+
- name: Clean up
84+
run: rm -f prs.json prs.txt

0 commit comments

Comments
 (0)