-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3deploy.sh
executable file
·146 lines (121 loc) · 3.65 KB
/
s3deploy.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
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
#!/bin/bash
source .env
init(){
cd cloudflare/aws_s3_bucket && terraform init \
&& cd ../s3_workers/blue && terraform init \
&& cd ../green && terraform init \
&& cd ../proxy && terraform init
}
build_s3_bucket(){
cd cloudflare/aws_s3_bucket \
&& TF_VAR_bucket_name=${S3_BUCKET_NAME} \
terraform apply
}
build_s3_bucket_with_versioning(){
cd cloudflare/aws_s3_bucket \
&& TF_VAR_bucket_name=${S3_BUCKET_NAME} \
&& TF_VAR_versioning=true \
terraform apply
}
save_worker_to_s3(){
aws s3 ls s3://${S3_BUCKET_NAME}/workers/v${WORKER_VERSION}/worker.js
if [[ $? -ne 0 ]]; then
aws s3 cp ./dist/worker.js s3://${S3_BUCKET_NAME}/workers/v${WORKER_VERSION}/worker.js --content-type text/plain
else
echo -n "
This version already exists on S3. If this should be a new release,
then bump the version in your .env file and try again.
Whould you like to deploy version ${WORKER_VERSION}?
Only 'yes' will be accepted.
Enter a value: "
read reply </dev/tty
if [ -z "$reply" ]; then
reply=no
fi
if [ $reply == yes ]; then
echo "Attempting to deploy version ${WORKER_VERSION}"
else
echo "Deploy cancelled"
exit 1
fi
fi
}
save_proxy_to_s3(){
aws s3 ls s3://${S3_BUCKET_NAME}/workers/v${PROXY_VERSION}/worker_proxy.js
if [[ $? -ne 0 ]]; then
aws s3 cp ./dist/worker_proxy.js s3://${S3_BUCKET_NAME}/proxy_worker/v${PROXY_VERSION}/worker_proxy.js --content-type text/plain
else
echo -n "
This version already exists on S3. If this should be a new release,
then bump the version in your .env file and try again.
Whould you like to deploy proxy version ${PROXY_VERSION}?
Only 'yes' will be accepted.
Enter a value: "
read reply </dev/tty
if [ -z "$reply" ]; then
reply=no
fi
if [ $reply == yes ]; then
echo "Attempting to deploy proxy version ${PROXY_VERSION}"
else
echo "Deploy cancelled"
exit 1
fi
fi
}
deploy_worker(){
if [[ $1 == "blue" || $1 == "green" ]]; then
COLOR=$1 npm run build-worker \
&& save_worker_to_s3 \
&& cd cloudflare/workers/${1} \
&& TF_VAR_cloudflare_email=${ACCOUNT_EMAIL} \
TF_VAR_cloudflare_token=${ACCOUNT_AUTH_KEY} \
TF_VAR_worker_s3_key=workers/v${WORKER_VERSION}/worker.js \
terraform apply
else
echo "You must call this function with a color (blue or green). Try again with ./commands.sh deploy_worker blue/green"
exit 1
fi
}
deploy_proxy(){
npm run build-proxy \
&& save_proxy_to_s3 \
&& cd cloudflare/workers/proxy \
&& TF_VAR_cloudflare_email=${ACCOUNT_EMAIL} \
TF_VAR_cloudflare_token=${ACCOUNT_AUTH_KEY} \
TF_VAR_worker_s3_key=proxy_worker/v${PROXY_VERSION}/worker_proxy.js \
terraform apply
}
destroy_blue(){
cd cloudflare/s3_workers/blue \
&& TF_VAR_cloudflare_email=${ACCOUNT_EMAIL} \
TF_VAR_cloudflare_token=${ACCOUNT_AUTH_KEY} \
TF_VAR_worker_s3_key=workers/v${WORKER_VERSION}/worker.js \
terraform destroy
}
destroy_green(){
cd cloudflare/s3_workers/green \
&& TF_VAR_cloudflare_email=${ACCOUNT_EMAIL} \
TF_VAR_cloudflare_token=${ACCOUNT_AUTH_KEY} \
TF_VAR_worker_s3_key=workers/v${WORKER_VERSION}/worker.js \
terraform destroy
}
destroy_proxy(){
cd cloudflare/s3_workers/proxy \
&& TF_VAR_cloudflare_email=${ACCOUNT_EMAIL} \
TF_VAR_cloudflare_token=${ACCOUNT_AUTH_KEY} \
TF_VAR_worker_s3_key=proxy_worker/v${PROXY_VERSION}/worker_proxy.js \
terraform destroy
}
destroy_s3_bucket(){
cd cloudflare/aws_s3_bucket \
&& TF_VAR_bucket_name=${S3_BUCKET_NAME} \
terraform destroy
}
destroy_all(){
destroy_blue \
&& destroy_green \
&& destroy_proxy \
&& destroy_s3_bucket
}
"$@"