-
Notifications
You must be signed in to change notification settings - Fork 4
/
continuous_deployment.sh
executable file
·63 lines (54 loc) · 3.13 KB
/
continuous_deployment.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
#!/usr/bin/env bash
OPENSSL_CMD="openssl aes-256-cbc -K $encrypted_76474c7b2cee_key -iv $encrypted_76474c7b2cee_iv -in secret-mojp-k8s-ops.json.enc -out k8s-ops-secret.json -d"
K8S_OPS_REPO_SLUG=Beit-Hatfutsot/mojp-k8s
K8S_OPS_REPO_DIRECTORY=mojp-k8s
K8S_OPS_DOCKER_IMAGE=orihoch/mojp-k8s
K8S_OPS_ENVIRONMENT=production
SIMPLE_APP_DEPLOYMENT_NAME="back"
SIMPLE_APP_DEPLOYMENT_IMAGE="gcr.io/bh-org-01/mojp-dbs-back"
SIMPLE_APP_CONTAINER_NAME="back"
DEPLOYMENT_SCRIPT="
./update_yaml.py '{"'"'"${SIMPLE_APP_DEPLOYMENT_NAME}"'"'":{"'"'"image"'"'":"'"'"${SIMPLE_APP_DEPLOYMENT_IMAGE}:${TRAVIS_COMMIT}"'"'"}}' \
values.${K8S_OPS_ENVIRONMENT}.auto-updated.yaml &&\
git config user.email deployment-bot'@'k8s-ops &&\
git config user.name deployment-bot &&\
git pull https://${K8S_OPS_GITHUB_REPO_TOKEN}'@'github.com/${K8S_OPS_REPO_SLUG}.git master &&\
git add values.${K8S_OPS_ENVIRONMENT}.auto-updated.yaml &&\
git commit -m '${SIMPLE_APP_DEPLOYMENT_NAME} image update --no-deploy' &&\
git push https://${K8S_OPS_GITHUB_REPO_TOKEN}'@'github.com/${K8S_OPS_REPO_SLUG}.git master &&\
kubectl set image deployment/${SIMPLE_APP_DEPLOYMENT_NAME} ${SIMPLE_APP_CONTAINER_NAME}=${SIMPLE_APP_DEPLOYMENT_IMAGE}:$TRAVIS_COMMIT &&\
kubectl rollout status deployment "${SIMPLE_APP_DEPLOYMENT_NAME}"
"
# preflight checks - we don't fail on any of there, just skip the deployment
[ "${TRAVIS_COMMIT}" == "" ] &&\
echo "missing commit from travis - skipping deployment" && exit 0
[ "${TRAVIS_COMMIT_MESSAGE}" == "" ] &&\
echo "missing commit message from travis - skipping deployment" && exit 0
[ "${K8S_CONTINUOUS_DEPLOYMENT}" != "true" ] &&\
echo "K8S_CONTINUOUS_DEPLOYMENT is not true - skipping deployment" && exit 0
[ "${TRAVIS_PULL_REQUEST}" != "false" ] &&\
echo "TRAVIS_PULL_REQUEST is not false - skipping deployment" && exit 0
[ "${TRAVIS_BRANCH}" != "master" ] &&\
echo "TRAVIS_BRANCH is not master - skipping deployment" && exit 0
echo "${TRAVIS_COMMIT_MESSAGE}" | grep -- --no-deploy >/dev/null &&\
echo "--no-deploy commit flag is set - skipping deployment" && exit 0
# decrypt the service account secret json
! $OPENSSL_CMD &&\
echo "failed to decrypt service account secret" && exit 1
! docker pull "${K8S_OPS_DOCKER_IMAGE}" &&\
echo "failed to pull docker image" && exit 1
if [ "${K8S_OPS_REPO_SLUG}" == "${TRAVIS_REPO_SLUG}" ]; then
echo "skipping cloning of ops repo as this is the current repo (as reported by TRAVIS_REPO_SLUG)"
K8S_OPS_REPO_DIRECTORY="./"
else
# clone the k8s repo as a subdirectory of the current project's repo
! git clone --depth 1 "https://github.com/${K8S_OPS_REPO_SLUG}.git" "${K8S_OPS_REPO_DIRECTORY}" &&\
echo "failed to clone k8s repo" && exit 1
fi
! docker run -it -v "`pwd`/k8s-ops-secret.json:/k8s-ops/secret.json" \
-v "`pwd`/${K8S_OPS_REPO_DIRECTORY}:/ops" \
"${K8S_OPS_DOCKER_IMAGE}" \
-c "source ~/.bashrc && source switch_environment.sh ${K8S_OPS_ENVIRONMENT};
${DEPLOYMENT_SCRIPT}" &&\
echo "failed to run DEPLOYMENT_SCRIPT: ${DEPLOYMENT_SCRIPT}" && exit 1
exit 0