-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.sh
executable file
·72 lines (60 loc) · 2.48 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
#!/usr/bin/env bash
# Set defaults and tell the user what we're doing.
echo "Build ID: ${BUILD_ID:=anonymous_build}"
echo "Commit SHA: $COMMIT_SHA"
echo "Short SHA: $SHORT_SHA"
echo "Branch: $BRANCH_NAME"
echo "Tag: ${TAG_NAME:=latest}"
# Kustomization dirs.
PRODUCTION_KUSTOMIZATIONS=kubernetes/lookit/environments/production
STAGING_KUSTOMIZATIONS=kubernetes/lookit/environments/staging
MANIFESTS=kubernetes/manifests
# 1) Get proper kustomizations dir.
# Branches --> environment directories.
declare -A BRANCH_ENV_MAP=(
[master]="$PRODUCTION_KUSTOMIZATIONS"
[develop]="$STAGING_KUSTOMIZATIONS"
)
TARGET_KUSTOMIZATIONS=${BRANCH_ENV_MAP[$BRANCH_NAME]}
printf "\ntarget kustomizations directory: %s\n" "$TARGET_KUSTOMIZATIONS"
# 2) Get correct manifests dir.
# Branches --> manifest directories.
declare -A BRANCH_MANIFESTS_MAP=(
[master]="$MANIFESTS/production"
[develop]="$MANIFESTS/staging"
)
MANIFESTS_TARGET="${BRANCH_MANIFESTS_MAP[$BRANCH_NAME]}"
mkdir -p "$MANIFESTS_TARGET"
printf "\ntarget manifests directory: %s\n" "$MANIFESTS_TARGET"
# 3) Get correct Makefile target for secret decryption.
# Branches --> Makefile targets.
declare -A BRANCH_DECRYPTER_MAP=(
[master]="decrypt-prod"
[develop]="decrypt-staging"
)
DECRYPTER_MAKE_TARGET="${BRANCH_DECRYPTER_MAP[$BRANCH_NAME]}"
make "${DECRYPTER_MAKE_TARGET}"
# 4) Generate top-level kustomization from build env vars.
# FYI: We can also use kustomize edit to propogate build vars.
# https://github.com/kubernetes-sigs/kustomize/blob/master/docs/eschewedFeatures.md#build-time-side-effects-from-cli-args-or-env-variables
# but for now we're just doing environment variable templating.
envsubst \
< kubernetes/lookit/base/kustomization.template.yaml \
> kubernetes/lookit/base/kustomization.yaml
envsubst \
< kubernetes/lookit/transformers/dynamic-labels.template.yaml \
> kubernetes/lookit/transformers/dynamic-labels.yaml
# 5) Generate the actual configs.
kustomize build --reorder none -o "$MANIFESTS_TARGET" "${TARGET_KUSTOMIZATIONS}"
# 6) Deploy to the correct cluster.
# Branches --> Clusters.
declare -A BRANCH_CLUSTERS_MAP=(
[master]="lookit-production"
[develop]="lookit-staging"
)
TARGET_CLUSTER="${BRANCH_CLUSTERS_MAP[$BRANCH_NAME]}"
printf "\ntarget cluster for deployment: %s\n" "$TARGET_CLUSTER"
gcloud container clusters get-credentials "$TARGET_CLUSTER" --zone=us-east1-d
# TODO: Figure out a better way than nuking the jobs every time.
kubectl -n default delete job --all
kubectl apply -f "$MANIFESTS_TARGET"