#!/bin/bash
#PURPOSE: Copy an existing production Heroku slug to 4 alternate URLs, using the Heroku Platform API.
#MORE INFO: https://devcenter.heroku.com/articles/platform-api-copying-slugs
#USAGE: ./cloneSlug.sh myappname 2>/dev/null
APPNAME=$1
SLUG_ID=$(curl -H "Accept: application/vnd.heroku+json; version=3" -n https://api.heroku.com/apps/${APPNAME}/releases | tail -15 | awk '/"slug"/,/"id"/' | grep '"id":' | cut -d: -f2 | sed 's/"//g')
#Example: the output of the above curl is the entire version history of the app in the form of JSON text data obtained from your Heroku account (see below).
#Next, we feed the last 15 lines of that data to awk to isolate consecutive lines having properties "slug" & "id", inclusive.
#From those lines, we isolate the "id" line and parse it to isolate the raw slug ID string.
# :
# :
# {
# "addon_plan_names":[],
# "app":{
# "id":"c9bff57f-bb82-4742-ab66-79ea8eb98c6c",
# "name":"myappname"
# },
# "created_at":"2020-03-21T21:29:47Z",
# "description":"Deploy 559ab324",
# "status":"succeeded",
# "id":"768f3f32-a367-430f-a493-86d1395f20f1",
# "slug":{
# "id":"7d3af19-5ac9-4a80-93ac-f1a49af4e75a"
# },
# "updated_at":"2020-03-21T21:29:47Z",
# "user":{
# "email":"youremail@gmail.com",
# "id":"1fe9f387-fdcf-4776-85a6-2ffd19f2bd57"
# },
# "version":43,
# "current":true,
# "output_stream_url":null
# }
#Once we have SLUG_ID, pass it to the following curl to ask Heroku to copy that slug over to 4 other app slugs.
for i in {1..4}
do
echo -n "copying ${APPNAME} to ${APPNAME}0$i ..."
curl -X POST -H "Accept: application/vnd.heroku+json; version=3" -n -H "Content-Type: application/json" -d '{"slug": "'${SLUG_ID}'"}' https://api.heroku.com/apps/${APPNAME}0$i/releases | grep 'status' | sed 's/"//g; s/,$//'
done
#The result is that the same app can be launched using 5 different URLs. Example:
# https://myappname.herokuapp.com
# https://myappname01.herokuapp.com
# https://myappname02.herokuapp.com
# https://myappname03.herokuapp.com
# https://myappname04.herokuapp.com