Last active
March 29, 2022 15:18
-
-
Save magickatt/212fc0c17d705373790fdf282e386133 to your computer and use it in GitHub Desktop.
Check if a CircleCI workflow is being run more than 1 at once
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
#!/bin/bash | |
# Project in the form vcs-type/organisation-name/repository-name | |
PROJECT=github/magickatt/example | |
# First, get all the Pipeline IDs for this workflow (triggered by a Git tag from a new release) | |
PIPELINE_IDS=(`curl --silent GET https://circleci.com/api/v2/project/$PROJECT/pipeline \ | |
--header "Circle-Token: $CIRCLE_API_TOKEN" \ | |
| jq --raw-output '.items | map(select(.vcs.tag != null)) | .[].id'`) | |
if [ -z "$PIPELINE_IDS" ]; then | |
echo "Pipeline IDs cannot be determined, did you specify the project parameter correctly?" | |
exit 1 | |
fi | |
# Second, check if any of those Pipelines are currently running or on-hold | |
CONCURRENT_WORKFLOWS=0 | |
for PIPELINE_ID in "${PIPELINE_IDS[@]}" | |
do | |
: | |
WORKFLOW_COUNT=`curl --silent GET https://circleci.com/api/v2/pipeline/$PIPELINE_ID/workflow \ | |
--header "Circle-Token: $CIRCLE_API_TOKEN" \ | |
| jq --raw-output '[.items | .[] | select( .status | contains("running") or contains("on_hold"))] | length'` | |
if [ -z "$WORKFLOW_COUNT" ]; then | |
continue | |
fi | |
CONCURRENT_WORKFLOWS=$((CONCURRENT_WORKFLOWS + WORKFLOW_COUNT)) | |
done | |
# If there is more than 1 (including this very pipeline) then stop | |
if [ "$CONCURRENT_WORKFLOWS" -gt "1" ]; then | |
echo "Attempting to run more than 1 pipeline for this workflow" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment