-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.sh
90 lines (81 loc) · 3.47 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
#!/bin/bash
### This works for local execution, not CircleCI
# REPO_NAME=$(basename `git rev-parse --show-toplevel`)
# BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
# STACK_NAME=${REPO_NAME}-${BRANCH_NAME}
# echo -e "STACK_NAME=" $STACK_NAME
# if [ "$BRANCH_NAME" != "master" ]; then {
# echo -e "Deploy works for MASTER branch only, current branch $BRANCH_NAME is not supported"
# exit 1
# }
# fi
### This works for CircleCI, not local execution
STACK_NAME=${CIRCLE_PROJECT_REPONAME}-${CIRCLE_BRANCH}
AWS_REGION="us-east-1"
AWS_S3_BUCKET="openapi-specifications"
echo -e "STACK_NAME=" $STACK_NAME
echo -e "AWS_REGION="$AWS_REGION
echo -e "AWS_S3_BUCKET="$AWS_S3_BUCKET
function checkCommandExitCode {
if [ $? -ne 0 ]; then {
echo -e $1 "command has failed"
exit 1
}
fi
}
echo -e "Checking if CloudFormation stack exists.."
aws cloudformation describe-stacks --region $AWS_REGION --stack-name $STACK_NAME
if [ $? -ne 0 ]; then {
echo -e "Starting CloudFormation stack create.."
aws cloudformation create-stack \
--region $AWS_REGION \
--capabilities CAPABILITY_NAMED_IAM \
--stack-name $STACK_NAME \
--template-body file://cloudformation.template \
--parameters \
ParameterKey=BucketName,ParameterValue=$AWS_S3_BUCKET
checkCommandExitCode "CloudFormation stack create"
WAIT_RESULT=$(aws cloudformation wait stack-create-complete --region $AWS_REGION --stack-name $STACK_NAME)
if [ "$WAIT_RESULT" == "Waiter StackCreateComplete failed: Waiter encountered a terminal failure state" ]; then {
echo -e "CloudFormation stack create has failed"
aws cloudformation describe-stack-events --region $AWS_REGION --stack-name $STACK_NAME
exit 1
}
fi
DEPLOY_RESULT=$(aws cloudformation describe-stacks --region $AWS_REGION --stack-name $STACK_NAME | jq --raw-output '.Stacks | .[] | .StackStatus')
if [ "$DEPLOY_RESULT" != "CREATE_COMPLETE" ]; then {
echo -e "CloudFormation stack create has failed"
aws cloudformation describe-stack-events --region $AWS_REGION --stack-name $STACK_NAME
exit 1
} else {
echo -e "CloudFormation stack create has passed successfully"
}
fi
} else {
echo -e "Starting CloudFormation stack update.."
aws cloudformation update-stack \
--region $AWS_REGION \
--capabilities CAPABILITY_NAMED_IAM \
--stack-name $STACK_NAME \
--template-body file://cloudformation.template \
--parameters \
ParameterKey=BucketName,ParameterValue=$AWS_S3_BUCKET
checkCommandExitCode "CloudFormation stack update"
WAIT_RESULT=$(aws cloudformation wait stack-update-complete --region $AWS_REGION --stack-name $STACK_NAME)
if [ "$WAIT_RESULT" == "Waiter StackCreateComplete failed: Waiter encountered a terminal failure state" ]; then {
echo -e "CloudFormation stack update has failed"
aws cloudformation describe-stack-events --region $AWS_REGION --stack-name $STACK_NAME
exit 1
}
fi
DEPLOY_RESULT=$(aws cloudformation describe-stacks --region $AWS_REGION --stack-name $STACK_NAME | jq --raw-output '.Stacks | .[] | .StackStatus')
if [ "$DEPLOY_RESULT" != "UPDATE_COMPLETE" ]; then {
echo -e "CloudFormation stack update has failed"
aws cloudformation describe-stack-events --region $AWS_REGION --stack-name $STACK_NAME
exit 1
} else {
echo -e "CloudFormation stack update has passed successfully"
}
fi
}
fi