-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathheroku.sh
executable file
·96 lines (81 loc) · 3.18 KB
/
heroku.sh
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# Warning: this script has only been tested on macOS Sierra. There's a good chance
# it won't work on other operating systems. If you get it working on another OS,
# please send a pull request with any changes required. Thanks!
set -e
cd `dirname $0`
r=`pwd`
echo $r
if [ -z "$(which heroku)" ]; then
echo "You must install the Heroku CLI first!"
echo "https://devcenter.heroku.com/articles/heroku-cli"
exit 1
fi
if ! echo "$(heroku plugins)" | grep -q heroku-cli-deploy; then
heroku plugins:install heroku-cli-deploy
fi
if ! echo "$(git remote -v)" | grep -q good-beer-server-; then
server_app=good-beer-server-$RANDOM
heroku create -r server $server_app
else
server_app=$(heroku apps:info -r server --json | python -c 'import json,sys;print json.load(sys.stdin)["app"]["name"]')
fi
serverUri="https://$server_app.herokuapp.com"
if ! echo "$(git remote -v)" | grep -q react-client-; then
client_app=react-client-$RANDOM
heroku create -r client $client_app
else
client_app=$(heroku apps:info -r client --json | python -c 'import json,sys;print json.load(sys.stdin)["app"]["name"]')
fi
clientUri="https://$client_app.herokuapp.com"
# replace the client URL in the server
sed -i -e "s|http://localhost:3000|$clientUri|g" $r/server/src/main/java/com/okta/developer/demo/beer/BeerController.java
# Deploy the server
cd $r/server
mvn clean package -DskipTests
heroku deploy:jar target/*jar -r server -o "--server.port=\$PORT"
heroku config:set -r server FORCE_HTTPS="true"
# Deploy the client
cd $r/client
rm -rf build
# replace the server URL in the client
sed -i -e "s|http://localhost:8080|$serverUri|g" $r/client/src/BeerList.tsx
yarn && yarn build
cd build
cat << EOF > static.json
{
"https_only": true,
"root": ".",
"routes": {
"/**": "index.html"
}
}
EOF
rm -f ../dist.tgz
tar -zcvf ../dist.tgz .
# TODO replace this with the heroku-cli-static command `heroku static:deploy`
source=$(curl -n -X POST https://api.heroku.com/apps/$client_app/sources -H 'Accept: application/vnd.heroku+json; version=3')
get_url=$(echo "$source" | python -c 'import json,sys;print json.load(sys.stdin)["source_blob"]["get_url"]')
put_url=$(echo "$source" | python -c 'import json,sys;print json.load(sys.stdin)["source_blob"]["put_url"]')
curl "$put_url" -X PUT -H 'Content-Type:' --data-binary @../dist.tgz
cat << EOF > build.json
{
"buildpacks": [{ "url": "https://github.com/heroku/heroku-buildpack-static" }],
"source_blob": { "url" : "$get_url" }
}
EOF
build_out=$(curl -n -s -X POST https://api.heroku.com/apps/$client_app/builds \
-d "$(cat build.json)" \
-H 'Accept: application/vnd.heroku+json; version=3' \
-H "Content-Type: application/json")
output_stream_url=$(echo "$build_out" | python -c 'import json,sys;print json.load(sys.stdin)["output_stream_url"]')
curl -s -L "$output_stream_url"
rm build.json
rm ../dist.tgz
# cleanup changed files
sed -i -e "s|$serverUri|http://localhost:8080|g" $r/client/src/BeerList.tsx
sed -i -e "s|$clientUri|http://localhost:3000|g" $r/server/src/main/java/com/okta/developer/demo/beer/BeerController.java
rm $r/client/src/BeerList.tsx-e
rm $r/server/src/main/java/com/okta/developer/demo/beer/BeerController.java-e
# show apps and URLs
heroku open -r client