Skip to content

Commit

Permalink
feat: add multiple workflow support (via regex) to the cancel-redunda…
Browse files Browse the repository at this point in the history
…nt-workflows command (#29)
  • Loading branch information
ankorstore-haddowg authored Mar 30, 2023
1 parent fda098a commit 93e7898
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/commands/cancel-redundant-workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ description: >
This command can be used with an appropriate `when` step to do this.
This command requires a CIRCLE_TOKEN env var to be set with a valid CircleCi API Token.
parameters:
workflow-name:
type: string
default: ""
description: >-
The name of teh workflow you wish to cancel.
Will default to the current workflow name.
You can use regex if you start your name with ^.
Using regex you can target multiple workflow names this can be helpful if you are using setup workflows and wish to cancel any pipelines still setting up as well as if they have started the continuation workflows(s).
steps:
- run:
name: Cancelling redundant workflows
environment:
TARGET_WORKFLOW_NAME: << parameters.workflow-name >>
command: << include(scripts/cancel-redundant-workflows.sh) >>
1 change: 1 addition & 0 deletions src/commands/fail-fast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parameters:
why:
type: string
default: "Cancelling workflow as this job has failed."
description: The reason why the workflow is being failed to show in the output
steps:
- run:
name: Failing Fast
Expand Down
11 changes: 8 additions & 3 deletions src/scripts/cancel-redundant-workflows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ fi
# Get the name of the workflow and the related pipeline number
CURRENT_WORKFLOW_URL="https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}?circle-token=$CIRCLE_TOKEN"
curl -f -s --retry 3 --retry-all-errors "$CURRENT_WORKFLOW_URL" > /tmp/aks/current_wf.json
WORKFLOW_NAME="$(jq -r '.name' /tmp/aks/current_wf.json)"
CURRENT_WORKFLOW_NAME="$(jq -r '.name' /tmp/aks/current_wf.json)"
CURRENT_PIPELINE_NUM="$(jq -r '.pipeline_number' /tmp/aks/current_wf.json)"

WORKFLOW_NAME=${TARGET_WORKFLOW_NAME:-$CURRENT_WORKFLOW_NAME}
if [[ $WORKFLOW_NAME != ^* ]]; then
WORKFLOW_NAME="^$WORKFLOW_NAME\$"
fi

# Get the IDs of pipelines created for the same branch. (Only consider pipelines that have a pipeline number smaller than the current pipeline)
PIPELINES_URL="https://circleci.com/api/v2/project/gh/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pipeline?circle-token=$CIRCLE_TOKEN&branch=$CIRCLE_BRANCH"
curl -f -s --retry 3 --retry-all-errors "$PIPELINES_URL" >> /tmp/aks/pipelines.json
Expand All @@ -25,13 +30,13 @@ PIPELINE_IDS=$(jq -r --argjson CURRENT_PIPELINE_NUM "$CURRENT_PIPELINE_NUM" '.it
if [ -n "$PIPELINE_IDS" ]; then
for PIPELINE_ID in $PIPELINE_IDS
do
curl -f -s --retry 3 --retry-all-errors "https://circleci.com/api/v2/pipeline/${PIPELINE_ID}/workflow?circle-token=$CIRCLE_TOKEN" | jq -r --arg workflow_name "${WORKFLOW_NAME}" '.items[] | select(.status == "on_hold" or .status == "running") | select(.name == $workflow_name) | .id | values' >> /tmp/aks/wf_to_cancel
curl -f -s --retry 3 --retry-all-errors "https://circleci.com/api/v2/pipeline/${PIPELINE_ID}/workflow?circle-token=$CIRCLE_TOKEN" | jq -r --arg workflow_name "${WORKFLOW_NAME}" '.items[] | select(.status == "on_hold" or .status == "running") | select(.name | test($workflow_name)) | .id | values' >> /tmp/aks/wf_to_cancel
done
fi

## Cancel any currently running/on_hold workflow with the same name
if [ -s /tmp/aks/wf_to_cancel ]; then
echo "Cancelling the following redundant workflow(s):"
echo "Cancelling the following redundant \`$WORKFLOW_NAME\` workflow(s):"
cat /tmp/aks/wf_to_cancel
while read -r WORKFLOW_ID;
do
Expand Down

0 comments on commit 93e7898

Please sign in to comment.