-
Notifications
You must be signed in to change notification settings - Fork 2
/
pipeline-slug-ids
executable file
·75 lines (68 loc) · 3.02 KB
/
pipeline-slug-ids
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Capture slug IDs from a Heroku Pipeline exporting them into
# `TF_VARS_*_slug` environment variables for Terraform.
#
# Usage:
# export BUILD_PIPELINE_API=2f557b76-d685-452a-8651-9a6295a2a032
# export BUILD_PIPELINE_WEB_UI=26a3ecbf-8188-43ae-b0fe-be2d9e9fe26f
# source bin/pipeline-slug-ids
#
# When the Heroku API calls are successfull,
# `TF_VAR_{downcased_name}_slug_{stage}` variables will be exported
# for the current shell.
#
# The "downcased_name" part is extracted from the variable names,
# such as "web_ui" from "BUILD_PIPELINE_WEB_UI".
#
# A different prefix may be specified by setting
# `BUILD_PIPELINE_VAR_PREFIX`. The default is equivalent
# to `BUILD_PIPELINE_VAR_PREFIX=BUILD_PIPELINE_`.
# Function to extract slug IDs based on each app's pipeline coupling.
# Requires two arguments:
# 1. the slug name to identify it in variable `TF_VARS_*_slug`
# 2. the pipeline's latest-releases data
function capture_slug_ids {
slug_name=$1
releases_data=$2
if [ ! -z "$1" ]
then
pipeline_apps=(`echo $releases_data | jq -r 'map(.app.id) | unique | @sh'`)
for ix in ${!pipeline_apps[*]}
do
eval app_id=${pipeline_apps[$ix]} # remove single-quotes
url="https://api.heroku.com/apps/$app_id/pipeline-couplings"
>&2 echo "🔸 GET $url"
coupling_stage=`curl -sS --fail $url -H "Authorization: Bearer $HEROKU_API_KEY" -H "Accept: application/vnd.heroku+json; version=3" -H "Content-Type: application/json" | jq -r '.stage'`
if [ ! -z "$coupling_stage" ]
then
slug_id=`echo "$releases_data" | jq --arg app_id $app_id -r '.[] | select(.app.id == $app_id) | .slug.id'`
echo "🔹 TF_VAR_${slug_name}_slug_${coupling_stage}=${slug_id}"
eval "export TF_VAR_${slug_name}_slug_${coupling_stage}=${slug_id}"
fi
done
fi
}
# The Heroku API key to fetch pipeline & app data.
if [ -z "$HEROKU_API_KEY" ]
then
>&2 echo "Error: requires HEROKU_API_KEY set to an auth token"
return
fi
# These are the pipelines used to build the slugs.
var_prefix="${BUILD_PIPELINE_VAR_PREFIX:-BUILD_PIPELINE_}"
eval 'pipeline_vars=(${!'"$var_prefix"'@})'
if [ "${#pipeline_vars[*]}" = "0" ]
then
echo "Error: no pipelines specified, set using \"${var_prefix}\"-prefixed environment variables"
return
fi
for pipeline_var in ${pipeline_vars[*]}
do
pipeline_name=`echo "${pipeline_var#*${var_prefix}}" | tr '[:upper:]' '[:lower:]'`
pipeline_id=`eval echo '$'"${pipeline_var}"`
# Fetch the most recent release data for each pipeline.
# TODO These use pre-release `version=3.pipelines` APIs. Update when released.
>&2 echo "🔸 GET https://api.heroku.com/pipelines/${pipeline_id}/latest-releases"
pipeline_data=`curl -sS --fail "https://api.heroku.com/pipelines/${pipeline_id}/latest-releases" -H "Authorization: Bearer $HEROKU_API_KEY" -H "Accept: application/vnd.heroku+json; version=3.pipelines" -H "Content-Type: application/json" -H "Range: version ..; order=desc, max=10"`
# Extract slug IDs based on each app's pipeline coupling
capture_slug_ids "$pipeline_name" "$pipeline_data"
done