Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if a CircleCI workflow is being run more than 1 at once
#!/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