From 93e78989cc16055f275285c81b57ddfe61fd10e8 Mon Sep 17 00:00:00 2001 From: Gregory Haddow <93638800+ankorstore-haddowg@users.noreply.github.com> Date: Thu, 30 Mar 2023 13:03:00 +0100 Subject: [PATCH] feat: add multiple workflow support (via regex) to the cancel-redundant-workflows command (#29) --- src/commands/cancel-redundant-workflows.yml | 11 +++++++++++ src/commands/fail-fast.yml | 1 + src/scripts/cancel-redundant-workflows.sh | 11 ++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/commands/cancel-redundant-workflows.yml b/src/commands/cancel-redundant-workflows.yml index 27c0dad..97e7c18 100644 --- a/src/commands/cancel-redundant-workflows.yml +++ b/src/commands/cancel-redundant-workflows.yml @@ -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) >> diff --git a/src/commands/fail-fast.yml b/src/commands/fail-fast.yml index 5b719ff..3bf0446 100644 --- a/src/commands/fail-fast.yml +++ b/src/commands/fail-fast.yml @@ -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 diff --git a/src/scripts/cancel-redundant-workflows.sh b/src/scripts/cancel-redundant-workflows.sh index e864709..aae4bec 100755 --- a/src/scripts/cancel-redundant-workflows.sh +++ b/src/scripts/cancel-redundant-workflows.sh @@ -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 @@ -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