This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
341 lines (298 loc) · 11.8 KB
/
ch8-canary-poi.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
name: POI CICD - Ch8-CanaryDeployment
# Variables used in the jobs
env:
registry: openhackflo1xyl7acr.azurecr.io
repository: devopsoh/api-poi
docker_path: apis/poi/web
webapp_name: openhackflo1xyl7poi
poi_path: apis/poi/**
staging_url: https://openhackflo1xyl7poi-staging.azurewebsites.net
prod_url: https://openhackflo1xyl7poi.azurewebsites.net
resource_group: openhackflo1xyl7rg
build_name: POI
# If you want to trigger this branch then change the branch name to a usable branch name
on:
push:
branches: [ openhack_refresh ]
paths:
- apis/poi/**
- .github/workflows/ch8-canary-poi.yml
pull_request:
branches: [ openhack_refresh ]
paths:
- apis/poi/**
- .github/workflows/ch8-canary-poi.yml
jobs:
# CI Phase - BUILD AND TEST CODE CHANGES
build_and_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
# If you want to specify a specific DOTNET version you may uncomment this line. It is not needed.
#with:
# dotnet-version: '3.1.202'
- name: Add Code Coverage Tools
working-directory: apis/poi/tests/UnitTests
run: dotnet add package coverlet.msbuild
- name: Install dependencies
run: dotnet restore apis/poi/web/*.csproj
- name: .NET Build Web
run: dotnet build --configuration Release --no-restore apis/poi/web/*.csproj
- name: .NET Build Unit Tests
run: dotnet build --configuration Release apis/poi/tests/UnitTests/UnitTests.csproj
- name: .NET Test
run: dotnet test --no-restore --verbosity normal apis/poi/tests/UnitTests /p:CollectCoverage=true /p:CoverletOutput=lcov/ /p:CoverletOutputFormat=lcov
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: apis/poi/tests/UnitTests/lcov/coverage.info
base-path: apis/poi
flag-name: Unit
- name: Create Issue
uses: actions/github-script@0.9.0
if: failure()
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let body = "${{ env.build_name }} Worflow Failure \n Build Number: ${{ github.run_number }} \n Build Log: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} \n SHA: [${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }}) \n";
github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "${{ env.build_name }} Workflow ${{ github.run_number }} Failed! ",
body: body
});
# CD Phase 2 - 1 - PUSH TO ACR
docker_build_and_push:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: build_and_test
steps:
- uses: actions/checkout@v2
- name: Docker login
run: docker login $registry -u ${{ secrets.ACR_USER }} -p ${{ secrets.ACR_KEY }}
- name: Docker build
run: docker build -t $registry/$repository:${{github.run_number}} --build-arg build_version=${{github.run_number}} $docker_path
- name: Docker Push
run: docker push $registry/$repository:${{github.run_number}}
- name: Create Issue
uses: actions/github-script@0.9.0
if: failure()
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let body = "${{ env.build_name }} Worflow Failure \n Build Number: ${{ github.run_number }} \n Build Log: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} \n SHA: [${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }}) \n";
github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "${{ env.build_name }} Workflow ${{ github.run_number }} Failed! ",
body: body
});
# CD Phase 2 - 2 - PUSH NEW CODE FROM ACR TO WEBAPP STAGING SLOT
deploy_staging:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: docker_build_and_push
steps:
- name: Azure authentication
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Azure WebApp
uses: Azure/webapps-deploy@v2
with:
app-name: ${{ env.webapp_name }}
slot-name: staging
images: ${{ env.registry }}/${{ env.repository }}:${{github.run_number}}
- name: Create Issue
uses: actions/github-script@0.9.0
if: failure()
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let body = "${{ env.build_name }} Worflow Failure \n Build Number: ${{ github.run_number }} \n Build Log: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} \n SHA: [${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }}) \n";
github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "${{ env.build_name }} Workflow ${{ github.run_number }} Failed! ",
body: body
});
# CD Phase 3 - 1 - VALIDATE WEBAPP STAGING SLOT HTTP STATUS
validate_staging:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: deploy_staging
steps:
- uses: actions/checkout@v2
- name: Run Validation script
run: |
pwd
ls -la
chmod +x poihttp.sh
./poihttp.sh
- name: Create Issue
uses: actions/github-script@0.9.0
if: failure()
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let body = "${{ env.build_name }} Worflow Failure \n Build Number: ${{ github.run_number }} \n Build Log: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} \n SHA: [${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }}) \n";
github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "${{ env.build_name }} Workflow ${{ github.run_number }} Failed! ",
body: body
});
# CD Phase 8-1 - PHASED ROLLOUT 10%
route_traffic_ten_percent:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: validate_staging
steps:
- name: Azure authentication
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Route 10% only to Staging Slot
run: |
az webapp traffic-routing set --distribution staging=10 --name ${{ env.webapp_name }} --resource-group $resource_group
validate_ten_percent:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: route_traffic_ten_percent
steps:
- name: Install Apache Bench
run: |
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install apache2-utils
- name: Load Test
run: |
failures=$(ab -t 10 -c 10 ${{ env.prod_url }}/api/version/poi | grep -Poe 'Failed requests: \K.*' | sed 's/^ *//g')
if [ "$failures" -gt "0" ];then
exit -1
fi
# CD Phase 8-2 - PHASED ROLLOUT 100%
route_traffic_onehundred_percent:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: validate_ten_percent
steps:
- name: Azure authentication
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Route 100% to Staging Slot
run: |
az webapp traffic-routing set --distribution staging=100 --name ${{ env.webapp_name }} --resource-group $resource_group
validate_onhundred_percent:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: route_traffic_onehundred_percent
steps:
- name: Install Apache Bench
run: |
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install apache2-utils
- name: Load Test
run: |
failures=$(ab -t 10 -c 10 ${{ env.prod_url }}/api/version/poi | grep -Poe 'Failed requests: \K.*' | sed 's/^ *//g')
if [ "$failures" -gt "0" ];then
exit -1
fi
# CD Phase 8-3 - CLEAR ROUTING RULES AND SEND ALL TRAFFIC TO PRODUCTION AGAIN
clear_route_traffic:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: validate_onhundred_percent
steps:
- name: Azure authentication
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Clear routing rules
run: |
az webapp traffic-routing clear --name ${{ env.webapp_name }} --resource-group $resource_group
# CD Phase 7 - LOAD TESTING WITH K6
k6_cloud_test:
if: github.ref == 'refs/heads/openhack_refresh'
name: k6 cloud test run
runs-on: ubuntu-latest
needs: clear_route_traffic
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Run k6 cloud test
uses: k6io/action@v0.1
with:
filename: test-poi.js
cloud: true
token: ${{ secrets.K6_CLOUD_API_TOKEN }}
# CD Phase 3 - 2 - SWAP SLOTS FROM STAGING TO PRODUCTION
swap_slots:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: k6_cloud_test
steps:
- name: Azure authentication
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Swap staging to production
run: |
az webapp deployment slot swap --name $webapp_name --resource-group $resource_group --slot staging --target-slot production
- name: Create Issue
uses: actions/github-script@0.9.0
if: failure()
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let body = "${{ env.build_name }} Worflow Failure \n Build Number: ${{ github.run_number }} \n Build Log: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} \n SHA: [${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }}) \n";
github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "${{ env.build_name }} Workflow ${{ github.run_number }} Failed! ",
body: body
});
# CD Phase 3 - 3 - VALIDATE WEBAPP PRODUCTION SLOT HTTP STATUS
validate_production:
if: github.ref == 'refs/heads/openhack_refresh'
runs-on: ubuntu-latest
needs: swap_slots
steps:
- uses: actions/checkout@v2
- name: Run Validation script
run: |
pwd
ls -la
chmod +x prod-poi-http.sh
./prod-poi-http.sh
- name: Create Issue
uses: actions/github-script@0.9.0
if: failure()
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let body = "${{ env.build_name }} Worflow Failure \n Build Number: ${{ github.run_number }} \n Build Log: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} \n SHA: [${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }}) \n";
github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "${{ env.build_name }} Workflow ${{ github.run_number }} Failed! ",
body: body
});
# CD Phase 4 - 1 - ROLLBACK IN CASE THE PRODUCTION SLOT FAILS VALIDATION
roll_back:
if: failure()
runs-on: ubuntu-latest
needs: validate_production
steps:
- name: Azure authentication
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Swap production and staging
run: |
az webapp deployment slot swap --name $webapp_name --resource-group $resource_group --slot staging --target-slot production