Skip to content

Commit 0116279

Browse files
committed
Improved build and perftest scripts
* Do not use loops since we only have one project for main sink, one for tests and one for perftests. * Use try-finally blocks to pop back to caller's initial dir on errors. * Fail the build if there were nuget restore errors (e.g. due to known vulns). * build also sample projects on PR (skip them in release.yml and perftests.yml).
1 parent 23d1596 commit 0116279

File tree

4 files changed

+146
-59
lines changed

4 files changed

+146
-59
lines changed

.github/workflows/perftests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions/checkout@v4
1212

1313
- name: Run build
14-
run: ./Build.ps1 -SkipTests
14+
run: ./Build.ps1 -SkipTests -SkipSamples
1515
shell: pwsh
1616

1717
- name: Run performance tests

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
shell: pwsh
3333

3434
- name: Run build
35-
run: ./Build.ps1 -SkipTests
35+
run: ./Build.ps1 -SkipTests -SkipSamples
3636
shell: pwsh
3737

3838
- name: Run performance tests

Build.ps1

+117-43
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,135 @@
22
param (
33
[Parameter(Mandatory = $false)]
44
[Switch]
5-
$SkipTests
6-
)
7-
8-
echo "build: Build started"
9-
10-
Push-Location "$PSScriptRoot"
11-
12-
if (Test-Path .\artifacts) {
13-
echo "build: Cleaning .\artifacts"
14-
Remove-Item .\artifacts -Force -Recurse
15-
}
16-
17-
& dotnet restore --no-cache
5+
$SkipTests,
186

19-
$branch = @{ $true = $env:GITHUB_REF_NAME; $false = $(git symbolic-ref --short -q HEAD) }[$env:GITHUB_REF_NAME -ne $NULL]
20-
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:GITHUB_RUN_NUMBER, 10); $false = "local" }[$env:GITHUB_RUN_NUMBER -ne $NULL]
21-
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10, $branch.Length)))-$revision" }[$branch -ne "dev" -and $revision -ne "local"]
7+
[Parameter(Mandatory = $false)]
8+
[Switch]
9+
$SkipPerfTests,
2210

23-
echo "build: Version suffix is $suffix"
11+
[Parameter(Mandatory = $false)]
12+
[Switch]
13+
$SkipSamples
14+
)
2415

25-
foreach ($src in Get-ChildItem "$PSScriptRoot/src" -Directory) {
26-
Push-Location $src.FullName
16+
echo "build: Build started"
2717

28-
echo "build: Packaging project in $($src.FullName)"
18+
try
19+
{
20+
Push-Location "$PSScriptRoot"
2921

30-
if ($suffix) {
31-
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix
32-
} else {
33-
& dotnet pack -c Release -o ..\..\artifacts
22+
if (Test-Path .\artifacts)
23+
{
24+
echo "build: Cleaning .\artifacts"
25+
Remove-Item .\artifacts -Force -Recurse
3426
}
35-
if ($LASTEXITCODE -ne 0) { exit 1 }
3627

37-
Pop-Location
38-
}
39-
40-
if ($SkipTests -eq $false) {
41-
foreach ($test in Get-ChildItem "$PSScriptRoot/test" -Filter "*.Tests" -Directory) {
42-
Push-Location $test.FullName
43-
44-
echo "build: Testing project in $($test.FullName)"
45-
46-
& dotnet test -c Release --collect "XPlat Code Coverage"
47-
if ($LASTEXITCODE -ne 0) { exit 3 }
28+
echo "build: Restoring packages for solution"
29+
& dotnet restore --no-cache
30+
if ($LASTEXITCODE -ne 0)
31+
{
32+
echo "Error returned by dotnet restore. Aborting build."
33+
exit 1
34+
}
4835

36+
$branch = @{ $true = $env:GITHUB_REF_NAME; $false = $( git symbolic-ref --short -q HEAD ) }[$env:GITHUB_REF_NAME -ne $NULL]
37+
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:GITHUB_RUN_NUMBER, 10); $false = "local" }[$env:GITHUB_RUN_NUMBER -ne $NULL]
38+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0,[math]::Min(10, $branch.Length)) )-$revision" }[$branch -ne "dev" -and $revision -ne "local"]
39+
40+
echo "build: Version suffix is $suffix"
41+
42+
$sinkProjectPath = "$PSScriptRoot/src/Serilog.Sinks.MSSqlServer"
43+
try
44+
{
45+
Push-Location "$sinkProjectPath"
46+
47+
echo "build: Packaging sink main project in $sinkProjectPath"
48+
if ($suffix)
49+
{
50+
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix
51+
}
52+
else
53+
{
54+
& dotnet pack -c Release -o ..\..\artifacts
55+
}
56+
if ($LASTEXITCODE -ne 0)
57+
{
58+
echo "Error returned by dotnet pack. Aborting build."
59+
exit 1
60+
}
61+
}
62+
finally
63+
{
4964
Pop-Location
5065
}
5166

52-
# The performance benchmark tests should at least build without errors during PR validation
53-
$perfTestProjectPath = "$PSScriptRoot/test/Serilog.Sinks.MSSqlServer.PerformanceTests"
54-
Push-Location "$perfTestProjectPath"
67+
if ($SkipTests -eq $false)
68+
{
69+
$testProjectPath = "$PSScriptRoot/test/Serilog.Sinks.MSSqlServer.Tests"
70+
try
71+
{
72+
Push-Location "$testProjectPath"
73+
74+
echo "build: Testing project in $testProjectPath"
75+
& dotnet test -c Release --collect "XPlat Code Coverage"
76+
if ($LASTEXITCODE -ne 0)
77+
{
78+
exit 2
79+
}
80+
81+
}
82+
finally
83+
{
84+
Pop-Location
85+
}
86+
}
87+
88+
if ($SkipPerfTests -eq $false)
89+
{
90+
# The performance benchmark tests should at least build without errors during PR validation
91+
$perfTestProjectPath = "$PSScriptRoot/test/Serilog.Sinks.MSSqlServer.PerformanceTests"
92+
try
93+
{
94+
Push-Location "$perfTestProjectPath"
95+
96+
echo "build: Building performance test project in $perfTestProjectPath"
97+
& dotnet build -c Release
98+
if ($LASTEXITCODE -ne 0)
99+
{
100+
exit 3
101+
}
102+
}
103+
finally
104+
{
105+
Pop-Location
106+
}
107+
}
55108

56-
echo "build: Building performance test project in $perfTestProjectPath"
57-
& dotnet build -c Release
109+
if ($SkipSamples -eq $false)
110+
{
111+
foreach ($src in Get-ChildItem "$PSScriptRoot/samples/**/*.csproj" -File)
112+
{
113+
try
114+
{
115+
Push-Location $src.DirectoryName
116+
117+
echo "build: Building sample project $( $src.FullName )"
118+
& dotnet pack -c Release -o ..\..\artifacts
119+
if ($LASTEXITCODE -ne 0)
120+
{
121+
echo "Error returned by dotnet build. Aborting build."
122+
exit 4
123+
}
124+
}
125+
finally
126+
{
127+
Pop-Location
128+
}
129+
}
130+
}
58131

132+
}
133+
finally
134+
{
59135
Pop-Location
60136
}
61-
62-
Pop-Location

RunPerfTests.ps1

+27-14
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,37 @@ param (
77

88
echo "perf: Performance tests started with Filter = $Filter"
99

10-
Push-Location $PSScriptRoot
10+
try
11+
{
12+
Push-Location $PSScriptRoot
1113

12-
$artifactsPath = "$PSScriptRoot\artifacts\perftests"
14+
$artifactsPath = "$PSScriptRoot\artifacts\perftests"
1315

14-
if (Test-Path "$artifactsPath") {
15-
echo "perf: Cleaning $artifactsPath"
16-
Remove-Item "$artifactsPath" -Force -Recurse
17-
}
16+
if (Test-Path "$artifactsPath")
17+
{
18+
echo "perf: Cleaning $artifactsPath"
19+
Remove-Item "$artifactsPath" -Force -Recurse
20+
}
1821

19-
New-Item -Path "$artifactsPath" -ItemType Directory
22+
New-Item -Path "$artifactsPath" -ItemType Directory
2023

21-
$perfTestProjectPath = "$PSScriptRoot/test/Serilog.Sinks.MSSqlServer.PerformanceTests"
22-
Push-Location "$perfTestProjectPath"
24+
$perfTestProjectPath = "$PSScriptRoot/test/Serilog.Sinks.MSSqlServer.PerformanceTests"
25+
try
26+
{
27+
Push-Location "$perfTestProjectPath"
2328

24-
echo "perf: Running performance test project in $perfTestProjectPath"
25-
& dotnet run -c Release -- -f $Filter
29+
echo "perf: Running performance test project in $perfTestProjectPath"
30+
& dotnet run -c Release -- -f $Filter
2631

27-
cp ".\BenchmarkDotNet.Artifacts\results\*.*" "$artifactsPath\"
28-
Pop-Location
32+
cp ".\BenchmarkDotNet.Artifacts\results\*.*" "$artifactsPath\"
33+
}
34+
finally
35+
{
36+
Pop-Location
37+
}
2938

30-
Pop-Location
39+
}
40+
finally
41+
{
42+
Pop-Location
43+
}

0 commit comments

Comments
 (0)