|
| 1 | +#!/bin/bash |
| 2 | +set -eu |
| 3 | +set -o pipefail |
| 4 | + |
| 5 | +if [ -z "${TPS_HOSTNAME:-}" ]; then |
| 6 | + TPS_HOSTNAME="tps.heroku.tools" |
| 7 | +fi |
| 8 | + |
| 9 | +if [ -z "${TPS_API_TOKEN:-}" ]; then |
| 10 | + echo "Requires environment variable: TPS_API_TOKEN" >&2 |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +# Argument overrides the environment variable |
| 15 | +component_slug="${1:-$COMPONENT_SLUG}" |
| 16 | +if [ -z "$component_slug" ]; then |
| 17 | + echo "Requires first argument: Heroku component slug" >&2 |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +release_sha="${2:-$RELEASE_SHA}" |
| 22 | +if [ -z "$release_sha" ]; then |
| 23 | + echo "Requires second argument: SHA of the commit being released" >&2 |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +actor_email="${3:-$ACTOR_EMAIL}" |
| 28 | +if [ -z "$actor_email" ]; then |
| 29 | + echo "Requires third argument: email of actor performing the release" >&2 |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +app_id="${3:-$APP_ID}" |
| 34 | +if [ -z "$app_id" ]; then |
| 35 | + echo "Requires fourth argument: UUID of app being released" >&2 |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +stage="${3:-$STAGE}" |
| 40 | +if [ -z "$stage" ]; then |
| 41 | + echo "Requires fifth argument: stage of the release" >&2 |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +description="Deploy ${release_sha} of ${component_slug} in ${stage}" |
| 46 | + |
| 47 | +response_status=0 |
| 48 | + |
| 49 | +tpsRecordRelease() { |
| 50 | + response_status="$(curl --silent \ |
| 51 | + -o tpsRecordRelease_response.txt -w "%{response_code}" \ |
| 52 | + -X POST \ |
| 53 | + -H "Accept: */*" \ |
| 54 | + -H "Content-Type: application/json" \ |
| 55 | + -H "Authorization: Bearer ${TPS_API_TOKEN}" \ |
| 56 | + -d "{\"release\": {\"sha\": \"${release_sha}\", \"component_slug\": \"${component_slug}\", \"actor_email\": \"${actor_email}\", \"app_id\": \"${app_id}\", \"stage\": \"${stage}\", \"description\": \"${description}\"}}" \ |
| 57 | + https://${TPS_HOSTNAME}/api/component/${component_slug}/releases)" |
| 58 | +} |
| 59 | + |
| 60 | +echo "Recording release with ${TPS_HOSTNAME}…" >&2 |
| 61 | +retry_count=0 |
| 62 | +set +e |
| 63 | +tpsRecordRelease |
| 64 | +until [ "$response_status" == "200" -o "$response_status" == "201" ] |
| 65 | +do |
| 66 | + tpsRecordRelease |
| 67 | + ((retry_count++)) |
| 68 | + if [ $retry_count -gt 120 ] |
| 69 | + then |
| 70 | + echo "❌ Could not record release for \"$component_name\" after retrying for 30-minutes." >&2 |
| 71 | + exit 2 |
| 72 | + fi |
| 73 | + echo Response status $response_status: $(cat tpsRecordRelease_response.txt) >&2 |
| 74 | + echo "⏳ Retry in 15-seconds…" >&2 |
| 75 | + sleep 15 |
| 76 | +done |
| 77 | +set -e |
| 78 | +echo "✅ Release recorded" >&2 |
0 commit comments