-
Notifications
You must be signed in to change notification settings - Fork 8
210 lines (197 loc) · 8.04 KB
/
amplify-deploy.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
name: Amplify Deploy
on:
workflow_call:
inputs:
os:
required: true
type: string
node-version:
required: true
type: string
amplify-app-id:
description: "The ID for your Amplify app (which you can get from the end of its ARN)"
required: true
type: string
package-name:
description: "Name of the application being deployed"
required: true
type: string
package-dist-directory:
description: "The directory where the package dist is located"
required: true
type: string
bucket-name-preview:
description: "Name of the preview bucket being deployed to"
required: false
type: string
bucket-name-main:
description: "Name of the main bucket being deployed to"
required: true
type: string
build-script:
required: false
type: string
default: 'build'
destination-dir:
required: false
type: string
default: '/'
sub-domain-suffix:
required: false
type: string
env:
AMPLIFY_ID: ${{ inputs.amplify-app-id }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'eu-west-1'
BRANCH_NAME: ${{ github.event_name == 'pull_request' && format('pr{0}', github.event.number) || (github.ref == 'refs/heads/main' && 'main' || github.ref == 'refs/heads/master' && 'master') }}
BUCKET_NAME: ${{ github.event_name == 'pull_request' && inputs.bucket-name-preview || (github.ref == 'refs/heads/main' && inputs.bucket-name-main || github.ref == 'refs/heads/master' && inputs.bucket-name-main) }}
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
ZIP_NAME: ${{ github.event_name == 'pull_request' && format('{0}-{1}-preview.zip', inputs.package-name, github.event.number) || (github.ref == 'refs/heads/main' && format('{0}-main.zip', inputs.package-name) || github.ref == 'refs/heads/master' && format('{0}-master.zip', inputs.package-name)) }}
jobs:
deploy:
runs-on: ${{ inputs.os }}
steps:
# Checkout the Repo
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
# Setup Repo
- name: Setup Repo
uses: ./.github/actions/setup-repo
with:
node-version: ${{ inputs.node-version }}
os: ${{ inputs.os }}
# Build
- name: Build ${{ inputs.package-name }}
uses: ./.github/actions/run-script
with:
script-name: "${{inputs.build-script }} --filter=${{ inputs.package-name }}"
# Create Github Deployment
- name: Create GitHub deployment
if: ${{ github.event_name == 'pull_request' }}
uses: chrnorm/deployment-action@55729fcebec3d284f60f5bcabbd8376437d696b1 # v2.0.7
id: deploy
with:
token: ${{ secrets.GITHUB_TOKEN }}
environment: "${{ inputs.sub-domain-suffix }}-pr-${{ github.event.number }}"
# Zip dist folder
- name: Zip build output
shell: bash
run: |
cd ${{ inputs.package-dist-directory }}
zip -r ./${{ env.ZIP_NAME }} .
# Upload zip to S3
- name: Upload to S3
id: upload-s3
uses: hkusu/s3-upload-action@df0d0d688ce4593c477be764d08f63566dfd968e # v2.1.0
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
aws-bucket: ${{ env.BUCKET_NAME }}
bucket-root: "/"
destination-dir: "${{ inputs.destination-dir }}"
file-path: "${{inputs.package-dist-directory}}/${{ env.ZIP_NAME }}"
content-type: "application/zip"
public: true
output-file-url: 'true'
# Create branch on Amplify
- name: Create Amplify branch
shell: bash
# We return true to prevent the step from failing if the branch already exists
run: |
aws amplify create-branch \
--app-id ${{ env.AMPLIFY_ID }} \
--branch-name ${{ env.BRANCH_NAME }} \
|| true
# Deploy Amplify from S3
- name: Start Amplify Deployment from S3
shell: bash
run: |
aws amplify start-deployment \
--app-id ${{ env.AMPLIFY_ID }} \
--branch-name ${{ env.BRANCH_NAME }} \
--source-url ${{ steps.upload-s3.outputs.file-url }}
# Set env vars for domain name association
- name: Set environment variables for domain name association
if: github.event_name == 'pull_request'
env:
SUB_DOMAIN_SUFFIX: ${{ inputs.sub-domain-suffix }}
run: |
echo "SUB_DOMAIN=pr${{ github.event.number }}-$SUB_DOMAIN_SUFFIX" >> $GITHUB_ENV
- name: Get existing domain associations
if: github.event_name == 'pull_request'
run: |
DOMAIN_ASSOCIATIONS=$(aws amplify get-domain-association \
--app-id ${{ env.AMPLIFY_ID }} \
--domain-name pie.design \
--region ${{ env.AWS_REGION }} \
| jq '
[.domainAssociation.subDomains[].subDomainSetting | { prefix: (.prefix // ""), branchName }] |
. |= (map(.branchName) | index("pr${{ github.event.number }}")) as $ix |
if $ix == null then
. + [{branchName: "pr${{ github.event.number }}", prefix: "${{ env.SUB_DOMAIN }}"}]
else . end
' -c)
echo "DOMAIN_ASSOCIATIONS=$DOMAIN_ASSOCIATIONS" >> $GITHUB_ENV
- name: Create Subdomain Association
if: github.event_name == 'pull_request'
shell: bash
run: |
aws amplify update-domain-association \
--app-id ${{ env.AMPLIFY_ID }} \
--domain-name pie.design \
--sub-domain-settings '${{ env.DOMAIN_ASSOCIATIONS }}'
- name: Wait for Amplify deployment
if: github.event_name == 'pull_request'
run: |
LATEST_JOB_ID=$(aws amplify list-jobs \
--app-id ${{ env.AMPLIFY_ID }} \
--branch-name ${{ env.BRANCH_NAME }} \
--region ${{ env.AWS_REGION }} --query jobSummaries[0].jobId \
--output text)
MAX_ATTEMPTS=12
SLEEP_SECONDS=10
attempt_counter=0
status=""
while [ $attempt_counter -lt $MAX_ATTEMPTS ]; do
status=$(aws amplify get-job \
--app-id ${{ env.AMPLIFY_ID }} \
--branch-name ${{ env.BRANCH_NAME }} \
--job-id $LATEST_JOB_ID \
--query job.summary.status \
--output text)
echo "Attempt $(( attempt_counter+1 )) of $MAX_ATTEMPTS: Status - $status"
if [ "$status" == "SUCCEED" ]; then
echo "Amplify deployment succeeded."
break
fi
if [ "$status" == "FAILED" ]; then
echo "Amplify deployment failed."
exit 1
fi
let "attempt_counter+=1"
sleep $SLEEP_SECONDS
done
if [ "$status" != "SUCCEED" ]; then
echo "Amplify deployment did not succeed within the expected time."
exit 1
fi
# If successful
- name: Update deployment status (success)
if: ${{ github.event_name == 'pull_request' && success() }}
uses: chrnorm/deployment-status@9a72af4586197112e0491ea843682b5dc280d806 # v2.0.3
with:
token: ${{ secrets.GITHUB_TOKEN }}
environment-url: https://${{ env.SUB_DOMAIN }}.pie.design/
deployment-id: ${{ steps.deploy.outputs.deployment_id }}
state: "success"
# If it failed
- name: Update deployment status (failure)
if: ${{ github.event_name == 'pull_request' && failure() }}
uses: chrnorm/deployment-status@9a72af4586197112e0491ea843682b5dc280d806 # v2.0.3
with:
token: ${{ secrets.GITHUB_TOKEN }}
environment-url: https://${{ env.SUB_DOMAIN }}.pie.design/
deployment-id: ${{ steps.deploy.outputs.deployment_id }}
state: "failure"