Skip to content

Instantly share code, notes, and snippets.

@redthor
Last active December 14, 2022 14:59
Show Gist options
  • Save redthor/cc5b6f789b7ab98f4cadd5fcf65c8a24 to your computer and use it in GitHub Desktop.
Save redthor/cc5b6f789b7ab98f4cadd5fcf65c8a24 to your computer and use it in GitHub Desktop.
Bash script for getting bitbucket pipeline status
#!/bin/bash
#
# I made this because bitbucket does not a have a team view on all the
# pipeline builds.
#
# See https://community.atlassian.com/t5/Bitbucket-questions/Where-can-I-get-a-view-of-all-the-pipeline-builds-for-a-team/qaq-p/786264
#
# Requires curl and jq
#
# Use it like this:
#
# pipelines-status.sh <bb-api-pass> <bb-team> <bb-repo>
#
# Or to run a bunch in `parallel`, for example:
#
# ls <repos-dir> | parallel pipelines-status.sh <bb-api-pass> <bb-team> <bb-repo>
#
bitbucketApiPass=$1
team=$2
repo=$3
# BitBucket apiKey can be obtained from here:
# https://bitbucket.org/account/user/glowteam/api-key/
# or you'll need to use your own profile name/key
bitBucketCredentials=$team:$bitbucketApiPass
bitbucketv2=https://api.bitbucket.org/2.0
url=$bitbucketv2/repositories/$team/$repo/pipelines/?pagelen=100
curlResult=$(curl -Ss -u $bitBucketCredentials -H 'Content-Type: application/json' $url)
pipelineStatus=$(echo $curlResult |jq '.values |max_by(.build_number) |.state.result.name')
buildSec=$(echo $curlResult |jq '.values |max_by(.build_number) |.build_seconds_used')
buildTime=$(date -d@$buildSec -u '+%H:%M:%S')
echo -n $repo [$buildTime]
if [[ "$pipelineStatus" =~ FAIL ]]
then
pipelineUrl='https://bitbucket.org/'$team'/'$repo'/addon/pipelines/home#!/'
echo -e " \e[0;31m\e[1m"$pipelineStatus"\e[0m" "\e[0;36m$pipelineUrl\e[0m"
else
if [[ "${pipelineStatus}x" == "x" ]];
then
# Something went wrong with the curl "-S" should have printed it, but this might help:
echo -e " \e[0;31m\e[1m--"$pipelineResult"--\e[0m"
else
# Should be SUCCESS hopefully
echo -e " \e[0;32m"$pipelineStatus"\e[0m"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment