Skip to content

Commit 9fb18a2

Browse files
panvaaduh95
authored andcommitted
tools: check CI availability and workload
Select mergeable PRs and check Jenkins availability and workload before removing request labels. Leave requests for a later run when Jenkins is unavailable or the workload has reached the configured limit. Make the batch size and workload limit repository variables, defaulting to 5 and 10 respectively. Refs: nodejs/node-core-utils#1204 Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66280 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 8dee0d1 commit 9fb18a2

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎.github/workflows/auto-start-ci.yml‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# This action uses the following secrets:
22
# JENKINS_USER: GitHub user whose Jenkins token is defined below
33
# JENKINS_TOKEN: Jenkins token, to be used to start or resume CI
4+
# Optional repository variables:
5+
# AUTO_START_CI_BATCH_SIZE: maximum PRs to process per run (default: 5)
6+
# AUTO_START_CI_MAX_WORKLOAD: pause at this many running or queued PR jobs (default: 15)
47
name: Auto Start CI
58

69
on:
@@ -30,16 +33,25 @@ jobs:
3033
steps:
3134
- name: Get Pull Requests
3235
id: get_prs_for_ci
36+
# Explicit bash enables pipefail so jq cannot hide a failed gh query.
37+
shell: bash
3338
run: |
39+
if ! [[ "$BATCH_SIZE" =~ ^[1-9][0-9]*$ ]]; then
40+
echo '::error::AUTO_START_CI_BATCH_SIZE must be a strictly positive integer'
41+
exit 1
42+
fi
43+
# Filter before selecting the batch; UNKNOWN mergeability is retried later.
3444
numbers=$(gh pr list \
3545
--repo "$GITHUB_REPOSITORY" \
36-
--json 'number' \
46+
--json 'number,mergeable' \
3747
--search 'review:approved label:request-ci,resume-ci' \
38-
-t '{{ range . }}{{ .number }} {{ end }}' \
39-
--limit 5)
48+
--limit 100 \
49+
| jq --argjson limit "$BATCH_SIZE" \
50+
'[.[] | select(.mergeable == "MERGEABLE") | .number] | .[:$limit] | map(tostring) | join(" ")' -r)
4051
echo "numbers=$numbers" >> "$GITHUB_OUTPUT"
4152
env:
4253
GH_TOKEN: ${{ github.token }}
54+
BATCH_SIZE: ${{ vars.AUTO_START_CI_BATCH_SIZE || '5' }}
4355
start-ci:
4456
permissions:
4557
checks: read
@@ -78,3 +90,4 @@ jobs:
7890
env:
7991
GH_TOKEN: ${{ github.token }}
8092
PULL_REQUESTS: ${{ needs.get-prs-for-ci.outputs.numbers }}
93+
MAX_WORKLOAD: ${{ vars.AUTO_START_CI_MAX_WORKLOAD || '15' }}

‎tools/actions/start-ci.sh‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
set -xe
44

55
cqurl="${GITHUB_SERVER_URL:?}/${GITHUB_REPOSITORY:?}/actions/runs/${GITHUB_RUN_ID:?}"
6+
max_workload=${MAX_WORKLOAD:-15}
7+
case $max_workload in
8+
*[!0-9]*)
9+
echo '::error::AUTO_START_CI_MAX_WORKLOAD must be a positive integer'
10+
exit 1
11+
;;
12+
*) ;;
13+
esac
614

715
escape_code_block_or_line() {
816
case $1 in
@@ -19,6 +27,30 @@ escape_code_block_or_line() {
1927
}
2028

2129
for pr in "$@"; do
30+
# Sleep in between.
31+
[ "$pr" = "$1" ] || sleep 20
32+
33+
# Keep request labels until Jenkins is ready to accept more work.
34+
if ! ncu-ci available; then
35+
echo '::notice::CI is unavailable; leaving CI requests for a later run.'
36+
break
37+
fi
38+
if ! workload=$(ncu-ci workload); then
39+
echo '::notice::CI workload could not be checked; leaving CI requests for a later run.'
40+
break
41+
fi
42+
case $workload in
43+
''|*[!0-9]*)
44+
echo '::error::ncu-ci workload did not return a positive integer'
45+
exit 1
46+
;;
47+
*) ;;
48+
esac
49+
if ! [ "$workload" -lt "$max_workload" ]; then
50+
echo "::notice::CI workload is $workload (limit: $max_workload); leaving CI requests for a later run."
51+
break
52+
fi
53+
2254
request_labels=$(gh -R "$GITHUB_REPOSITORY" pr view "$pr" --json labels \
2355
--jq '[.labels[].name | select(. == "request-ci" or . == "resume-ci")] | sort | join(",")')
2456
case "$request_labels" in

0 commit comments

Comments
 (0)