-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sample
executable file
·60 lines (52 loc) · 2.38 KB
/
deploy.sample
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
#!/usr/bin/env bash
set -e
# Uploads a directory to an S3 bucket and invalidates a Cloudfront distribution.
# This script expects to be invoked with two arguments.
# $1 is expected to be the path of the directory to be uploaded
# $2 is expected to be the environment to be operated on (prd|dev)
readonly greenColor='\033[0;32m'
readonly redColor='\033[0;31m'
readonly noColor='\033[0m'
readonly prdS3Uri='s3://YOUR_PRD_URI'
readonly devS3Uri='s3://YOUR_DEV_URI'
s3Uri=''
readonly prdDistributionId='YOUR_PRD_ID'
readonly devDistributionId='YOUR_DEV_ID'
distributionId=''
readonly directory=$1
if [[ $# -ne 2 ]]; then
echo -e "${redColor}Expected two arguments.${noColor}"
echo -e "${redColor}Something like ${noColor}build dev${redColor} where${noColor}"
echo -e " ${redColor}Argument 1 is a relative path to a directory to upload to S3${noColor}"
echo -e " ${redColor}Argument 2 is 'dev' or 'prd'. This deploys to the defined 'dev' or 'prd' bucket.${noColor}"
exit 1
fi
if [[ $2 = 'prd' ]]; then
s3Uri="$prdS3Uri"
distributionId="$prdDistributionId"
elif [[ $2 = 'dev' ]]; then
s3Uri="$devS3Uri"
distributionId="$devDistributionId"
else
echo -e "${redColor}Second argument must be either ${noColor}prd${redColor} or ${noColor}dev"
exit 1
fi
echo -e "${greenColor}Uploading "$1" to bucket \"$s3Uri\"...${noColor}"
echo "\$ aws s3 cp "$directory" "$s3Uri" --recursive"
aws s3 cp "$directory" "$s3Uri" --recursive
echo -e "${greenColor}Upload complete.${noColor}"
echo -e "${greenColor}Creating invalidation for all paths for Cloudfront distribution "$distributionId"...${noColor}"
echo "\$ aws cloudfront create-invalidation --distribution-id "$distributionId" --paths '/*'"
invalidationOutput=$(aws cloudfront create-invalidation --distribution-id "$distributionId" --paths '/*')
echo "$invalidationOutput"
echo -e "${greenColor}Invalidation created. Waiting for invalidation to complete...${noColor}"
invalidationId=$(echo "$invalidationOutput" | awk '/"Id":/ { print $2 }' | tr -d [:punct:])
isCompleted=false
until [[ "$isCompleted" = true ]]; do
invalidationStatus=$(aws cloudfront get-invalidation --id "$invalidationId" --distribution-id "$distributionId" --query 'Invalidation.Status' --output text)
if [[ "$invalidationStatus" = "Completed" ]]; then
isCompleted=true
fi
done
echo -e "${greenColor}Invalidation completed.${noColor}"
echo -e "${greenColor}Deploy complete.${noColor}"