|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Static |
| 4 | +ORG="gh/voiceflow" |
| 5 | +BRANCH="master" |
| 6 | +REVISION="$CIRCLE_SHA1" |
| 7 | +PROJECT="$CIRCLE_PROJECT_REPONAME" |
| 8 | + |
| 9 | +list_pipelines() { |
| 10 | + ## |
| 11 | + # get paginated list of master pipelines for specified repo |
| 12 | + ## |
| 13 | + local PAGE_TOKEN |
| 14 | + PAGE_TOKEN="$1" |
| 15 | + PAGE_TOKEN="${PAGE_TOKEN:+&page-token=${PAGE_TOKEN}}" |
| 16 | + curl --fail --silent --request GET \ |
| 17 | + --url "https://circleci.com/api/v2/project/${ORG}/${PROJECT}/pipeline?branch=${BRANCH}${PAGE_TOKEN}" \ |
| 18 | + --header "Circle-Token: ${CIRCLECI_API_TOKEN}" |
| 19 | +} |
| 20 | + |
| 21 | +get_pipeline() { |
| 22 | + ## |
| 23 | + # find master pipeline by long commit hash |
| 24 | + ## |
| 25 | + local LENGTH |
| 26 | + local PIPELINES |
| 27 | + local PAGE_TOKEN |
| 28 | + local MAX_PAGES |
| 29 | + local COUNT |
| 30 | + |
| 31 | + LENGTH=0 |
| 32 | + COUNT=0 |
| 33 | + MAX_PAGES=10 |
| 34 | + |
| 35 | + while [[ "$LENGTH" == 0 && "$((COUNT++))" -lt "$MAX_PAGES" ]]; do |
| 36 | + PIPELINES=$(list_pipelines "${PAGE_TOKEN}") |
| 37 | + PAGE_TOKEN=$(<<<"$PIPELINES" jq -r '.next_page_token // empty' || echo "") |
| 38 | + |
| 39 | + LENGTH=$(<<<"$PIPELINES" jq --arg revision "${REVISION}" '.items | map(select(.vcs.revision == $revision and .state != "setup")) | length') |
| 40 | + done |
| 41 | + <<<"$PIPELINES" jq -r --arg revision "${REVISION}" '.items | map(select(.vcs.revision == $revision and .state != "setup")) | first | .id' |
| 42 | +} |
| 43 | + |
| 44 | +get_workflow() { |
| 45 | + ## |
| 46 | + # get non-setup workflow for pipeline |
| 47 | + ## |
| 48 | + local PIPELINE_ID |
| 49 | + PIPELINE_ID="$1" |
| 50 | + curl --fail --silent --request GET \ |
| 51 | + --url "https://circleci.com/api/v2/pipeline/${PIPELINE_ID?}/workflow" \ |
| 52 | + --header "Circle-Token: ${CIRCLECI_API_TOKEN}" \ |
| 53 | + | jq -r '.items | map(select(.name != "setup")) | first' |
| 54 | +} |
| 55 | + |
| 56 | +MASTER_PIPELINE_ID=$(get_pipeline) |
| 57 | + |
| 58 | +WORKFLOW=$(get_workflow "$MASTER_PIPELINE_ID") |
| 59 | + |
| 60 | +COUNT=0 |
| 61 | +MAX_RETRY=10 |
| 62 | +INTERVAL=60 |
| 63 | +while [[ "$((COUNT++))" -lt "$MAX_RETRY" ]]; do |
| 64 | + case $(<<<"$WORKFLOW" jq -r '.status') in |
| 65 | + "running") |
| 66 | + echo "waiting for master workflow to finish" |
| 67 | + echo "sleep ${INTERVAL}s..." |
| 68 | + sleep "$INTERVAL" |
| 69 | + WORKFLOW=$(get_workflow "$MASTER_PIPELINE_ID") |
| 70 | + ;; |
| 71 | + |
| 72 | + "success") |
| 73 | + echo "Found master workflow successfully completed" |
| 74 | + break |
| 75 | + ;; |
| 76 | + |
| 77 | + *) |
| 78 | + echo "master workflow not successful. exiting..." |
| 79 | + exit 1 |
| 80 | + ;; |
| 81 | + esac |
| 82 | +done |
| 83 | + |
| 84 | +echo "continuing" |
0 commit comments