-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildspec.yml
59 lines (55 loc) · 2.17 KB
/
buildspec.yml
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
version: 0.2
# env:
# parameter-store:
# REACT_APP_HELLO_REACT: "/$ENV/REACT_APP_HELLO_REACT"
phases:
install:
commands:
- apt-get update
- apt install jq yarn
- node -v
- yarn version
pre_build:
commands:
- echo Logging in to Amazon ECR...
- FOLDER_NAME=$(echo $(date '+%Y%m%d%H%M%S'))
- NEW_ORIGIN_PATH="/$FOLDER_NAME"
- KEEP_RELEASES=3
- DIST_CONFIG_OLD_FILENAME="dist-config_old.json"
- DIST_CONFIG_NEW_FILENAME="dist-config_new.json"
build:
commands:
- echo Build started on `date`
- echo Building JS .....
- envsubst < ./codebuild-template/environment-template.yaml > .env
- yarn install
- yarn build
- echo Copying /build to S3 ....
- aws s3 cp --recursive ./build s3://$S3_DEPLOY_BUCKET/$FOLDER_NAME
post_build:
commands:
- echo Deploy frontend to S3 on `date`
- echo get $CLOUDFRONT_FRONTEND_DISTRIBUTION_ID current config
- aws cloudfront get-distribution --id $CLOUDFRONT_FRONTEND_DISTRIBUTION_ID > $DIST_CONFIG_OLD_FILENAME
- Etag=`cat $DIST_CONFIG_OLD_FILENAME | jq '.ETag' | tr -d \"`
- echo modify $CLOUDFRONT_FRONTEND_DISTRIBUTION_ID config with new value
- |
cat $DIST_CONFIG_OLD_FILENAME | jq \
--arg targetOriginId $CLOUDFRONT_FRONTEND_ORIGIN_ID \
--arg newOriginPath $NEW_ORIGIN_PATH \
'.Distribution.DistributionConfig | .Origins.Items = (.Origins.Items | map(if (.Id == $targetOriginId) then (.OriginPath = $newOriginPath) else . end))' \
> $DIST_CONFIG_NEW_FILENAME
- echo Update Cloudfront with new config
- |
aws cloudfront update-distribution --id $CLOUDFRONT_FRONTEND_DISTRIBUTION_ID \
--distribution-config "file://${DIST_CONFIG_NEW_FILENAME}" \
--if-match $Etag \
> /dev/null
- echo Invalidate the distribution to pick up the changes
- aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_FRONTEND_DISTRIBUTION_ID --paths "/*"
- echo Remove old releases folder
- chmod +x ./codebuild-template/remove-old-releases.sh
- ./codebuild-template/remove-old-releases.sh
artifacts:
files:
- '**/*'