-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdeploy.sh
executable file
·100 lines (79 loc) · 3.42 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
#!/bin/bash
set -e
export AWS_PROFILE=transitmatters
export AWS_REGION=us-east-1
export AWS_DEFAULT_REGION=us-east-1
export AWS_PAGER=""
PRODUCTION=false
CI=false
# Argument parsing
# pass "-p" flag to deploy to production
# pass "-c" flag if deploying with CI
while getopts "pc" opt; do
case $opt in
p)
PRODUCTION=true
;;
c)
CI=true
;;
esac
done
$PRODUCTION && ENV_SUFFIX="" || ENV_SUFFIX="-beta"
$PRODUCTION && CHALICE_STAGE="production" || CHALICE_STAGE="beta"
$PRODUCTION && STACK_NAME="ntt" || STACK_NAME="ntt-beta"
$PRODUCTION && FRONTEND_ZONE="traintracker.transitmatters.org" || FRONTEND_ZONE="labs.transitmatters.org"
$PRODUCTION && FRONTEND_CERT_ARN="$TM_NTT_CERT_ARN" || FRONTEND_CERT_ARN="$TM_LABS_WILDCARD_CERT_ARN"
$PRODUCTION && FRONTEND_DOMAIN_PREFIX="" || FRONTEND_DOMAIN_PREFIX="ntt-beta."
BACKEND_ZONE="labs.transitmatters.org"
BACKEND_CERT_ARN="$TM_LABS_WILDCARD_CERT_ARN"
$PRODUCTION && BACKEND_DOMAIN_PREFIX="traintracker-api." || BACKEND_DOMAIN_PREFIX="ntt-api-beta."
BACKEND_BUCKET=ntt-backend$ENV_SUFFIX
FRONTEND_HOSTNAME=$FRONTEND_DOMAIN_PREFIX$FRONTEND_ZONE # Must match in .chalice/config.json!
BACKEND_HOSTNAME=$BACKEND_DOMAIN_PREFIX$BACKEND_ZONE # Must match in .chalice/config.json!
# Ensure required secrets are set
if [[ -z "$DD_API_KEY" ]]; then
echo "Must provide DD_API_KEY in environment to deploy" 1>&2
exit 1
fi
# Fetch repository tags
# Run unshallow if deploying in CI
if $CI; then
git fetch --unshallow --tags
else
git fetch --tags
fi
# Identify the version and commit of the current deploy
GIT_VERSION=`git describe --tags --always`
GIT_SHA=`git rev-parse HEAD`
export GIT_ABR_VERSION=`git describe --tags --abbrev=0`
echo "Deploying version $GIT_VERSION | $GIT_SHA"
# Adding some datadog tags to get better data
DD_TAGS="git.commit.sha:$GIT_SHA,git.repository_url:github.com/transitmatters/new-train-tracker"
npm run build
echo "Deploying Train Tracker CloudFormation stack to $HOSTNAME..."
echo "View stack log here: https://$AWS_REGION.console.aws.amazon.com/cloudformation/home?region=$AWS_REGION"
pushd server/ > /dev/null
poetry export --without-hashes --output requirements.txt
poetry run chalice package --stage $CHALICE_STAGE --merge-template cloudformation.json cfn/
aws cloudformation package --template-file cfn/sam.json --s3-bucket $BACKEND_BUCKET --output-template-file cfn/packaged.yaml
aws cloudformation deploy --stack-name $STACK_NAME \
--template-file cfn/packaged.yaml \
--capabilities CAPABILITY_NAMED_IAM \
--no-fail-on-empty-changeset \
--parameter-overrides \
TMFrontendHostname=$FRONTEND_HOSTNAME \
TMFrontendZone=$FRONTEND_ZONE \
TMFrontendCertArn=$FRONTEND_CERT_ARN \
TMBackendCertArn=$BACKEND_CERT_ARN \
TMBackendHostname=$BACKEND_HOSTNAME \
TMBackendZone=$BACKEND_ZONE \
MbtaV3ApiKey=$MBTA_V3_API_KEY \
DDApiKey=$DD_API_KEY \
GitVersion=$GIT_VERSION \
DDTags=$DD_TAGS
popd > /dev/null
aws s3 sync dist/ s3://$FRONTEND_HOSTNAME
# Grab the cloudfront ID and invalidate its cache
CLOUDFRONT_ID=$(aws cloudfront list-distributions --query "DistributionList.Items[?Aliases.Items!=null] | [?contains(Aliases.Items, '$FRONTEND_HOSTNAME')].Id | [0]" --output text)
aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_ID --paths "/*"