-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.sh
executable file
·79 lines (70 loc) · 2.86 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
#!/bin/sh
set -e
SCRIPT_DIR="$(dirname "$0")"
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <env> <dockerVersionTag>"
echo "Example: $0 test latest"
exit 1
fi
# --------------------------------------------------------------------------------
# PARAMS
# --------------------------------------------------------------------------------
BASE_NAME=ktorbase
ENV=$1
SERVICE_IMAGE_VERSION=$2
STACK_NAME=$BASE_NAME-$ENV
TEMPLATE_FILE=$SCRIPT_DIR/../aws/templates/$BASE_NAME.yml
PARAM_FILE=$SCRIPT_DIR/../aws/templates/$STACK_NAME.json
TEMP_PARAM_FILE=$(mktemp)
# --------------------------------------------------------------------------------
# HELPER FUNCTIONS
# --------------------------------------------------------------------------------
echoDemarcation() {
TEXT=$1
echo
echo "--------------------------------------------------------------------------"
echo "$STACK_NAME: $TEXT"
echo "--------------------------------------------------------------------------"
}
# --------------------------------------------------------------------------------
# TRIGGER DEPLOY
# --------------------------------------------------------------------------------
echoDemarcation "Deploy Cloud Formation Template ..."
aws --version
jq -r "(.[] | select(.ParameterKey == \"ServiceImageVersion\") | .ParameterValue) |= \"$SERVICE_IMAGE_VERSION\"" "$PARAM_FILE" > $TEMP_PARAM_FILE
DEPLOY_RES=$(
aws cloudformation deploy \
--template-file "${TEMPLATE_FILE}" \
--stack-name "${STACK_NAME}" \
--capabilities CAPABILITY_NAMED_IAM \
--no-execute-changeset \
--no-fail-on-empty-changeset \
--parameter-overrides "file://$TEMP_PARAM_FILE"
)
echo "$DEPLOY_RES"
# --------------------------------------------------------------------------------
# DESCRIBE AND EXECUTE CHANGE SET IF CREATED
# --------------------------------------------------------------------------------
DESCRIBE_CMD=$(echo "$DEPLOY_RES" | tail -n1)
if echo "$DESCRIBE_CMD" | grep -q "describe-change-set"; then
DESCRIBE_RES=$(eval "$DESCRIBE_CMD")
CHANGE_SET_ID=$(echo "$DESCRIBE_RES" | tr '\r\n' ' ' | jq -r .ChangeSetId)
echo "$DESCRIBE_RES"
aws cloudformation execute-change-set \
--change-set-name "$CHANGE_SET_ID" \
--stack-name "$STACK_NAME"
fi
# --------------------------------------------------------------------------------
# CLEAN UP FAILED CHANGE SETS
# will be created if there were no changes
# -> see https://github.com/aws/aws-cli/issues/4534
# --------------------------------------------------------------------------------
echoDemarcation "Clean up failed change sets ..."
CHANGE_SETS=$(aws cloudformation list-change-sets \
--stack-name "${STACK_NAME}" \
--query "Summaries[?Status==\`FAILED\`].ChangeSetId" \
--output text)
for CHANGE_SET in $CHANGE_SETS; do
echo "$STACK_NAME: Deleting change set: ${CHANGE_SET} ..."
aws cloudformation delete-change-set --change-set-name "${CHANGE_SET}"
done