This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
deploy.sh
executable file
·148 lines (138 loc) · 3.26 KB
/
deploy.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
function usage() {
echo "Usage: $(basename "$0") [option...] {development|staging|production}" >&2
echo
echo " Coding Garden Community App API deployment script"
echo " Deploys the API to the specified environment on now.sh"
echo
echo " -h, --help Show this message"
echo " -n, --now-token Specify the now token. (or set environment variable \$NOW_TOKEN)"
echo " -e, --node-env Specify the node environemt. (or set environment variable \$NODE_ENV)"
echo " -m, --mongo-uri Specify the mongo uri. (or set environment variable \$MONGO_URI)"
echo " -a, --alias Specify the deploy alias. (or set environment variable \$DEPLOY_ALIAS)"
echo
exit 1
}
while :
do
case "$1" in
-h|--help)
usage
exit 0
;;
-n|--now-token)
# TODO: validate input length and chars
NOW_TOKEN="$2"
shift 2
;;
-m|--mongo-uri)
# TODO: validate input length and chars
MONGO_URI="$2"
shift 2
;;
-e|--node-env)
# TODO: validate input length and chars
NODE_ENV="$2"
shift 2
;;
-a|--alias)
# TODO: validate input length and chars
DEPLOY_ALIAS="$2"
shift 2
;;
--)
shift
break
;;
-*)
echo "Error: Unknown option: $1" >&2
echo
usage
exit 1
;;
*)
break
;;
esac
done
if [ -z "$NOW_TOKEN" ]; then
echo "Error: NOW_TOKEN is not set via environment variable or as argument"
echo
usage
exit 1
fi
if [ "$1" ]; then
env=$1
elif [ -n "$TRAVIS_BRANCH" ]; then
case "$TRAVIS_BRANCH" in
develop)
env=development
;;
staging)
env=staging
;;
master)
env=production
;;
*)
echo "Missing or invalid environment."
usage
exit 1
;;
esac
fi
case "$env" in
development)
if [ -z "$NODE_ENV" ]; then
NODE_ENV=development
fi
if [ -z "$DEPLOY_ALIAS" ]; then
DEPLOY_ALIAS=api-dev.codinggarden.community
fi
if [ -z "$MONGO_URI" ]; then
MONGO_URI=@community-app-db-dev
fi
;;
staging)
if [ -z "$NODE_ENV" ]; then
NODE_ENV=development
fi
if [ -z "$DEPLOY_ALIAS" ]; then
DEPLOY_ALIAS=api-staging.codinggarden.community
fi
if [ -z "$MONGO_URI" ]; then
MONGO_URI=@community-app-db-staging
fi
;;
production)
if [ -z "$NODE_ENV" ]; then
NODE_ENV=production
fi
if [ -z "$DEPLOY_ALIAS" ]; then
DEPLOY_ALIAS=api.codinggarden.community
fi
if [ -z "$MONGO_URI" ]; then
MONGO_URI=@community-app-db-prod
fi
;;
*)
echo "Missing or invalid environment."
usage
exit 1
;;
esac
if [ -z "$MONGO_URI" ]; then
echo "Error: MONGO_URI is not set via environment variable or as argument"
echo
usage
exit 1
fi
if [ -z "$NOW_TOKEN" ]; then
echo "Error: NOW_TOKEN is not set via environment variable or as argument"
echo
usage
exit 1
fi
echo "Deploying to $env environment with alias $DEPLOY_ALIAS"
DEPLOYMENT_URL=$(npx now --token "$NOW_TOKEN" deploy -e NODE_ENV="$NODE_ENV" -e MONGO_URI="$MONGO_URI" -e ADMIN_SECRET=@community-api-admin-secret)
npx now --token "$NOW_TOKEN" alias $DEPLOYMENT_URL $DEPLOY_ALIAS