-
Notifications
You must be signed in to change notification settings - Fork 28
/
deploy.sh
226 lines (186 loc) · 6.77 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env bash
set -euo pipefail
# catch exit 1 (which is a special case in grep meaning 'no matches') so that we can use pipefail
_grep() { grep "$@" || test $? = 1; }
HELM_AUTH=""
OCI_REGISTRY=false
# Check repository type
if [ -n "${HELM_REPOSITORY}" ]; then
if [[ ${HELM_REPOSITORY} =~ ^http.* ]] || [[ ${HELM_REPOSITORY} =~ ^s3.* ]]; then
OCI_REGISTRY=false
else
if [[ ${HELM_REPOSITORY} =~ ^oci.* ]]; then
OCI_REGISTRY=true
else
echo "::error::Protocol handler expected here. Need http, s3 or oci."
exit 1
fi
fi
fi
# First Install any required helm plugins
if [ -n "${PLUGINS_LIST}" ]; then
plugins=${PLUGINS_LIST//,/ }
for plugin in $plugins
do
echo "installing helm plugin: [$plugin]"
helm plugin install $plugin
done
fi
helm plugin list
echo "Logging into kubernetes cluster $CLUSTER_NAME"
if [ -n "$CLUSTER_ROLE_ARN" ]; then
aws eks \
--region ${AWS_REGION} \
update-kubeconfig --name ${CLUSTER_NAME} \
--role-arn=${CLUSTER_ROLE_ARN}
else
aws eks \
--region ${AWS_REGION} \
update-kubeconfig --name ${CLUSTER_NAME}
fi
# If defined, will set up authentication parameters
if [ "${HELM_ACTION}" == "install" ] && [ "${OCI_REGISTRY}" != "true" ]; then
# Authentication management
if [ -n "$CA_FILE" ]; then
HELM_AUTH="${HELM_AUTH} --ca-file ${CA_FILE}"
fi
if [ -n "$CERT_FILE" ]; then
HELM_AUTH="${HELM_AUTH} --cert-file ${CERT_FILE}"
fi
if [ -n "$KEY_FILE" ]; then
HELM_AUTH="${HELM_AUTH} --key-file ${KEY_FILE}"
fi
if [ -n "$SKIP_TLS" ]; then
HELM_AUTH="${HELM_AUTH} --insecure-skip-tls-verify"
fi
if [ -n "$PASS_CREDENTIALS" ]; then
HELM_AUTH="${HELM_AUTH} --pass-credentials"
fi
if [ -n "$REPO_USERNAME" ]; then
HELM_AUTH="${HELM_AUTH} --username ${REPO_USERNAME}"
fi
if [ -n "$REPO_PASSWORD" ]; then
HELM_AUTH="${HELM_AUTH} --password ${REPO_PASSWORD}"
fi
fi
# Checking to see if a repo URL is in the path, if so add it or update. Validate it's not an OCI based registry.
if [ -n "${HELM_REPOSITORY}" ] && [ "${OCI_REGISTRY}" != "true" ]; then
HELM_CHART_NAME="${DEPLOY_CHART_PATH%/*}"
# Need this to avoid exit if no repo exists
HELM_REPOS=$(helm repo list || true)
CHART_REPO_EXISTS=$(echo $HELM_REPOS | _grep ^${HELM_CHART_NAME})
if [ -z "${CHART_REPO_EXISTS}" ]; then
echo "Adding repo ${HELM_CHART_NAME} (${HELM_REPOSITORY})"
helm repo add ${HELM_CHART_NAME} ${HELM_REPOSITORY} ${HELM_AUTH}
if ! [ $? == 0 ]; then
echo "::error::Something went wrong adding the helm repository."
echo "Please check the logs. If you consider this a bug, please submit an issue in our repo."
exit 1
fi
else
echo "Updating repo ${HELM_CHART_NAME}"
helm repo update "${HELM_CHART_NAME}"
if ! [ $? == 0 ]; then
echo "::error::Something went wrong updating the helm repository."
exit 1
fi
fi
fi
# If OCI registry, we need to login before performing any action
if [ "${OCI_REGISTRY}" == "true" ] ; then
if [ -z "${REPO_PASSWORD}" ] || [ -z "${REPO_USERNAME}" ] ; then
echo "Missing credentials to login to ECR registry"
else
echo "Logging into helm registry"
echo "${REPO_PASSWORD}" | helm registry login ${HELM_REPOSITORY#oci://} --username ${REPO_USERNAME} --password-stdin
if ! [ $? == 0 ]; then
echo "::error::Something went wrong with logging into the ECR Registry."
exit 1
fi
fi
fi
# Proceed with installation procedure
if [ "${HELM_ACTION}" == "install" ]; then
if [ -n "${USE_SECRETS_VALS}" ]; then
HELM_COMMAND="helm secrets --backend vals --evaluate-templates true upgrade --install --timeout ${TIMEOUT} ${HELM_AUTH}"
else
# Upgrade or install the chart. This does it all.
HELM_COMMAND="helm upgrade --install --timeout ${TIMEOUT} ${HELM_AUTH}"
fi
# If we should wait, then do so
if [ -n "${HELM_WAIT}" ]; then
HELM_COMMAND="${HELM_COMMAND} --wait"
fi
# Add atomic flag
if [ -n "${HELM_ATOMIC}" ]; then
HELM_COMMAND="${HELM_COMMAND} --atomic"
fi
for config_file in ${DEPLOY_CONFIG_FILES//,/ }
do
HELM_COMMAND="${HELM_COMMAND} -f ${config_file}"
done
if [ -n "$DEPLOY_VALUES" ]; then
for value in ${DEPLOY_VALUES//,/ }
do
if [[ -z "$value" ]]; then
echo "Warning: Empty value detected, skipping."
continue
elif [[ "$value" != *"="* ]]; then
echo "Warning: Value '$value' does not contain an '=' sign, skipping."
continue
elif [[ "$value" =~ [[:space:]] ]]; then
echo "Warning: Value '$value' contains whitespace, skipping."
continue
fi
HELM_COMMAND="${HELM_COMMAND} --set ${value}"
done
fi
if [ -n "$VERSION" ]; then
HELM_COMMAND="${HELM_COMMAND} --version ${VERSION}"
fi
if [ "${UPDATE_DEPS}" == "true" ]; then
HELM_COMMAND="${HELM_COMMAND} --dependency-update"
fi
if [ "${DRY_RUN}" == "true" ]; then
HELM_COMMAND="${HELM_COMMAND} --dry-run"
fi
elif [ "${HELM_ACTION}" == "uninstall" ]; then
HELM_COMMAND="helm uninstall --timeout ${TIMEOUT}"
if [ "${DRY_RUN}" == "true" ]; then
HELM_COMMAND="${HELM_COMMAND} --dry-run"
fi
elif [ "${HELM_ACTION}" == "list" ]; then
HELM_COMMAND="helm list"
else
echo "::error:: HELM_ACTION specified doesn't exist in this context. Please use 'install','uninstall' or 'list'"
exit 2
fi
if [ -n "$DEPLOY_NAMESPACE" ]; then
HELM_COMMAND="${HELM_COMMAND} -n ${DEPLOY_NAMESPACE}"
fi
# Create namespace if it doesn't exist. Requires cluster API permissions.
# Only applies to `install` option. `helm upgrade` is not used.
if [ "${CREATE_NAMESPACE}" == "true" ] && [ "${HELM_ACTION}" == "install" ]; then
HELM_COMMAND="${HELM_COMMAND} --create-namespace"
fi
if [ "${HELM_ACTION}" == "list" ]; then
if [ -n "$DEPLOY_NAME" ]; then
HELM_COMMAND="${HELM_COMMAND} --filter"
else
HELM_COMMAND="${HELM_COMMAND} --all"
fi
fi
# Execute Commands
HELM_COMMAND="${HELM_COMMAND} ${DEPLOY_NAME}"
if [ "${HELM_ACTION}" == "install" ]; then
if [ "${OCI_REGISTRY}" == "true" ]; then
if [ -n "${DEPLOY_CHART_PATH}" ]; then
DEPLOY_CHART_PATH="${HELM_REPOSITORY}/${DEPLOY_CHART_PATH}"
else
DEPLOY_CHART_PATH="${HELM_REPOSITORY}"
fi
fi
HELM_COMMAND="${HELM_COMMAND} ${DEPLOY_CHART_PATH}"
fi
echo "Executing: ${HELM_COMMAND} ${HELM_EXTRA_ARGS}"
${HELM_COMMAND} ${HELM_EXTRA_ARGS}