Skip to content

Commit

Permalink
Merge pull request #106 from Lombiq/issue/CARE-265
Browse files Browse the repository at this point in the history
CARE-265: Adding .NET Framework build features
  • Loading branch information
0liver authored Nov 25, 2022
2 parents 1a6b021 + dddd35c commit af965b9
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 29 deletions.
10 changes: 3 additions & 7 deletions .github/actions/build-dotnet/Build-DotNetSolutionOrProject.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
param (
param (
[string] $SolutionOrProject,
[string] $Verbosity,
[string] $EnableCodeAnalysis,
Expand All @@ -14,7 +14,7 @@ function ConvertTo-Array([string] $rawInput)
Write-Output ".NET version number: $Version"

# Notes on build switches that aren't self-explanatory:
# - -p:Retries and -p:RetryDelayMilliseconds are to retry builds if it fails the first time due to random locks.
# - -p:Retries and -p:RetryDelayMilliseconds are used to retry builds when they fail due to random locks.
# - --warnAsMessage:MSB3026 is also to prevent random locks along the lines of "warning MSB3026: Could not copy dlls
# errors." from breaking the build (since we treat warnings as errors).

Expand Down Expand Up @@ -74,11 +74,7 @@ if ($noErrors -and !$?)
exit 1
}

# With node reuse, dotnet build spawns processes that while speed up build, they can cause dotnet test and other dotnet
# tools to randomly hang. So, here we shut down those processes for later actions.
# For details see: https://github.com/Lombiq/UI-Testing-Toolbox/issues/228.
Write-Output "Shutting down .NET build servers."
dotnet build-server shutdown
Stop-DotNetBuildServers

if ($expectedErrorCodes)
{
Expand Down
3 changes: 2 additions & 1 deletion .github/actions/build-dotnet/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ runs:
shell: pwsh
run: |
"${{ github.action_path }}" >> $Env:GITHUB_PATH
(Resolve-Path "${{ github.action_path }}/../../../Scripts").Path >> $Env:GITHUB_PATH
- name: Setup & Detect Cache Configuration
id: detect-cache-configuration
Expand Down Expand Up @@ -125,7 +126,7 @@ runs:
'@
$switches = @{
Solution = (Get-ChildItem ${{ inputs.solution-or-project-path }}).FullName
SolutionOrProject = (Get-ChildItem ${{ inputs.solution-or-project-path }}).FullName
Verbosity = "${{ inputs.verbosity }}"
EnableCodeAnalysis = "${{ inputs.enable-code-analysis }}"
Version = "${{ inputs.publish-version }}"
Expand Down
20 changes: 20 additions & 0 deletions .github/actions/checkout/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Checkout
description: >
Runs actions/checkout. Exists only to centralize which version of the action we use. Intentionally not documented in
Actions.md since it's only meant for internal use.
inputs:
token:
required: false
default: ${{ github.token }}
description: >
The GitHub token to authenticate checkout. Pass in a GitHub personal access token if authenticated submodules are
used.
runs:
using: "composite"
steps:
- uses: actions/[email protected]
with:
submodules: 'recursive'
token: ${{ inputs.token }}
77 changes: 77 additions & 0 deletions .github/actions/msbuild/Build-DotNetFrameworkSolutionOrProject.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
param (
[string] $SolutionOrProject,
[string] $Verbosity,
[string] $EnableCodeAnalysis,
[string] $Version,
[string] $Switches)

function ConvertTo-Array([string] $rawInput)
{
$rawInput.Replace("`r", "").Split("`n") | ForEach-Object { $PSItem.Trim() } | Where-Object { $PSItem }
}

Write-Output ".NET version number: $Version"

if (Test-Path src/Utilities/Lombiq.Gulp.Extensions/Lombiq.Gulp.Extensions.csproj)
{
Write-Output "::group::Gulp Extensions found. It needs to be explicitly built before the solution."

# These need to be different than those for msbuild.
$gulpBuildSwitches = ConvertTo-Array @"
--configuration:Release
--nologo
--verbosity:$Verbosity
--warnaserror
--warnAsMessage:MSB3026
--consoleLoggerParameters:NoSummary
-p:TreatWarningsAsErrors=true
-p:RunAnalyzersDuringBuild=$EnableCodeAnalysis
-p:Retries=4
-p:RetryDelayMilliseconds=1000
-p:Version=$Version
"@

$startTime = [DateTime]::Now
dotnet build src/Utilities/Lombiq.Gulp.Extensions/Lombiq.Gulp.Extensions.csproj @gulpBuildSwitches
$endTime = [DateTime]::Now

Write-Output ("Gulp Extensions build took {0:0.###} seconds." -f ($endTime - $startTime).TotalSeconds)
Write-Output "::endgroup::"
}

# -p:Retries and -p:RetryDelayMilliseconds are used to retry builds when they fail due to random locks.

$buildSwitches = ConvertTo-Array @"
-p:Configuration=Release
-restore
--verbosity:$Verbosity
--warnaserror
-p:TreatWarningsAsErrors=true
-p:RunAnalyzersDuringBuild=$EnableCodeAnalysis
-p:Retries=4
-p:RetryDelayMilliseconds=1000
-p:Version=$Version
$Switches
"@

Write-Output "Building solution or project with ``msbuild $SolutionOrProject $($buildSwitches -join " ")``."

msbuild $SolutionOrProject @buildSwitches

# Without this, if the msbuild command fails with certain MSB error codes (not build errors), they still won't cause
# this script to fail.

# error MSB3644: The reference assemblies for .NETFramework,Version=v4.6.1 were not found. To resolve this, install the
# Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET
# Framework Developer Packs at https://aka.ms/msbuild/developerpacks

if ($?)
{
Write-Output "Build successful."
Stop-DotNetBuildServers
}
else
{
Write-Output "::error::Build failed. See the errors above in the build log."
exit 1
}
73 changes: 73 additions & 0 deletions .github/actions/msbuild/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: MSBuild .NET Framework Build with Static Code Analysis
description: Builds the .NET Framework solution in the given directory with static code analysis.

inputs:
directory:
required: false
default: .
description: Path to the directory where a solution file can be found.
verbosity:
required: false
default: quiet
description: Verbosity parameter for msbuild.
enable-code-analysis:
required: false
default: "true"
description: If set to "true", static code analysis is enabled during the build.
publish-version:
required: false
default: "1.${{ github.run_number }}.${{ github.run_attempt }}-${{ github.run_id }}"
description: The value given to `msbuild`'s `-Version` switch.
msbuild-switches:
required: false
default: ""
description: >
Additional command line switches given to `msbuild`. You must split these into multiple lines, even the
parameter value.
solution-or-project-path:
required: false
default: "*.sln"
description: >
The path of the solution or project file to be built. If you have exactly one .sln file in the current directory
then this can be omitted. Otherwise make sure to specify it to avoid an "MSB1008: Only one project can be
specified." error that is thrown when the `*.sln` wildcard results in multiple files. To build a project file you
must specify a path that ends with `.csproj`. An example: "./src/*Web/*.Web.csproj". The value is given to
PowerShell's `Get-ChildItem` cmdlet as-is, so grepping is still possible but a name with spaces must be escaped
separately.
runs:
using: "composite"
steps:
- name: Setup Scripts
shell: pwsh
run: |
"${{ github.action_path }}" >> $Env:GITHUB_PATH
(Resolve-Path "${{ github.action_path }}/../../../Scripts").Path >> $Env:GITHUB_PATH
- name: Add MSBuild to PATH
# v1.1.3
uses: microsoft/setup-msbuild@34cfbaee7f672c76950673338facd8a73f637506
with:
vs-version: "[17.3,]"

- name: Build and Static Code Analysis
shell: pwsh
working-directory: ${{ inputs.directory }}
run: |
$givenSwitches = @'
${{ inputs.msbuild-switches }}
'@
$allSwitches = @{
SolutionOrProject = (Get-ChildItem ${{ inputs.solution-or-project-path }}).FullName
Verbosity = "${{ inputs.verbosity }}"
EnableCodeAnalysis = "${{ inputs.enable-code-analysis }}"
Version = "${{ inputs.publish-version }}"
Switches = $givenSwitches
}
$startTime = [DateTime]::Now
Build-DotNetFrameworkSolutionOrProject @allSwitches
$endTime = [DateTime]::Now
Write-Output ("Solution or project build took {0:0.###} seconds." -f ($endTime - $startTime).TotalSeconds)
9 changes: 7 additions & 2 deletions .github/actions/test-dotnet/Invoke-SolutionTests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
param ($Verbosity, $Filter)
param ($Verbosity, $Filter, $Configuration)

# Note that this script will only find tests if they were previously build in Release mode.

Expand Down Expand Up @@ -26,13 +26,18 @@ else

$Env:Lombiq_Tests_UI__BrowserConfiguration__Headless = "true"

# We assume that the solution was built in Release configuration. If the tests need to be built in Debug configuration,
# as they should, we need to first build them, but not restore. Otherwise, the Release tests are already built, so we
# don't need to build them here.
$optOut = $Configuration -eq "Debug" ? "--no-restore" : "--no-build"

$tests = dotnet sln list |
Select-Object -Skip 2 |
Select-String "\.Tests\." |
Select-String -NotMatch "Lombiq.Tests.UI.csproj" |
Select-String -NotMatch "Lombiq.Tests.csproj" |
Where-Object {
$result = dotnet test --no-restore --list-tests --verbosity $Verbosity $PSItem 2>&1 | Out-String -Width 9999
$result = dotnet test $optOut --configuration $Configuration --list-tests --verbosity $Verbosity $PSItem 2>&1 | Out-String -Width 9999
-not [string]::IsNullOrEmpty($result) -and $result.Contains("The following Tests are available")
}

Expand Down
13 changes: 12 additions & 1 deletion .github/actions/test-dotnet/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ inputs:
required: false
description: >
Filter expression for dotnet test. See: https://learn.microsoft.com/en-us/dotnet/core/testing/selective-unit-tests.
test-configuration:
required: false
description: Value passed to the --configuration parameter of dotnet test.
default: "Debug"
ui-test-parallelism:
required: false
default: "-1"
Expand Down Expand Up @@ -54,7 +58,14 @@ runs:
- name: Run Tests
shell: pwsh
working-directory: ${{ inputs.build-directory }}
run: Invoke-SolutionTests -Verbosity ${{ inputs.test-verbosity }} -Filter "${{ inputs.test-filter }}"
run: |
$switches = @{
Verbosity = "${{ inputs.test-verbosity }}"
Filter = "${{ inputs.test-filter }}"
Configuration = "${{ inputs.test-configuration }}"
}
Invoke-SolutionTests @switches
# Note that uploading the failure dumps would fail under Windows if the path were too long, regardless of
# LongPathsEnabled, see: https://github.com/actions/upload-artifact/issues/309. To get around that we merge all
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/build-and-test-orchard-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ jobs:
- ${{ inputs.timeout-minutes }}
timeout-minutes: ${{ matrix.timeout-minutes }}
steps:
- uses: actions/[email protected]
with:
submodules: 'recursive'
- name: Checkout
uses: Lombiq/GitHub-Actions/.github/actions/checkout@dev

- name: Set up .NET
uses: Lombiq/GitHub-Actions/.github/actions/setup-dotnet@dev
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/build-dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ jobs:
- ${{ inputs.timeout-minutes }}
timeout-minutes: ${{ matrix.timeout-minutes }}
steps:
- uses: actions/[email protected]
with:
submodules: 'recursive'
- name: Checkout
uses: Lombiq/GitHub-Actions/.github/actions/checkout@dev

- name: Set up .NET
uses: Lombiq/GitHub-Actions/.github/actions/setup-dotnet@dev
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/deploy-to-azure-app-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ jobs:
shell: pwsh
timeout-minutes: ${{ inputs.timeout-minutes }}
steps:
- uses: actions/[email protected]
with:
submodules: 'recursive'
- name: Checkout
uses: Lombiq/GitHub-Actions/.github/actions/checkout@dev

- name: Set up .NET
uses: Lombiq/GitHub-Actions/.github/actions/setup-dotnet@dev
Expand Down
Loading

0 comments on commit af965b9

Please sign in to comment.