generated from sikleber/aws-cdk-react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
194 lines (156 loc) · 5.69 KB
/
cd.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
name: CD
on:
push:
branches: [ "main" ]
workflow_dispatch:
jobs:
test-backend:
runs-on: ubuntu-latest
defaults:
run:
working-directory: backend
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: backend/package-lock.json
- name: Install
run: npm install
- name: Lint
run: npm run lint
- name: Format Check
run: npm run format:check
- name: Test
run: npm run test
- name: Build
run: npm run build
- name: Upload Backend node_modules
uses: actions/upload-artifact@v4
with:
name: backend-node-modules
path: backend/node_modules
deploy-infrastructure:
runs-on: ubuntu-latest
needs: [ test-backend ]
defaults:
run:
working-directory: infrastructure
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: infrastructure/package-lock.json
- name: Install AWS CLI
run: npm install -g aws-cdk
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: eu-central-1
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Download Backend node_modules
uses: actions/download-artifact@v4
with:
name: backend-node-modules
path: backend/node_modules
- name: Install
run: npm install
- name: Lint
run: npm run lint
- name: Format check
run: npm run format:check
- name: Test
run: npm run test
- name: Build
run: npm run build
- name: CDK Deploy
run: |
cdk deploy --all -vv --require-approval never -c config=dev -O cdk-outputs.json
echo "CDK Outputs: $(cat cdk-outputs.json)"
- name: Upload CDK Outputs
uses: actions/upload-artifact@v4
with:
name: cdk-outputs
path: infrastructure/cdk-outputs.json
retention-days: 1
if-no-files-found: error
- name: Write CDK Outputs to Summary
run: |
echo "## CDK Deployment Success! :rocket:" >> $GITHUB_STEP_SUMMARY
echo "### Outputs Table" >> $GITHUB_STEP_SUMMARY
echo "| Stack | Key | Value |" >> $GITHUB_STEP_SUMMARY
echo "| --- | --- | --- |" >> $GITHUB_STEP_SUMMARY
for stack in $(jq -r 'keys[]' cdk-outputs.json); do
for key in $(jq -r ".${stack} | keys[]" cdk-outputs.json); do
value=$(jq -r ".${stack}.${key}" cdk-outputs.json)
echo "| ${stack} | ${key} | ${value} |" >> $GITHUB_STEP_SUMMARY
done
done
echo "" >> $GITHUB_STEP_SUMMARY
build-and-deploy-frontend:
runs-on: ubuntu-latest
needs: [ deploy-infrastructure ]
defaults:
run:
working-directory: frontend
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install
run: npm install
- name: Lint
run: npm run lint
- name: Format Check
run: npm run format:check
- name: Test
run: npm run test
- name: Download CDK Outputs
uses: actions/download-artifact@v4
with:
name: cdk-outputs
path: frontend
- name: Write .env.build File
run: |
echo "COGNITO_USER_POOL_ID=$(jq -r '.AuthStack.CognitoUserPoolId' cdk-outputs.json)" > .env.build
echo "COGNITO_USER_POOL_CLIENT_ID=$(jq -r '.AuthStack.CognitoUserPoolClientId' cdk-outputs.json)" >> .env.build
echo "COGNITO_REGION=$(jq -r '.AuthStack.CognitoRegion' cdk-outputs.json)" >> .env.build
echo "GRAPHQL_API_ENDPOINT=$(jq -r '.BackendStack.AppGraphqlApiUrl' cdk-outputs.json)" >> .env.build
echo "GRAPHQL_API_REGION=$(jq -r '.BackendStack.AppGraphqlApiRegion' cdk-outputs.json)" >> .env.build
echo "REST_API_ENDPOINT=$(jq -r '.BackendStack.AppRestApiUrl' cdk-outputs.json)" >> .env.build
- name: Build
run: npm run build
- name: Upload Frontend Build Artifact
uses: actions/upload-artifact@v4
with:
name: frontend-build
path: frontend/dist
retention-days: 1
if-no-files-found: error
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: eu-central-1
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Deploy to S3
run: |
FRONTEND_BUCKET_NAME=$(jq -r '.ReactAppStack.ReactAppDeploymentBucketName' cdk-outputs.json)
CLOUDFRONT_DISTRIBUTION_ID=$(jq -r '.ReactAppStack.ReactAppDistributionId' cdk-outputs.json)
aws s3 sync dist s3://$FRONTEND_BUCKET_NAME --delete
aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_DISTRIBUTION_ID --paths "/*"
- name: Write Frontend Deployment Success to Summary
run: |
CLOUDFRONT_DOMAIN=$(jq -r '.ReactAppStack.ReactAppDistributionDomainName' cdk-outputs.json)
echo "## Frontend Deployment Success! :sparkles:" >> $GITHUB_STEP_SUMMARY
echo "### APP: https://$CLOUDFRONT_DOMAIN" >> $GITHUB_STEP_SUMMARY