-
Notifications
You must be signed in to change notification settings - Fork 0
116 lines (101 loc) · 4.38 KB
/
code-deploy-client.yml
File metadata and controls
116 lines (101 loc) · 4.38 KB
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
name: Sync content with S3 Staging/Beta
on:
workflow_call:
inputs:
AWS_ACCOUNT:
required: true
type: string
STACK:
required: true
type: string
WORKING_DIR:
required: true
type: string
TARGET_BRANCH:
required: true
type: string
DOMAIN:
required: true
type: string
BUILD_DIR:
required: true
type: string
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
jobs:
# GitHub-Slack app sends a workflow status message with live updates to #code-* channel
sync:
name: Upload to Amazon S3
runs-on: ubuntu-24.04
permissions:
id-token: write
contents: read
steps:
- name: Build role name
run: |
# Use shell string manipulation to extract the repository name
REPO_NAME="${GITHUB_REPOSITORY#*/}"
ROLE_NAME="arn:aws:iam::${{ inputs.AWS_ACCOUNT }}:role/${REPO_NAME}-repo"
# Set the environment variable for future steps
echo "ROLE_NAME=${ROLE_NAME}" >> $GITHUB_ENV
- name: Configure web-sync AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ env.ROLE_NAME }}
aws-region: us-west-2
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ inputs.TARGET_BRANCH }}
- name: Get HELLO_VERSION
run: |
PACKAGE_FILE="package.json"
VERSION=$(jq -r '.version' "$PACKAGE_FILE")
echo "HELLO_VERSION=${VERSION}" >> $GITHUB_ENV
- name: Check repo version vs staged version
if: ${{ inputs.TARGET_BRANCH == 'main' }}
run: |
STAGE_VERSION=$(curl -sI "https://${{ inputs.DOMAIN }}/" | grep -i "x-amz-meta-version" | awk '{print $2}' | tr -d '\r')
echo "HELLO_VERSION: $HELLO_VERSION"
echo "STAGE_VERSION: $STAGE_VERSION"
if [ $STAGE_VERSION = $HELLO_VERSION ]; then
echo "$HELLO_VERSION has already been staged."
exit 1
fi
# Restore npm dependencies -----------------------
- name: Restore client node_modules
uses: actions/cache/restore@v4
id: client-node-modules-cache
with:
path: ./${{ inputs.WORKING_DIR }}/node_modules
key: ${{ runner.os }}-${{ runner.arch }}-client-node-modules-${{ hashFiles(format('{0}/package-lock.json', inputs.WORKING_DIR)) }}
- name: Install client dependencies
if: steps.client-node-modules-cache.outputs.cache-hit != 'true'
run: cd ${{ inputs.WORKING_DIR }} && npm ci
# ------------------------------------------------
- name: Build content
run: cd ${{ inputs.WORKING_DIR }} && npm run build
- name: Get bucket and distribution_id
run: |
BUCKET_NAME=$(aws cloudformation describe-stacks --stack-name ${{ inputs.STACK }} --query 'Stacks[0].Outputs[?OutputKey==`BucketNameOutput`].OutputValue' --output text)
DISTRIBUTION_ID=$(aws cloudformation describe-stacks --stack-name ${{ inputs.STACK }} --query 'Stacks[0].Outputs[?OutputKey==`DistributionIdOutput`].OutputValue' --output text)
echo "BUCKET_NAME=${BUCKET_NAME}" >> $GITHUB_ENV
echo "DISTRIBUTION_ID=${DISTRIBUTION_ID}" >> $GITHUB_ENV
- name: Cleanup S3 bucket
run: aws s3 rm s3://${BUCKET_NAME} --recursive
- name: Create versioned copies of root files for rollback and cp to S3
run: |
cd ${{inputs.BUILD_DIR}}
for file in *; do
if [ -f "$file" ]; then
aws s3 cp --metadata version=${HELLO_VERSION} --content-type "text/html; charset=UTF-8" --cache-control max-age=0 ${file} s3://${BUCKET_NAME}/
# dont create version files when deploying to beta
if [ "${{ inputs.TARGET_BRANCH }}" != "beta" ]; then
aws s3 cp --metadata version=${HELLO_VERSION} --content-type "text/html; charset=UTF-8" --cache-control max-age=0 ${file} s3://${BUCKET_NAME}/${HELLO_VERSION}/${file}
fi
fi
done
- name: Copy S3 assets to S3 bucket with the AWS CLI
run: aws s3 cp --metadata version=${HELLO_VERSION} ${{inputs.BUILD_DIR}}/assets s3://${BUCKET_NAME}/assets --recursive
- name: Invalidate CloudFront cache
run: aws cloudfront create-invalidation --distribution-id ${DISTRIBUTION_ID} --paths "/*"