-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy
executable file
·104 lines (87 loc) · 2.14 KB
/
deploy
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
#!/usr/bin/env bash
project=roger-api
service=push
# Configuration YAML is piped into kubectl below!
set -euf -o pipefail
echo -e "Building and deploying \x1B[97m$service\x1B[0m..."
echo
# Ensure that the working directory is clean.
if [[ -n $(git status --porcelain) ]]; then
echo -e "\x1B[91mPlease commit or revert changes before deploying.\x1B[0m"
exit 1
fi
# Update remote branch.
git fetch
# Ensure that the local branch and the remote branch are in sync.
if [[ "$(git rev-parse @)" != "$(git rev-parse @{u})" ]]; then
echo -e "\x1B[91mLocal and remote branch are out of sync. Please pull/push any changes.\x1B[0m"
exit 1
fi
# Calculate a tag to use for the deploy.
# TODO: First check if there's already a tag for the current commit.
tag=""
tag_number=1
while true; do
tag="$(date -u +%Y-%m-%d)/$(printf %03d $tag_number)"
git rev-parse "$tag" >/dev/null 2>&1 || break
(( tag_number++ ))
done
echo -en "\x1B[92mReady to build and deploy.\x1B[0m Tag as \"$tag\" and deploy? (Y/n) "
# Confirm that the deploy should still happen.
read answer
case "$answer" in
[yY]|[yY][Ee][Ss]|"") ;;
*) echo -e "\x1B[91mAborting.\x1B[0m"; exit 1;;
esac
# Docker and Kubernetes friendly version.
version="${tag//\//-}"
echo
docker build -t gcr.io/$project/$service:$version .
echo
gcloud docker -- push gcr.io/$project/$service:$version
echo
cat << YAML | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: $service
labels:
app: $service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: $service
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: $service
spec:
replicas: 5
template:
metadata:
labels:
app: $service
spec:
containers:
- name: $service
image: gcr.io/$project/$service:$version
ports:
- containerPort: 8080
YAML
# Check if deploy was successful.
if [[ $? -ne 0 ]]; then
echo
echo -e "\x1B[91mDeploy was aborted.\x1B[0m"
exit 1
fi
echo
echo -e "\x1B[92mEverything checks out.\x1B[0m Tagging and pushing the tag."
echo
git tag "$tag"
git push --tags
echo
echo -e "\x1B[92mAll done!\x1B[0m Au revoir!"