This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ci.sh
80 lines (71 loc) · 1.97 KB
/
ci.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
#!/bin/bash
# Exit on any error
set -e
# Setting environment variables
if [ "${CIRCLE_BRANCH}" == "prod" ]; then
export DEPLOY_ENV="${DEPLOY_ENV_PROD}"
export PROJECT_NAME="${PROJECT_NAME_PROD}"
export CLUSTER_NAME="${CLUSTER_NAME_PROD}"
export CLOUDSDK_COMPUTE_ZONE="${CLOUDSDK_COMPUTE_ZONE_PROD}"
else
export DEPLOY_ENV="${DEPLOY_ENV_DEV}"
export PROJECT_NAME="${PROJECT_NAME_DEV}"
export CLUSTER_NAME="${CLUSTER_NAME_DEV}"
export CLOUDSDK_COMPUTE_ZONE="${CLOUDSDK_COMPUTE_ZONE_DEV}"
fi
# Image URL
export IMAGE_URL="gcr.io/${PROJECT_NAME}/${APP_NAME}"
# Configuring gcloud
setup-gcloud() {
# Export $ACCT_AUTH_PROD and $ACCT_AUTH_DEV in CircleCI web interface
if [ "${CIRCLE_BRANCH}" == "prod" ]; then
echo ${ACCT_AUTH_PROD} | base64 --decode -i > ${GOOGLE_APPLICATION_CREDENTIALS}
else
echo ${ACCT_AUTH_DEV} | base64 --decode -i > ${GOOGLE_APPLICATION_CREDENTIALS}
fi
gcloud auth activate-service-account --key-file ${GOOGLE_APPLICATION_CREDENTIALS}
gcloud config set project ${PROJECT_NAME}
gcloud config set container/cluster ${CLUSTER_NAME}
gcloud config set compute/zone ${CLOUDSDK_COMPUTE_ZONE}
gcloud container clusters get-credentials ${CLUSTER_NAME}
}
# Building the images
build() {
yarn
yarn build
docker build -t ${IMAGE_URL}:${CIRCLE_SHA1} .
}
# Test Cases
run-tests() {
echo "Add tests here"
}
# Tagging & Pushing Images to Registry
deploy() {
docker tag ${IMAGE_URL}:${CIRCLE_SHA1} ${IMAGE_URL}:latest
gcloud docker -- push ${IMAGE_URL}:${CIRCLE_SHA1}
gcloud docker -- push ${IMAGE_URL}:latest
# Deploy apps on GKE
FILENAME=$(grep -rl ${IMAGE_URL}:latest ./k8s/${DEPLOY_ENV}/)
for i in ${FILENAME}; do
sed -i -e "s/\(image:.*\):latest/\1:${CIRCLE_SHA1}/g" ${i}
done
kubectl apply -f ./k8s/${DEPLOY_ENV}/
}
case $1 in
setup-gcloud)
setup-gcloud
;;
run-tests)
run-tests
;;
build)
build
;;
deploy)
deploy
;;
*)
echo "Usage: $0 {setup-gcloud|run-tests|build|deploy}"
exit 1
;;
esac