-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh.template
executable file
·167 lines (149 loc) · 4.61 KB
/
deploy.sh.template
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
# Function to display usage
function usage() {
cat <<EOF
Usage: $0 [options]
Uploads the Angular site to AWS S3.
Options:
-h, --help Show this message and exit.
-d, --delete-first Remove specific contents in the target bucket before syncing.
-s, --sync Sync the local directory to the S3 bucket.
EOF
}
# AWS CloudFront Distribution ID
DISTID=""
# Check for necessary commands
if ! command -v ng &>/dev/null || ! command -v aws &>/dev/null; then
echo "Error: This script requires both 'ng' and 'aws' commands to be available."
exit 1
fi
# Default action is to show usage if no arguments are passed
if [ $# -eq 0 ]; then
usage
exit 1
fi
# Function to determine content type based on file extension
get_mime_type() {
local filename="$1"
local extension="${filename##*.}" # Get everything after the last dot
case "$extension" in
js)
echo "application/javascript"
;;
css)
echo "text/css"
;;
html)
echo "text/html"
;;
json)
echo "application/json"
;;
wasm)
# If your Angular app uses WebAssembly, set application/wasm
echo "application/wasm" # [2]
;;
map)
echo "application/json"
;;
*)
# Fallback to auto-detection via the 'file' command
file --mime-type -b "./dist/$filename"
;;
esac
}
# Function to perform sync
sync_to_s3() {
echo "Building Angular project..."
ng build --configuration production || {
echo "Build failed"
exit 1
}
echo "Checking for changed files..."
local local_files_checksum="./local_files_checksum.txt"
local s3_files_metadata="./s3_files_metadata.txt"
# Generate list of local files and their checksums
find ./dist -type f -exec md5sum {} + | sed 's|./dist/||' | sort >"$local_files_checksum"
# Get list of existing files in S3 and their checksums
aws s3api list-objects \
--bucket junipertcy.info \
--query "Contents[].{Key: Key, ETag: ETag}" \
--output text \
| awk '{print $1 " " $2}' \
| sed 's/"//g' \
| sort > "$s3_files_metadata"
# Sync files that differ by MD5 or don’t exist in S3
while IFS= read -r line; do
local md5=$(echo "$line" | awk '{print $1}')
local file_path=$(echo "$line" | awk '{print $2}')
local s3_md5=$(grep "$file_path" "$s3_files_metadata" | awk '{print $1}')
if [[ "$md5" != "$s3_md5" ]]; then
echo "MD5 mismatch or new file - Syncing file: $file_path: local=$md5 vs s3=$s3_md5"
metadata=$(aws s3api head-object \
--bucket junipertcy.info \
--key "$file_path" \
--query Metadata.md5checksum \
--output text \
--profile default 2>/dev/null)
if [[ "$md5" != "$metadata" ]]; then
mime_type=$(get_mime_type "$file_path")
aws s3 cp "./dist/$file_path" "s3://junipertcy.info/$file_path" \
--acl public-read \
--metadata md5checksum=$md5 \
--content-type "$mime_type" \
--profile default \
|| echo "Failed to sync $file_path"
else
echo "Yet, metadata match - No need to sync: $file_path"
fi
fi
done <"$local_files_checksum"
# Invalidate CloudFront cache
echo "Invalidating CloudFront distribution..."
aws cloudfront create-invalidation \
--distribution-id $DISTID \
--paths "/*" \
--profile default \
|| echo "Failed to invalidate CloudFront distribution"
# Cleanup
echo "Cleaning up temporary files..."
rm -f "$local_files_checksum" "$s3_files_metadata"
echo "Sync completed."
}
# Parse options
while getopts ":hds" option; do
case "${option}" in
h)
usage
exit 0
;;
d)
# Remove specific contents before syncing
aws s3 rm s3://junipertcy.info --recursive \
--exclude "*" \
--include "*.js" \
--include "*.css" \
--include "*.eot" \
--include "*.woff" \
--include "*.woff2" \
--profile default
sync_to_s3
exit 0
;;
s)
sync_to_s3
exit 0
;;
*)
usage
exit 1
;;
esac
done
# Shift off the options
shift $((OPTIND - 1))
# If no options were passed, show usage
if [ $# -eq 0 ]; then
usage
exit 1
fi