|
1 | 1 | #!/usr/bin/env bash
|
2 |
| -set -x |
| 2 | +set -x # enable debug |
| 3 | +set -e # fail fast |
3 | 4 | # simple script to cancel a github actions workflow given its name
|
4 | 5 |
|
5 |
| -if [ -z "$GITHUB_WORKFLOW" ] |
6 |
| -then |
7 |
| - echo "Must specify GITHUB_WORKFLOW" |
8 |
| - exit 1 |
9 |
| -fi |
10 |
| - |
11 | 6 | if [ -z "$GITHUB_TOKEN" ]
|
12 | 7 | then
|
13 | 8 | echo "Must specify GITHUB_TOKEN"
|
|
20 | 15 | exit 1
|
21 | 16 | fi
|
22 | 17 |
|
23 |
| -if [ -z "$GITHUB_HEAD_REF" ] |
| 18 | +if [ -z "$GITHUB_RUN_ID" ] |
24 | 19 | 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 |
29 | 22 | fi
|
30 | 23 |
|
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 |
| - |
36 | 24 | GITHUB_API=https://api.github.com
|
37 | 25 |
|
38 | 26 | 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 | +} |
39 | 46 |
|
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" |
43 | 52 |
|
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) |
46 | 55 |
|
47 |
| -echo "run ids: "$run_ids |
| 56 | +echo "run ids: ${run_ids}" |
48 | 57 |
|
49 | 58 | # 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 |
53 | 61 | echo "Cancelled run $run_id"
|
54 | 62 | done
|
0 commit comments