-
Notifications
You must be signed in to change notification settings - Fork 0
245 lines (204 loc) · 7.13 KB
/
cicd.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
name: CICD
on:
push:
branches: [main]
permissions:
contents: read
id-token: write
env:
AZURE_RESOURCE_GROUP: "gh-personal-portfolio"
AZURE_STORAGE_ACCOUNT: "ghpersonalportfolio"
LOCATION: "West Europe"
RECAPTCHA_SITE_KEY: ${{ secrets.RECAPTCHA_SITE_KEY }}
RECAPTCHA_SECRET_KEY: ${{ secrets.RECAPTCHA_SECRET_KEY }}
SMTP_USER: ${{ secrets.SMTP_USER }}
SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }}
SMTP_RECEIVER: ${{ secrets.SMTP_RECEIVER }}
jobs:
setup-azure-resource:
name: Setup Azure Resource
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Create Resource Group
run: |
az group create \
--name ${{ env.AZURE_RESOURCE_GROUP }} \
--location "${{ env.LOCATION }}"
- name: Create Azure Storage Account
run: |
az storage account create \
--resource-group ${{ env.AZURE_RESOURCE_GROUP }} \
--name ${{ env.AZURE_STORAGE_ACCOUNT }} \
--sku Standard_LRS
- name: Grant Blob Data Contributor permissions to the Service Principal
run: |
az role assignment create \
--assignee ${{ secrets.AZURE_CLIENT_ID }} \
--role "Storage Blob Data Contributor" \
--scope /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/resourceGroups/${{ env.AZURE_RESOURCE_GROUP }}/providers/Microsoft.Storage/storageAccounts/${{ env.AZURE_STORAGE_ACCOUNT }}
- name: Create Azure Storage Container for the current commit
run: |
az storage container create \
--account-name ${{ env.AZURE_STORAGE_ACCOUNT }} \
--name ${{ github.sha }} \
--public-access blob \
--auth-mode login
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Install dependencies
run: npm install
- name: Build application
run: npm run build
- name: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: nextjs-artifacts
path: .next/
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Install dependencies
run: npm install
- name: Run linter
run: npm run lint
unit-tests:
name: Unit Tests
needs: [setup-azure-resource]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test:ci
- name: Generate Job summary Tests coverage
run: |
COVERAGE_SUMMARY_PATH="./coverage/coverage-summary.json"
LINES_PCT=$(jq -r '.total.lines.pct' "$COVERAGE_SUMMARY_PATH")
STATEMENTS_PCT=$(jq -r '.total.statements.pct' "$COVERAGE_SUMMARY_PATH")
FUNCTIONS_PCT=$(jq -r '.total.functions.pct' "$COVERAGE_SUMMARY_PATH")
BRANCHES_PCT=$(jq -r '.total.branches.pct' "$COVERAGE_SUMMARY_PATH")
cat <<- EOM >> $GITHUB_STEP_SUMMARY
#### Coverage Summary:
| Metric | Coverage |
|------------|----------|
| Lines | $LINES_PCT% |
| Statements | $STATEMENTS_PCT% |
| Functions | $FUNCTIONS_PCT% |
| Branches | $BRANCHES_PCT% |
[View Tests Coverage report](https://${{ env.AZURE_STORAGE_ACCOUNT }}.blob.core.windows.net/${{ github.sha }}/coverage/lcov-report/index.html)
EOM
- name: Log in to Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Upload current commit Jest coverage
run: |
az storage blob upload-batch \
--account-name ${{ env.AZURE_STORAGE_ACCOUNT }} \
--destination ${{ github.sha }} \
--destination-path coverage \
--source ./coverage \
--overwrite \
--auth-mode login
e2e-tests:
name: E2E Tests
needs: [setup-azure-resource, build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: nextjs-artifacts
path: .next/
- name: Install dependencies
run: npm install
- name: Install Browsers
run: npx playwright install --with-deps
- name: Run E2E tests
id: e2e_tests
run: |
echo "status=failed" >> $GITHUB_OUTPUT
xvfb-run npm run test:e2e
echo "status=success" >> $GITHUB_OUTPUT
continue-on-error: true
- name: Log in to Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Upload current commit E2E tests reports
run: |
az storage blob upload-batch \
--account-name ${{ env.AZURE_STORAGE_ACCOUNT }} \
--destination ${{ github.sha }} \
--destination-path playwright-report \
--source ./playwright-report \
--overwrite \
--auth-mode login
- name: Generate Job summary with E2E reports link
run: |
cat <<- EOM >> $GITHUB_STEP_SUMMARY
[View E2E Tests report](https://${{ env.AZURE_STORAGE_ACCOUNT }}.blob.core.windows.net/${{ github.sha }}/playwright-report/index.html)
EOM
- name: Check E2E test result
run: |
if [ "${{ steps.e2e_tests.outputs.status }}" == "failed" ]; then
exit 1
fi
lighthouse:
name: Lighthouse
needs: [build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2
- uses: actions/setup-node@v3
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: nextjs-artifacts
path: .next/
- name: Install dependencies
run: npm install
- name: Run Lighthouse CI
run: npm run lhci
deploy:
name: Deploy
needs: [build, lint, unit-tests, e2e-tests, lighthouse]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: nextjs-artifacts
path: .next/
- name: Deploy build artifacts to Vercel
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
run: npx vercel deploy --prod --token=${{ secrets.VERCEL_TOKEN }}