forked from bitovi/github-actions-deploy-eks-helm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
191 lines (153 loc) · 5.53 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
#!/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.* ]]; then
OCI_REGISTRY=false
else
if [[ ${HELM_REPOSITORY} =~ ^oci.* ]]; then
OCI_REGISTRY=true
else
echo "::error::Protocol handler expected here. Need http 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
# Check if namespace exists and create it if it doesn't.
KUBE_NAMESPACE_EXISTS=$(kubectl get namespaces | _grep ^${DEPLOY_NAMESPACE})
if [ -z "${KUBE_NAMESPACE_EXISTS}" ]; then
echo "The namespace ${DEPLOY_NAMESPACE} does not exists. Creating..."
kubectl create namespace "${DEPLOY_NAMESPACE}"
else
echo "The namespace ${DEPLOY_NAMESPACE} exists. Skipping creation..."
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
# Upgrade or install the chart. This does it all.
HELM_COMMAND="helm upgrade --install --timeout ${TIMEOUT} ${HELM_AUTH}"
# 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
HELM_COMMAND="${HELM_COMMAND} --set ${DEPLOY_VALUES}"
fi
if [ -n "$VERSION" ]; then
HELM_COMMAND="${HELM_COMMAND} --version ${VERSION}"
fi
if [ "${UPDATE_DEPS}" == "true" ]; then
HELM_COMMAND="${HELM_COMMAND} --dependency-update"
fi
elif [ "${HELM_ACTION}" == "uninstall" ]; then
HELM_COMMAND="helm uninstall --timeout ${TIMEOUT}"
else
echo "::error:: HELM_ACTION specified doesn't exist in this context. Please use 'install' or 'uninstall'"
exit 2
fi
if [ -n "$DEPLOY_NAMESPACE" ]; then
HELM_COMMAND="${HELM_COMMAND} -n ${DEPLOY_NAMESPACE}"
fi
# Execute Commands
HELM_COMMAND="${HELM_COMMAND} ${DEPLOY_NAME}"
if [ "${HELM_ACTION}" == "install" ]; then
if [ "${OCI_REGISTRY}" == "true" ]; then
DEPLOY_CHART_PATH="${HELM_REPOSITORY}/${DEPLOY_CHART_PATH}"
fi
HELM_COMMAND="${HELM_COMMAND} ${DEPLOY_CHART_PATH}"
fi
echo "Executing: ${HELM_COMMAND}"
${HELM_COMMAND}