Skip to content

Commit

Permalink
cancel running workflows from the same branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ivvist committed Apr 21, 2024
1 parent 24ed383 commit 4fdcef1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions scripts/cancelworkflow.sh
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

0 comments on commit 4fdcef1

Please sign in to comment.