Last active
December 11, 2023 11:40
-
-
Save pduchnovsky/3c5c20bdbb91f209025ffb4283733da5 to your computer and use it in GitHub Desktop.
Bitbucket paused pipelines monitoring script | https://duchnovsky.com/2020/11/bitbucket-pipelines-monitoring/
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 | |
# Script developed by pduchnovsky | |
# https://duchnovsky.com/2020/11/bitbucket-pipelines-monitoring/ | |
#Variables definition: | |
BITBUCKET_CREDS="username:app_password" | |
BITBUCKET_WORKSPACE="workspace-name" | |
BITBUCKET_REPO_LIST="repo-1 repo-2 repo-3" | |
BITBUCKET_BRANCH_LIST="primary secondary tertiary" | |
#The actual hard work: | |
for BITBUCKET_REPO_SLUG in $BITBUCKET_REPO_LIST; do | |
curl -s -u $BITBUCKET_CREDS \ | |
"https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$BITBUCKET_REPO_SLUG/pipelines/?sort=-created_on&pagelen=20"| | |
jq -r '[.values[] | select((.trigger.name == "PUSH" or .trigger.name == "MANUAL"))]' > api_response.json | |
for BITBUCKET_BRANCH in $BITBUCKET_BRANCH_LIST; do | |
if [[ $(jq -r '.[]| select(.target.ref_name == "'$BITBUCKET_BRANCH'")| | |
select((.state.stage.name == "RUNNING") or (.state.stage.name == "PENDING"))' api_response.json) ]] | |
then | |
echo "$BITBUCKET_REPO_SLUG/$BITBUCKET_BRANCH -> There is a deployment in progress, doing nothing" | |
else | |
if [[ $(jq -r '[.[]| select(.target.ref_name == "'$BITBUCKET_BRANCH'")]| | |
.[0]|select(.state.stage.name == "HALTED")' api_response.json) ]] | |
then | |
echo "$BITBUCKET_REPO_SLUG/$BITBUCKET_BRANCH -> The latest deployment is paused, triggering new deployment" | |
curl -X POST -is -u $BITBUCKET_CREDS -H 'Content-Type: application/json' \ | |
"https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$BITBUCKET_REPO_SLUG/pipelines/" \ | |
-d '{ "target": { "ref_type": "branch", "type": "pipeline_ref_target", "ref_name": "'$BITBUCKET_BRANCH'" } }' | |
else | |
echo "$BITBUCKET_REPO_SLUG/$BITBUCKET_BRANCH -> Latest pipeline is not paused, doing nothing" | |
fi | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment