Skip to content

Instantly share code, notes, and snippets.

@hkdnet
Created July 5, 2021 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkdnet/dde79c01dec803dbf6fb72c0d986b619 to your computer and use it in GitHub Desktop.
Save hkdnet/dde79c01dec803dbf6fb72c0d986b619 to your computer and use it in GitHub Desktop.
Kick and wait CircleCI WF
#!/bin/bash -eux
set -o pipefail
org=$1
repo=$2
branch=$3
get_status() {
curl -fs --retry 3 -X GET -u ${CIRCLE_TOKEN}: \
--url "https://circleci.com/api/v2/pipeline/$1/workflow"
}
output=$(
curl -fs --retry 3 -X POST -u ${CIRCLE_TOKEN}: \
--url "https://circleci.com/api/v2/project/gh/$org/$repo/pipeline" \
--header 'content-type: application/json' \
--data "{\"branch\":\"$branch\"}"
)
pipeline_id=$(echo $output | jq -r ".id")
pipeline_number=$(echo $output | jq -r ".number")
set +x
while true; do
build_output=$(get_status $pipeline_id)
# Ref: https://circleci.com/docs/api/v2/#operation/getWorkflowById
# "success" "running" "not_run" "failed" "error" "failing" "on_hold" "canceled" "unauthorized"
# And it can be null when the items is empty (not yet run).
build_status=$(echo $build_output | jq -r '.items[0].status')
build_id=$(echo $build_output | jq -r '.items[0].id')
case "$build_status" in
success)
exit 0
;;
null|running|not_run)
printf .
sleep 5
;;
*)
echo
echo "finished with status:\"${build_status}\"!"
echo "Check: https://app.circleci.com/pipelines/github/$org/$repo/${pipeline_number}/workflows/${build_id}"
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment