-
Notifications
You must be signed in to change notification settings - Fork 0
507 lines (435 loc) · 15.8 KB
/
build-and-test.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# Copyright (c) Omar Boukli-Hacene. All rights reserved.
# Distributed under an MIT-style license that can be
# found in the LICENSE file.
# SPDX-License-Identifier: MIT
name: Build and test
on:
pull_request:
branches:
- main
push:
branches:
- main
workflow_dispatch:
permissions: {}
env:
INTEGRATION_TEST_RESULTS_DIR_NAME: integration_test_results
UNIT_TEST_RESULTS_DIR_NAME: unit_test_results
jobs:
build:
name: Solution build
runs-on: ubuntu-latest
env:
DOTNET_CLI_TELEMETRY_OPTOUT: true
steps:
- name: Check out repository
uses: actions/checkout@v3
with:
persist-credentials: false
- name: Build release
uses: ./.github/workflows/composite/dotnet-build
with:
configuration: Release
integration_test:
name: Integration test
runs-on: ubuntu-latest
env:
AKTABOOK_ENVIRONMENT: Test
AKTABOOK_INTEGRATION_TEST_ENVIRONMENT: Test
BUS_HOST_PID_FILE: src/Aktabook.Bus/bin/Release/net6.0/aktabook-bus.pid
DOTNET_CLI_TELEMETRY_OPTOUT: true
RequesterServiceBus__RabbitMQConnectionOptions__HostName: localhost
RequesterServiceBus__RabbitMQConnectionOptions__Password: ${{ secrets.RABBITMQ_INTEGRATION_TEST_PASSWORD }}
RequesterServiceBus__RabbitMQConnectionOptions__PortNumber: 5672
RequesterServiceBus__RabbitMQConnectionOptions__UserName: ${{ secrets.RABBITMQ_INTEGRATION_TEST_USERNAME }}
RequesterServiceBus__RabbitMQConnectionOptions__VirtualHost: aktabook_vhost
RequesterServiceDbContext__SqlServerConfig__DataSource: 127.0.0.1
RequesterServiceDbContext__SqlServerConfig__TrustServerCertificate: true
RequesterServiceDbContext__SqlServerConfig__InitialCatalog: aktabook_common
RequesterServiceDbContext__SqlServerConfig__Password: ${{ secrets.SQLSERVER_INTEGRATION_TEST_SA_PASSWORD }}
RequesterServiceDbContext__SqlServerConfig__UserID: sa
RequesterServiceDbContext__SqlServerConfig__IntegratedSecurity: true
SQLSERVER_CLIENT_NUGET_VERSION: "5.1.0"
outputs:
artifact_name: ${{ steps.get_artifact_name.outputs.artifact_name }}
services:
sql_server:
image: mcr.microsoft.com/mssql/server:2022-latest
env:
ACCEPT_EULA: Y
SA_PASSWORD: ${{ secrets.SQLSERVER_INTEGRATION_TEST_SA_PASSWORD }}
SQLCMDUSER: sa
SQLCMDPASSWORD: ${{ secrets.SQLSERVER_INTEGRATION_TEST_SA_PASSWORD }}
ports:
- 1433:1433
options: >-
--health-cmd "
/opt/mssql-tools/bin/sqlcmd
-h -1
-Q 'SET NOCOUNT ON; SELECT 1'
-S 127.0.0.1
-W
> /dev/null
"
--health-interval 7s
--health-retries 5
--health-timeout 3s
rabbitmq:
image: rabbitmq:management
env:
RABBITMQ_DEFAULT_PASS: ${{ secrets.RABBITMQ_INTEGRATION_TEST_PASSWORD }}
RABBITMQ_DEFAULT_USER: ${{ secrets.RABBITMQ_INTEGRATION_TEST_USERNAME }}
RABBITMQ_DEFAULT_VHOST: aktabook_vhost
ports:
- 5672:5672
- 15672:15672
options: >-
--health-cmd "rabbitmq-diagnostics ping"
--health-interval 7s
--health-retries 5
--health-timeout 3s
--hostname aktabook-queue
steps:
- name: Mask escaped values
run: >-
printf "::add-mask::%q\n"
$RequesterServiceDbContext__SqlServerConfig__Password
echo "::add-mask::$(jq
--raw-input
--raw-output
@uri <<<"$RequesterServiceBus__RabbitMQConnectionOptions__Password")"
- name: Check out repository
uses: actions/checkout@v3
with:
persist-credentials: false
- name: Build release
uses: ./.github/workflows/composite/dotnet-build
with:
configuration: Release
- name: Restore .NET tools
run: dotnet tool restore
- name: Create RabbitMQ virtual host
env:
BUS_USERNAME: ${{ secrets.RABBITMQ_INTEGRATION_TEST_USERNAME }}
BUS_PASSWORD: ${{ secrets.RABBITMQ_INTEGRATION_TEST_PASSWORD }}
run: >-
curl
--data-urlencode
--request PUT
--silent
--netrc-file <(cat <<< "login $BUS_USERNAME password $BUS_PASSWORD")
http://localhost:15672/api/vhosts/$RequesterServiceBus__RabbitMQConnectionOptions__VirtualHost
- name: Create AMQP connection URL
run: >-
echo "RABBITMQ_CONNECTION_STRING="$(
dotnet script eval --
$RequesterServiceBus__RabbitMQConnectionOptions__HostName
$RequesterServiceBus__RabbitMQConnectionOptions__PortNumber
$RequesterServiceBus__RabbitMQConnectionOptions__VirtualHost
$RequesterServiceBus__RabbitMQConnectionOptions__UserName
$RequesterServiceBus__RabbitMQConnectionOptions__Password
<<<
'Console.WriteLine((new UriBuilder("amqp",
Args[0],
int.Parse(Args[1]),
Uri.EscapeDataString(Args[2]))
{ UserName = Uri.EscapeDataString(Args[3]),
Password = Uri.EscapeDataString(Args[4])
}).Uri.ToString());'
)""
>> $GITHUB_ENV
- name: Create NServiceBus delays
env:
BUS_USERNAME: ${{ secrets.RABBITMQ_INTEGRATION_TEST_USERNAME }}
BUS_PASSWORD: ${{ secrets.RABBITMQ_INTEGRATION_TEST_PASSWORD }}
run: >-
dotnet rabbitmq-transport
delays create
--connectionStringEnv RABBITMQ_CONNECTION_STRING
dotnet rabbitmq-transport
delays verify
--url http://localhost:15672
--username $BUS_USERNAME
--password $BUS_PASSWORD
- name: Create NServiceBus BookInfoRequestEndpoint
run: >-
dotnet rabbitmq-transport
endpoint create BookInfoRequestEndpoint
--auditQueueName AuditQueue
--connectionStringEnv RABBITMQ_CONNECTION_STRING
--errorQueueName ErrorQueue
--queueType Quorum
--routingTopology Conventional
--useDurableEntities
- name: Start code coverage collection server
run: >-
dotnet coverage collect
--background
--session-id coverage_session
--server-mode
--output ${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}/coverage_session/coverage.coverage
--output-format coverage
- name: Create SQL Server connection string
run: >-
echo "SQLSERVER_CONNECTION_STRING="$(
dotnet script eval --
$RequesterServiceDbContext__SqlServerConfig__DataSource
$RequesterServiceDbContext__SqlServerConfig__TrustServerCertificate
$RequesterServiceDbContext__SqlServerConfig__UserID
$RequesterServiceDbContext__SqlServerConfig__Password
$RequesterServiceDbContext__SqlServerConfig__InitialCatalog
$RequesterServiceDbContext__SqlServerConfig__IntegratedSecurity
<<<
'#r "nuget: Microsoft.Data.SqlClient, ${{ env.SQLSERVER_CLIENT_NUGET_VERSION }}"
using Microsoft.Data.SqlClient;
Console.WriteLine((new SqlConnectionStringBuilder
{
DataSource = Args[0],
TrustServerCertificate = bool.Parse(Args[1]),
UserID = Args[2],
Password = Args[3],
InitialCatalog = Args[4],
IntegratedSecurity = bool.Parse(Args[5]),
}).ConnectionString);'
)""
>> $GITHUB_ENV
- name: Create database objects
run: >-
dotnet coverage connect
coverage_session
dotnet ef database update
--configuration Release
--connection "$SQLSERVER_CONNECTION_STRING"
--no-build
--project src/Aktabook.Data.Migrations
- name: Run bus endpoint host
run: >-
dotnet coverage connect
--background
coverage_session
./Aktabook.Bus
working-directory: src/Aktabook.Bus/bin/Release/net6.0/
- name: Wait for bus readiness check
env:
PORT: 23514
run: >-
x=1;
while [[ $x -le 30 && ! -f "$BUS_HOST_PID_FILE" ]];
do sleep $(( x++ ));
done
nc -4 -d -i 1 -n -v -w 10 -z
127.0.0.1 ${{ env.PORT }}
- name: Wait for bus liveness first check
env:
PORT: 23515
run: >-
nc -4 -d -i 1 -n -v -w 10 -z
127.0.0.1 ${{ env.PORT }}
- name: Run integration tests with ephemeral database
env:
RequesterServiceDbContext__SqlServerConfig__InitialCatalog: aktabook_ephemeral
run: >-
dotnet test
--collect 'Code Coverage'
--configuration Release
--filter 'FullyQualifiedName~IntegrationTest&Category=Ephemeral'
--no-build
--no-restore
--results-directory ${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}
- name: Run integration tests with long-lived database
run: >-
dotnet test
--collect 'Code Coverage'
--configuration Release
--filter 'FullyQualifiedName~IntegrationTest&Category!=Ephemeral'
--no-build
--no-restore
--results-directory ${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}
- name: Wait for bus liveness last check
env:
PORT: 23515
run: >-
nc -4 -d -i 1 -n -v -w 10 -z
127.0.0.1 ${{ env.PORT }}
- name: Stop bus endpoint host
run: >-
pkill
--pidfile $BUS_HOST_PID_FILE
--signal SIGTERM
- name: Stop code coverage collection server
run: >-
dotnet coverage
shutdown
coverage_session
- name: Create merged test coverage report directory
run: mkdir --parents ${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}/merged/
- name: Merge test coverage reports
run: >-
dotnet coverage merge
--remove-input-files
--recursive
--output ${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}/merged/merged.xml
--output-format xml
${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}/*/*.*
- name: Archive test coverage results
run: >-
7z a -bb0 -bd -m0=lzma2 -mx=3
${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}.7z
${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}/merged/
- name: Get artifact name
id: get_artifact_name
run: >-
echo "artifact_name="${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}-$(git rev-parse --short "$GITHUB_SHA")""
>> $GITHUB_OUTPUT
- name: Upload test results archive
uses: actions/upload-artifact@v3
with:
if-no-files-found: error
name: ${{ steps.get_artifact_name.outputs.artifact_name }}
path: ${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}.7z
- name: Upload bus logs
if: ${{ always() }}
uses: actions/upload-artifact@v3
with:
if-no-files-found: error
name: bus-logs
path: src/Aktabook.Bus/bin/Release/net6.0/Logs
- name: Upload public API logs
if: ${{ always() }}
uses: actions/upload-artifact@v3
with:
if-no-files-found: error
name: public-api-logs
path: src/Aktabook.Bus/bin/Release/net6.0/Logs
unit_test:
name: Unit test
runs-on: ubuntu-latest
outputs:
artifact_name: ${{ steps.get_artifact_name.outputs.artifact_name }}
steps:
- name: Check out repository
uses: actions/checkout@v3
with:
persist-credentials: false
- name: Build release
uses: ./.github/workflows/composite/dotnet-build
with:
configuration: Release
- name: Restore .NET tools
run: dotnet tool restore
- name: Run unit tests
run: >-
dotnet test
--collect 'Code Coverage'
--configuration Release
--filter 'FullyQualifiedName~UnitTest'
--no-build
--no-restore
--results-directory ${{ env.UNIT_TEST_RESULTS_DIR_NAME }}
- name: Create merged test coverage report directory
run: mkdir --parents ${{ env.UNIT_TEST_RESULTS_DIR_NAME }}/merged/
- name: Merge test coverage reports
run: >-
dotnet coverage merge
--remove-input-files
--recursive
--output ${{ env.UNIT_TEST_RESULTS_DIR_NAME }}/merged/merged.xml
--output-format xml
${{ env.UNIT_TEST_RESULTS_DIR_NAME }}/*/*.*
- name: Archive test coverage results
run: >-
7z a -bb0 -bd -m0=lzma2 -mx=3
${{ env.UNIT_TEST_RESULTS_DIR_NAME }}.7z
${{ env.UNIT_TEST_RESULTS_DIR_NAME }}/merged/
- name: Get artifact name
id: get_artifact_name
run: >-
echo "artifact_name="${{ env.UNIT_TEST_RESULTS_DIR_NAME }}-$(git rev-parse --short "$GITHUB_SHA")""
>> $GITHUB_OUTPUT
- name: Upload test results archive
uses: actions/upload-artifact@v3
with:
if-no-files-found: error
name: ${{ steps.get_artifact_name.outputs.artifact_name }}
path: ${{ env.UNIT_TEST_RESULTS_DIR_NAME }}.7z
quality_gate:
name: Quality gate
runs-on: ubuntu-latest
needs:
- unit_test
- integration_test
env:
DOTNET_CLI_TELEMETRY_OPTOUT: true
steps:
- name: Check out repository
uses: actions/checkout@v3
with:
fetch-depth: 0
persist-credentials: false
- name: Download integration test results
uses: actions/download-artifact@v3
with:
name: ${{ needs.integration_test.outputs.artifact_name }}
- name: Download unit test results
uses: actions/download-artifact@v3
with:
name: ${{ needs.unit_test.outputs.artifact_name }}
- name: Unarchive integration test results
run: >-
7z x
-o"${{ github.workspace }}"
${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}.7z
- name: Unarchive unit test results
run: >-
7z x
-o"${{ github.workspace }}"
${{ env.UNIT_TEST_RESULTS_DIR_NAME }}.7z
- name: Cache NuGet packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: >-
${{ runner.os }}-nuget-${{
hashFiles('**/packages.lock.json',
'.config/dotnet-tools.json')
}}
restore-keys: |
${{ runner.os }}-nuget-
- name: Set up .NET SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- name: Restore .NET tools
run: dotnet tool restore
- name: Merge test coverage reports
run: >-
dotnet coverage merge
--remove-input-files
--recursive
--output merged.xml
--output-format xml
${{ env.UNIT_TEST_RESULTS_DIR_NAME }}/merged.xml
${{ env.INTEGRATION_TEST_RESULTS_DIR_NAME }}/merged.xml
- name: Restores dependencies
env:
DOTNET_NUGET_SIGNATURE_VERIFICATION: true
run: dotnet restore --locked-mode
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
DOTNET_CLI_TELEMETRY_OPTOUT: true
run: >-
JAVA_HOME=$JAVA_HOME_17_X64
dotnet sonarscanner begin
/k:'oboukli_aktabook'
/o:'oboukli'
/d:'sonar.host.url=https://sonarcloud.io'
/d:'sonar.sourceEncoding=UTF-8'
/d:'sonar.login=${{ secrets.SONAR_TOKEN }}'
/d:'sonar.cs.vscoveragexml.reportsPaths=merged.xml'
dotnet build
--no-incremental
--no-restore
dotnet sonarscanner end
/d:'sonar.login=${{ secrets.SONAR_TOKEN }}'