Skip to content

Commit 2bdd3f2

Browse files
fix: only cancel workflows sharing the same head repo (#14)
1 parent 213fe75 commit 2bdd3f2

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

entrypoint.sh

+34-26
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
#!/usr/bin/env bash
2-
set -x
2+
set -x # enable debug
3+
set -e # fail fast
34
# simple script to cancel a github actions workflow given its name
45

5-
if [ -z "$GITHUB_WORKFLOW" ]
6-
then
7-
echo "Must specify GITHUB_WORKFLOW"
8-
exit 1
9-
fi
10-
116
if [ -z "$GITHUB_TOKEN" ]
127
then
138
echo "Must specify GITHUB_TOKEN"
@@ -20,35 +15,48 @@ then
2015
exit 1
2116
fi
2217

23-
if [ -z "$GITHUB_HEAD_REF" ]
18+
if [ -z "$GITHUB_RUN_ID" ]
2419
then
25-
# we have a push event
26-
BRANCH=${GITHUB_REF:11}
27-
else
28-
BRANCH=${GITHUB_HEAD_REF}
20+
echo "Must specify GITHUB_RUN_ID"
21+
exit 1
2922
fi
3023

31-
# jq queries
32-
jq_run_ids=".workflow_runs | .[] | select(.head_branch==\"${BRANCH}\" and (.status==\"in_progress\" or .status==\"queued\" or .status==\"waiting\")) | .id"
33-
34-
# get the github workflow ID
35-
3624
GITHUB_API=https://api.github.com
3725

3826
auth_header="Authorization: token ${GITHUB_TOKEN}"
27+
accept_header="Accept: application/vnd.github.v3+json"
28+
29+
function extractMetaInformation() {
30+
jq "{ workflow_id: .workflow_id, branch: .head_branch, repo: .head_repository.full_name}"
31+
}
32+
33+
function convertToKeyValuePairs() {
34+
jq -r "to_entries | map(\"\(.key)=\(.value | tostring)\") | .[]"
35+
}
36+
37+
function getRunningWorkflowIds() {
38+
jq ".workflow_runs | .[] | select(.head_branch==\"${branch}\" and .head_repository.full_name==\"${repo}\" and .status==\"in_progress\" or .status==\"queued\" or .status== \"waiting\") | .id " | grep -v "${GITHUB_RUN_ID}"
39+
}
40+
41+
function exportAll() {
42+
for var in $1; do
43+
export $var
44+
done
45+
}
3946

40-
workflow_url=$(curl -s ${GITHUB_API}/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID} -H "${auth_header}" | jq -r '.workflow_url' | cut -d/ -f2-)
41-
workflow_id=${workflow_url##/*/}
42-
echo "workflow id: "$workflow_id
47+
# extract meta information for current workflow run
48+
exportAll "$(curl -s "${GITHUB_API}/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" -H "${auth_header}" -H "${accept_header}" | extractMetaInformation | convertToKeyValuePairs)"
49+
echo "workflow id: $workflow_id"
50+
echo "branch: $branch"
51+
echo "repo: $repo"
4352

44-
# get the run ids
45-
run_ids=$(curl -s ${GITHUB_API}/repos/${GITHUB_REPOSITORY}/actions/workflows/${workflow_id}/runs -H "${auth_header}" | jq -r "${jq_run_ids}" | sort -n | head -n-1)
53+
# get the run ids for runs on same branch/repo
54+
run_ids=$(curl -s ${GITHUB_API}/repos/${GITHUB_REPOSITORY}/actions/workflows/${workflow_id}/runs -H "${auth_header}" -H "${accept_header}" | getRunningWorkflowIds)
4655

47-
echo "run ids: "$run_ids
56+
echo "run ids: ${run_ids}"
4857

4958
# cancel the previous runs
50-
for run_id in $run_ids
51-
do
52-
curl -s -X POST -H "${auth_header}" ${GITHUB_API}/repos/${GITHUB_REPOSITORY}/actions/runs/${run_id}/cancel
59+
for run_id in $run_ids; do
60+
curl -s -X POST -H "${auth_header}" -H "${accept_header}" ${GITHUB_API}/repos/${GITHUB_REPOSITORY}/actions/runs/${run_id}/cancel
5361
echo "Cancelled run $run_id"
5462
done

0 commit comments

Comments
 (0)