-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cancel running workflows from the same branch
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/bash | ||
|
||
if [ "$#" -ne 3 ]; then | ||
echo "Cancel workflow script was not running. Arguments: $#" | ||
exit 0 | ||
fi | ||
|
||
repo=$1 | ||
br=$2 | ||
wf=$3 | ||
|
||
# Declare an array to hold the workflow details | ||
declare -a workflows=() | ||
|
||
# Counter to keep track of the number of lines read | ||
i=0 | ||
|
||
# Temporary array to hold the set of three lines (workflowName, databaseId, headBranch) | ||
temp=() | ||
|
||
# Execute your gh command and read the output line by line | ||
while IFS= read -r line; do | ||
# Add the line to the temporary array | ||
temp[i]="$line" | ||
|
||
# Increment the counter | ||
((i++)) | ||
|
||
# Every three lines, join them into a single array element and reset | ||
if [[ $i -eq 1 ]]; then | ||
# Concatenate the elements of temp array into a string, separated by a delimiter (e.g., "|") | ||
workflows+=("${temp[0]}") | ||
|
||
# Reset the counter and temporary array | ||
i=0 | ||
temp=() | ||
fi | ||
done < <(gh run list -b "${br}" --workflow "${wf}" --limit 3 -e pull_request_target -s in_progress --json 'databaseId' --jq '.[] | .databaseId' -R "${repo}") | ||
|
||
# Output the results | ||
for workflow in "${workflows[@]}"; do | ||
echo "Running workflow: $workflow. Trying to cancel..." | ||
gh run cancel $workflow || true | ||
done |