-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun
executable file
·277 lines (236 loc) · 9.04 KB
/
run
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env bash
set -e
SHORT_HASH=$(git rev-parse --short=7 HEAD)
BUILD_ID="$4"
TAG="$SHORT_HASH-$BUILD_ID"
ENV_INPUT=$(echo "$2" | awk '{print tolower($0)}')
ENV="$ENV_INPUT"
ENVIRONMENT_SLUG="$5"
if [ "$ENV_INPUT" = "review" ]; then
ENV='test'
fi
KEY_VAULT="k8sconfig-$ENV-kv"
RESOURCE_GROUP_NAME="k8scluster-$ENV-rg"
KUBERNETES_CLUSTER_NAME="k8scluster-$ENV-aks"
CONTAINER_REGISTRY_NAME=alvkubernetesclustertestacr
CONTAINER_REGISTRY="$CONTAINER_REGISTRY_NAME.azurecr.io"
SERVICE_TO_BUILD="$3"
SUBSCRIPTION="k8s-$ENV-subscription"
PULL_REQUEST_NUMBER="$6"
function getSecret() {
az keyvault secret show --vault-name $KEY_VAULT --name $1 | jq '.value' -r
}
function getSecrets() {
ALV_HOSTNAME="$(getSecret alv-hostname)"
ALVB_HOSTNAME="$(getSecret alvb-hostname)"
ALVBIOTECH_HOSTNAME="$(getSecret alvbiotech-hostname)"
SP_ALVTIME_ADMIN_RBAC_SECRET="$(getSecret sp-alvtime-admin-rbac-secret)"
SP_ALVTIME_ADMIN_CLIENT_ID="$(getSecret sp-alvtime-admin-client-id)"
SANITY_TOKEN_ALVB="$(getSecret sanity-token-alvb)"
SANITY_TOKEN="$(getSecret sanity-token)"
YT_API="$(getSecret youtube-api-token)"
SENDGRID_API_KEY="$(getSecret sendgrid-api-key)"
MAILTO="$(getSecret mail-api-recipient)"
AIRTABLE_APP_ID="$(getSecret airtable-app-id)"
AIRTABLE_TABLE_ID="$(getSecret airtable-table-id)"
AIRTABLE_API_KEY="$(getSecret airtable-api-key)"
VIRUSCHECK_URL="$(getSecret viruscheck-url)"
MAILTO_ALVB="$(getSecret mail-api-alvb-recipient)"
GITHUB_COMMENT_PR_TOKEN=$(getSecret github-comment-pr-token)
}
function buildServices() {
if [[ "website" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "building alv website"
if [ ! -f packages/website/.env.production ]; then
echo "ENV=$ENV" >>packages/website/.env.production
echo "SANITY_TOKEN=$SANITY_TOKEN" >>packages/website/.env.production
echo "YT_API=$YT_API" >>packages/website/.env.production
if [ "$ENV" != "prod" ]; then
echo "SANITY_DATASET=development" >>packages/website/.env.production
fi
fi
docker build . \
--file website.Dockerfile \
--tag "$CONTAINER_REGISTRY/website:$TAG" \
--no-cache
fi
if [[ "website-alvb" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "building alv-b website-no"
echo "SANITY_TOKEN=$SANITY_TOKEN_ALVB" >packages/website-alvb/.env.production
echo "GATSBY_HOSTNAME=www.$ALVB_HOSTNAME" >>packages/website-alvb/.env.production
echo "GATSBY_TRANSLATED_DOMAIN=www.$ALVBIOTECH_HOSTNAME" >>packages/website-alvb/.env.production
docker build . \
--file website-alvb.Dockerfile \
--tag "$CONTAINER_REGISTRY/website-alvb-no:$TAG" \
--no-cache
echo "building alv-b website-en"
echo "SANITY_TOKEN=$SANITY_TOKEN_ALVB" >packages/website-alvb/.env.production
echo "GATSBY_HOSTNAME=www.$ALVBIOTECH_HOSTNAME" >>packages/website-alvb/.env.production
echo "GATSBY_TRANSLATED_DOMAIN=www.$ALVB_HOSTNAME" >>packages/website-alvb/.env.production
echo "GATSBY_LOCALE=en" >>packages/website-alvb/.env.production
docker build . \
--file website-alvb.Dockerfile \
--tag "$CONTAINER_REGISTRY/website-alvb-en:$TAG" \
--no-cache
fi
if [[ "mail-api" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "building mail api"
docker build . \
--file mail-api.Dockerfile \
--tag "$CONTAINER_REGISTRY/mail-api:$SHORT_HASH" \
--no-cache
fi
if [[ "video-party" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "building video party"
echo "YT_API=$YT_API" >>packages/video-party/.env
docker build . \
--file packages/video-party/Dockerfile \
--tag "$CONTAINER_REGISTRY/video-party:$SHORT_HASH" \
--no-cache
fi
}
function pushServices() {
if [[ "website" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "pushing website"
docker push "$CONTAINER_REGISTRY/website:$TAG"
fi
if [[ "website-alvb" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "pushing website-no"
docker push "$CONTAINER_REGISTRY/website-alvb-no:$TAG"
fi
if [[ "website-alvb" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "pushing website-en"
docker push "$CONTAINER_REGISTRY/website-alvb-en:$TAG"
fi
if [[ "mail-api" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" && -n "$EXISTING_HASH" ]]; then
echo "pushing mail api"
docker push "$CONTAINER_REGISTRY/mail-api:$SHORT_HASH"
fi
if [[ "video-party" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "pushing video party"
docker push "$CONTAINER_REGISTRY/video-party:$SHORT_HASH"
fi
}
function commentPullRequest() {
curl \
--header Accept: application/vnd.github+json \
--header "Authorization: token $GITHUB_COMMENT_PR_TOKEN" \
"https://api.github.com/repos/Alv-no/alv-website/issues/$2/comments" \
--data "{\"body\": \"💻 $1\"}"
}
function getAksCredentials() {
# Retrieve the access credentials for your cluster and automatically configure kubectl
az aks get-credentials \
--subscription "$SUBSCRIPTION" \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$KUBERNETES_CLUSTER_NAME"
kubelogin convert-kubeconfig -l azurecli
}
function undeployServices() {
CHART_NAME_SUFFIX="-$ENVIRONMENT_SLUG"
helm uninstall "website$CHART_NAME_SUFFIX" \
--namespace "alv-no"
}
function deployServices() {
export HELM_EXPERIMENTAL_OCI=1
echo "$SP_ALVTIME_ADMIN_RBAC_SECRET" | helm registry login "$CONTAINER_REGISTRY" \
--username "$SP_ALVTIME_ADMIN_CLIENT_ID" \
--password-stdin
if [[ -d "chart" ]] && [[ "$CI" ]]; then
echo "The chart folder exists on this filesystem. And this is runing in CI"
echo "Deleting the chart folder in order for the next command to work"
rm -rf chart
fi
helm pull "oci://$CONTAINER_REGISTRY/helm/chart" --version v1 --untar
if [[ "website" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "helm website"
CHART_NAME_SUFFIX=''
HOSTS="$ALV_HOSTNAME,www.$ALV_HOSTNAME"
if [ "$ENV_INPUT" = "review" ]; then
CHART_NAME_SUFFIX="-$ENVIRONMENT_SLUG"
HOSTS="$ENVIRONMENT_SLUG.review.$ALV_HOSTNAME"
fi
helm upgrade "website$CHART_NAME_SUFFIX" ./chart \
--install \
--set image.repository="$CONTAINER_REGISTRY/website" \
--set image.tag="$TAG" \
--set "hosts={$HOSTS}" \
--namespace "alv-no"
if [ "$ENV_INPUT" = "review" ]; then
commentPullRequest "https://$HOSTS" $PULL_REQUEST_NUMBER
fi
fi
if [[ "website-alvb" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "helm website-no"
helm upgrade website-alvb ./chart \
--install \
--set image.repository="$CONTAINER_REGISTRY/website-alvb-no" \
--set image.tag="$TAG" \
--set "hosts={$ALVB_HOSTNAME,www.$ALVB_HOSTNAME}" \
--namespace "alvb-no"
sleep 20
echo "helm website-en"
helm upgrade website-alvb-en ./chart \
--install \
--set image.repository="$CONTAINER_REGISTRY/website-alvb-en" \
--set image.tag="$TAG" \
--set "hosts={$ALVBIOTECH_HOSTNAME,www.$ALVBIOTECH_HOSTNAME}" \
--namespace "alvb-no"
sleep 20
#curl "https://$ALVB_HOSTNAME" # Won't test for now until certificats is correctly configured for alv-b
fi
if [[ "mail-api" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "helm mail api"
helm upgrade mail-api ./chart \
--install \
--set image.repository="$CONTAINER_REGISTRY/mail-api" \
--set image.tag="$SHORT_HASH" \
--set "hosts={mail-api.$ALV_HOSTNAME}" \
--set secrets.MAILTO="$MAILTO" \
--set secrets.SENDGRID_API_KEY="$SENDGRID_API_KEY" \
--set secrets.AIRTABLE_APP_ID="$AIRTABLE_APP_ID" \
--set secrets.AIRTABLE_TABLE_ID="$AIRTABLE_TABLE_ID" \
--set secrets.AIRTABLE_API_KEY="$AIRTABLE_API_KEY" \
--set secrets.VIRUSCHECK_URL="$VIRUSCHECK_URL" \
--namespace "alv-no"
sleep 20
curl "https://mail-api.$ALV_HOSTNAME"
fi
if [[ "mail-api-alvb" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "helm mail api alvb"
helm upgrade mail-api-alvb ./chart \
--install \
--set image.repository="$CONTAINER_REGISTRY/mail-api" \
--set image.tag="$SHORT_HASH" \
--set "hosts={mail-api-alvb.$ALVB_HOSTNAME}" \
--set secrets.MAILTO="$MAILTO_ALVB" \
--set secrets.SENDGRID_API_KEY="$SENDGRID_API_KEY" \
--namespace "alvb-no"
sleep 20
curl "https://mail-api-alvb.$ALVB_HOSTNAME"
fi
if [[ "video-party" = "$SERVICE_TO_BUILD" || -z "$SERVICE_TO_BUILD" ]]; then
echo "helm video-party"
helm upgrade video-party ./chart \
--install \
--set image.repository="$CONTAINER_REGISTRY/video-party" \
--set image.tag="$SHORT_HASH" \
--set "hosts={video-party.$ALV_HOSTNAME}" \
--namespace "alv-no"
sleep 20
curl "https://video-party.$ALV_HOSTNAME"
fi
}
if [ "deploy" = "$1" ]; then
getSecrets
az acr login --name $CONTAINER_REGISTRY_NAME
buildServices
pushServices
getAksCredentials
deployServices
fi
if [ "undeploy" = "$1" ]; then
getSecrets
getAksCredentials
undeployServices
fi