From 96174883d6a2b9ecd3566660032e3517ef3b6154 Mon Sep 17 00:00:00 2001 From: Sam Darwin Date: Tue, 28 May 2024 14:05:01 -0600 Subject: [PATCH] CircleCI: autocancel workflows (#915) --- .circleci/autocancel.sh | 38 ++++++++++++++++++++++++++++++++++++++ .circleci/config.yml | 5 +++-- 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 .circleci/autocancel.sh diff --git a/.circleci/autocancel.sh b/.circleci/autocancel.sh new file mode 100644 index 0000000000..2273ea7c23 --- /dev/null +++ b/.circleci/autocancel.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +# Auto-cancel preceding workflows +# https://discuss.circleci.com/t/workaround-auto-cancel-redundant-builds-on-the-default-branch/39468 + +set -x + +## Get the name of the workflow and the related pipeline number +curl --header "Circle-Token: $PERS_API_TOKEN_BOOST_5" --request GET "https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}" -o current_workflow.json +WF_NAME=$(jq -r '.name' current_workflow.json) +CURRENT_PIPELINE_NUM=$(jq -r '.pipeline_number' current_workflow.json) +CURRENT_PIPELINE_CREATED=$(jq -r '.created_at' current_workflow.json) +TIME_THRESHOLD=$(date --utc +'%Y-%m-%dT%TZ' -d "${CURRENT_PIPELINE_CREATED} -10 minutes") + +## Get the IDs of pipelines created by the current user on the same branch. (Only consider pipelines that have a pipeline number inferior to the current pipeline) +PIPE_IDS=$(curl --header "Circle-Token: $PERS_API_TOKEN_BOOST_5" --request GET "https://circleci.com/api/v2/project/gh/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pipeline?branch=$CIRCLE_BRANCH"|jq -r --argjson CURRENT_PIPELINE_NUM "$CURRENT_PIPELINE_NUM" --arg TIME_THRESHOLD "${TIME_THRESHOLD}" '.items[] | select(.state == "created") | select(.number < $CURRENT_PIPELINE_NUM) | select(.created_at > $TIME_THRESHOLD) | .id') + +## Get the IDs of currently running/on_hold workflows that have the same name as the current workflow, in all previously created pipelines. +if [ ! -z "$PIPE_IDS" ]; then + for PIPE_ID in $PIPE_IDS + do + curl --header "Circle-Token: $PERS_API_TOKEN_BOOST_5" --request GET "https://circleci.com/api/v2/pipeline/${PIPE_ID}/workflow"|jq -r --arg WF_NAME "${WF_NAME}" '.items[]|select(.status == "on_hold" or .status == "running") | select(.name == $WF_NAME) | .id' >> WF_to_cancel.txt + done +fi + +## Cancel any currently running/on_hold workflow with the same name +if [ -s WF_to_cancel.txt ]; then + echo "Cancelling the following workflow(s):" + cat WF_to_cancel.txt + while read WF_ID; + do + curl --header "Circle-Token: $PERS_API_TOKEN_BOOST_5" --request POST https://circleci.com/api/v2/workflow/$WF_ID/cancel + done < WF_to_cancel.txt + ## Allowing some time to complete the cancellation + sleep 2 + else + echo "Nothing to cancel" +fi diff --git a/.circleci/config.yml b/.circleci/config.yml index 99f9515d4c..1fc7a63534 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,11 +1,12 @@ -version: 2 +version: 2.1 jobs: build: docker: - - image: cppalliance/boost_superproject_build:20.04-v4 + - image: cppalliance/boost_superproject_build:22.04-v1 parallelism: 2 steps: - checkout + - run: ./.circleci/autocancel.sh || true - run: wget "https://raw.githubusercontent.com/boostorg/release-tools/master/ci_boost_common.py" -P ${HOME} - run: wget "https://raw.githubusercontent.com/boostorg/release-tools/master/ci_boost_release.py" -P ${HOME} - run: python3 ${HOME}/ci_boost_release.py checkout_post