generated from cloudposse-github-actions/composite-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
entrypoint.sh
executable file
·84 lines (65 loc) · 2.94 KB
/
entrypoint.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
#!/bin/bash
set -e
if [ ! -z ${PATH_OVERRIDE+x} ]; then
export PATH=${PATH_OVERRIDE}:${PATH}
fi;
## Install required versions
apt-get update && apt-get install -y \
kubectl=${KUBECTL_VERSION}-1 \
chamber=${CHAMBER_VERSION}-1 \
helm=${HELM_VERSION}-1 \
helmfile=${HELMFILE_VERSION}-1
helm plugin install https://github.com/databus23/helm-diff --version v${HELM_DIFF_VERSION} \
&& helm plugin install https://github.com/aslafy-z/helm-git --version ${HELM_GIT_VERSION} \
&& rm -rf $XDG_CACHE_HOME/helm
# Used for debugging
aws ${AWS_ENDPOINT_OVERRIDE:+--endpoint-url $AWS_ENDPOINT_OVERRIDE} sts --region ${AWS_REGION} get-caller-identity
# Login to Kubernetes Cluster.
aws ${AWS_ENDPOINT_OVERRIDE:+--endpoint-url $AWS_ENDPOINT_OVERRIDE} eks --region ${AWS_REGION} update-kubeconfig --name ${CLUSTER_NAME}
# Read platform specific configs/info
chamber export platform/${CLUSTER_NAME}/${ENVIRONMENT} --format yaml | yq --exit-status --no-colors eval '{"platform": .}' - > /tmp/platform.yaml
APPLICATION_HELMFILE=$(pwd)/${HELMFILE_PATH}/${HELMFILE}
BASIC_ARGS="--namespace ${NAMESPACE} --environment ${ENVIRONMENT} --file ${APPLICATION_HELMFILE} --state-values-file /tmp/platform.yaml"
EXTRA_VALUES_ARGS=""
DEBUG_ARGS=""
if [[ "${HELM_DEBUG}" == "true" ]]; then
DEBUG_ARGS=" --debug"
fi
if [[ -n "$HELM_VALUES_YAML" ]]; then
echo -e "Using extra values:\n${HELM_VALUES_YAML}"
HELM_VALUES_FILE="/tmp/extra_helm_values.yml"
echo "$HELM_VALUES_YAML" > "$HELM_VALUES_FILE"
EXTRA_VALUES_ARGS="--state-values-file /tmp/extra_helm_values.yml"
fi
if [[ "${OPERATION}" == "deploy" ]]; then
OPERATION_COMMAND="helmfile ${BASIC_ARGS} ${EXTRA_VALUES_ARGS} ${DEBUG_ARGS} apply"
echo "Executing: ${OPERATION_COMMAND}"
${OPERATION_COMMAND}
RELEASES=$(helmfile ${BASIC_ARGS} ${EXTRA_VALUES_ARGS} ${DEBUG_ARGS} list --output json | jq .[].name -r)
for RELEASE in ${RELEASES}
do
echo "Processing release: ${RELEASE}"
echo "Executing kubectl command: kubectl --namespace ${NAMESPACE} get -l ${RELEASE_LABEL_NAME}=${RELEASE} ${URL_RESOURCE_TYPE} -o json"
ENTRYPOINT=$(kubectl --namespace ${NAMESPACE} get -l ${RELEASE_LABEL_NAME}=${RELEASE} ${URL_RESOURCE_TYPE} -o json | jq --raw-output '[.items[].metadata.annotations["outputs.webapp-url"]] | first')
if [[ "${ENTRYPOINT}" != "" ]]; then
echo "Found webapp-url for release ${RELEASE}: ${ENTRYPOINT}"
echo "webapp-url=${ENTRYPOINT}" >> $GITHUB_OUTPUT
else
echo "No webapp-url found for release ${RELEASE}"
fi
done
elif [[ "${OPERATION}" == "destroy" ]]; then
set +e
kubectl get ns ${NAMESPACE}
NAMESPACE_EXISTS=$?
set -e
if [[ ${NAMESPACE_EXISTS} -eq 0 ]]; then
OPERATION_COMMAND="helmfile ${BASIC_ARGS} ${EXTRA_VALUES_ARGS} ${DEBUG_ARGS} destroy"
echo "Executing: ${OPERATION_COMMAND}"
${OPERATION_COMMAND}
RELEASES_COUNTS=$(helm --namespace ${NAMESPACE} list --output json | jq 'length')
if [[ "${RELEASES_COUNTS}" == "0" ]]; then
kubectl delete ns ${NAMESPACE}
fi
fi
fi