Skip to content

Commit 537682c

Browse files
committed
jenkins: concurency
1 parent b3bb0ff commit 537682c

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

.github/workflows/build-pull-request-jenkins.yml

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ jobs:
2323
JENKINS_JOB: ${{ vars.JENKINS_JOB || 'star-sw-ci-pipeline' }}
2424
JENKINS_USER: ${{ secrets.JENKINS_USER }}
2525
JENKINS_TOKEN: ${{ secrets.JENKINS_TOKEN }}
26+
PR_NUMBER: ${{ github.event.pull_request.number }}
2627
BRANCH_NAME: ${{ github.event.pull_request.head.ref }}
2728
GIT_COMMIT: ${{ github.event.pull_request.head.sha }}
2829
REPO_URL: ${{ github.event.pull_request.head.repo.clone_url }}
2930
steps:
3031
- name: Validate Jenkins configuration
3132
run: |
3233
set -euo pipefail
33-
for var in JENKINS_URL JENKINS_JOB JENKINS_USER JENKINS_TOKEN BRANCH_NAME GIT_COMMIT REPO_URL; do
34+
for var in JENKINS_URL JENKINS_JOB JENKINS_USER JENKINS_TOKEN PR_NUMBER BRANCH_NAME GIT_COMMIT REPO_URL; do
3435
if [ -z "${!var:-}" ]; then
3536
echo "::error::Missing required value for ${var}"
3637
exit 1
@@ -47,6 +48,76 @@ jobs:
4748
echo "::add-mask::$crumb"
4849
echo "value=$crumb" >> "$GITHUB_OUTPUT"
4950
51+
- name: Cancel older Jenkins builds for this PR
52+
env:
53+
JENKINS_CRUMB: ${{ steps.crumb.outputs.value }}
54+
run: |
55+
set -euo pipefail
56+
curl_auth="${JENKINS_USER}:${JENKINS_TOKEN}"
57+
58+
build_api="${JENKINS_URL}/job/${JENKINS_JOB}/api/json?tree=builds[number,url,building,actions[parameters[name,value]]]"
59+
queue_api="${JENKINS_URL}/queue/api/json?tree=items[id,task[name],actions[parameters[name,value]]]"
60+
61+
running_builds="$(
62+
curl --globoff --silent --show-error --fail --retry 3 --retry-all-errors --user "$curl_auth" "$build_api" |
63+
jq -r --arg pr "$PR_NUMBER" --arg branch "$BRANCH_NAME" --arg repo "$REPO_URL" '
64+
def param($name): ([.actions[]?.parameters[]? | select(.name == $name) | (.value | tostring)][0] // "");
65+
.builds[]?
66+
| select(.building == true)
67+
| select(
68+
(param("PR_NUMBER") != "" and param("PR_NUMBER") == $pr)
69+
or
70+
(param("PR_NUMBER") == "" and param("BRANCH_NAME") == $branch and param("REPO_URL") == $repo)
71+
)
72+
| .url
73+
'
74+
)"
75+
76+
queued_items="$(
77+
curl --globoff --silent --show-error --fail --retry 3 --retry-all-errors --user "$curl_auth" "$queue_api" |
78+
jq -r --arg job "$JENKINS_JOB" --arg pr "$PR_NUMBER" --arg branch "$BRANCH_NAME" --arg repo "$REPO_URL" '
79+
def param($name): ([.actions[]?.parameters[]? | select(.name == $name) | (.value | tostring)][0] // "");
80+
.items[]?
81+
| select(.task.name == $job)
82+
| select(
83+
(param("PR_NUMBER") != "" and param("PR_NUMBER") == $pr)
84+
or
85+
(param("PR_NUMBER") == "" and param("BRANCH_NAME") == $branch and param("REPO_URL") == $repo)
86+
)
87+
| .id
88+
'
89+
)"
90+
91+
if [ -n "$queued_items" ]; then
92+
while IFS= read -r item_id; do
93+
[ -n "$item_id" ] || continue
94+
echo "Canceling queued Jenkins item ${item_id}"
95+
curl --silent --show-error --fail \
96+
--request POST \
97+
--retry 3 \
98+
--retry-all-errors \
99+
--user "$curl_auth" \
100+
--header "$JENKINS_CRUMB" \
101+
--output /dev/null \
102+
"${JENKINS_URL}/queue/cancelItem?id=${item_id}"
103+
done <<< "$queued_items"
104+
fi
105+
106+
if [ -n "$running_builds" ]; then
107+
while IFS= read -r build_url; do
108+
[ -n "$build_url" ] || continue
109+
echo "Stopping running Jenkins build ${build_url}"
110+
curl --silent --show-error --fail \
111+
--request POST \
112+
--retry 3 \
113+
--retry-all-errors \
114+
--user "$curl_auth" \
115+
--header "$JENKINS_CRUMB" \
116+
--output /dev/null \
117+
"${build_url}stop"
118+
done <<< "$running_builds"
119+
fi
120+
50121
- name: Trigger Jenkins docker job
51122
env:
52123
JENKINS_CRUMB: ${{ steps.crumb.outputs.value }}
@@ -61,6 +132,7 @@ jobs:
61132
--retry-all-errors \
62133
--user "$curl_auth" \
63134
--header "$JENKINS_CRUMB" \
135+
--data-urlencode "PR_NUMBER=${PR_NUMBER}" \
64136
--data-urlencode "BRANCH_NAME=${BRANCH_NAME}" \
65137
--data-urlencode "GIT_COMMIT=${GIT_COMMIT}" \
66138
--data-urlencode "REPO_URL=${REPO_URL}" \
@@ -77,6 +149,7 @@ jobs:
77149
echo "### Jenkins Triggered"
78150
echo
79151
echo "- Job: \`${JENKINS_JOB}\`"
152+
echo "- Pull request: \`${PR_NUMBER}\`"
80153
echo "- Branch: \`${BRANCH_NAME}\`"
81154
echo "- Commit: \`${GIT_COMMIT}\`"
82155
echo "- Queue item: ${queue_url}"

Jenkinsfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pipeline {
88
}
99

1010
parameters {
11+
string(name: 'PR_NUMBER', defaultValue: '', description: 'Pull request number, optional')
1112
string(name: 'BRANCH_NAME', defaultValue: 'main', description: 'Branch to build')
1213
string(name: 'GIT_COMMIT', defaultValue: '', description: 'Commit SHA to build, optional')
1314
string(name: 'REPO_URL', defaultValue: 'https://github.com/star-bnl/star-sw.git', description: 'Repository URL')

0 commit comments

Comments
 (0)