Skip to content

Commit 0c371f9

Browse files
Update pipeline logic to generate the SBOM for release builds (#767) (#770)
* Update pipeline logic to generate the SBOM for release builds (#767) * Update helper.psm1 to support installing multiple .Net SDK versions * Upgrade .Net to version 2.1.818 * Remove install .Net 3.1 task
1 parent 7899acb commit 0c371f9

File tree

2 files changed

+57
-69
lines changed

2 files changed

+57
-69
lines changed

azure-pipelines.yml

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,15 @@ steps:
3131
env:
3232
BuildSourceBranch: $(Build.SourceBranch)
3333

34-
- pwsh: |
35-
Invoke-WebRequest 'https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.ps1' -OutFile 'dotnet-install.ps1'
36-
./dotnet-install.ps1 -InstallDir "$env:ProgramFiles/dotnet" -Version "3.1.415" -Channel 'release'
37-
displayName: 'Install .Net 3.1 which is required by the Microsoft.ManifestTool.dll tool'
38-
condition: eq(variables['IsReleaseBuild'], 'true')
39-
4034
- pwsh: ./build.ps1 -NoBuild -Bootstrap
4135
displayName: 'Running ./build.ps1 -NoBuild -Bootstrap'
4236

4337
- pwsh: |
4438
$ErrorActionPreference = "Stop"
45-
if ($isReleaseBuild)
46-
{
47-
./build.ps1 -Clean -Configuration Release -BuildNumber "$(buildNumber)" -AddSBOM -SBOMUtilSASUrl $env:SBOMUtilSASUrl
48-
}
49-
else
50-
{
51-
./build.ps1 -Clean -Configuration Release -BuildNumber "$(buildNumber)"
52-
}
39+
$shouldAddSBOM = [bool]"$(IsReleaseBuild)"
40+
41+
./build.ps1 -Clean -Configuration Release -BuildNumber "$(buildNumber)" -AddSBOM:$shouldAddSBOM -SBOMUtilSASUrl "$(SBOMUtilSASUrl)"
5342
displayName: 'Build worker code'
54-
env:
55-
SBOMUtilSASUrl: $(SBOMUtilSASUrl)
5643

5744
- pwsh: ./build.ps1 -NoBuild -Test
5845
displayName: 'Running UnitTest'

tools/helper.psm1

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,85 +7,69 @@ using namespace System.Runtime.InteropServices
77

88
$IsWindowsEnv = [RuntimeInformation]::IsOSPlatform([OSPlatform]::Windows)
99
$RepoRoot = (Resolve-Path "$PSScriptRoot/..").Path
10-
$MinimalSDKVersion = '2.1.805'
11-
$DefaultSDKVersion = '2.1.805'
12-
$LocalDotnetDirPath = if ($IsWindowsEnv) { "$env:LocalAppData\Microsoft\dotnet" } else { "$env:HOME/.dotnet" }
13-
$GrpcToolsVersion = '2.27.0' # grpc.tools
14-
$GoogleProtobufToolsVersion = '3.11.4' # google.protobuf.tools
1510

16-
function Find-Dotnet
17-
{
18-
$dotnetFile = if ($IsWindowsEnv) { "dotnet.exe" } else { "dotnet" }
19-
$dotnetExePath = Join-Path -Path $LocalDotnetDirPath -ChildPath $dotnetFile
20-
21-
# If dotnet is already in the PATH, check to see if that version of dotnet can find the required SDK.
22-
# This is "typically" the globally installed dotnet.
23-
$foundDotnetWithRightVersion = $false
24-
$dotnetInPath = Get-Command 'dotnet' -ErrorAction SilentlyContinue
25-
if ($dotnetInPath) {
26-
$foundDotnetWithRightVersion = Test-DotnetSDK $dotnetInPath.Source
27-
}
11+
$DotnetSDKVersionRequirements = @{
2812

29-
if (-not $foundDotnetWithRightVersion) {
30-
if (Test-DotnetSDK $dotnetExePath) {
31-
Write-Warning "Can't find the dotnet SDK version $MinimalSDKVersion or higher, prepending '$LocalDotnetDirPath' to PATH."
32-
$env:PATH = $LocalDotnetDirPath + [IO.Path]::PathSeparator + $env:PATH
33-
}
34-
else {
35-
throw "Cannot find the dotnet SDK for .NET Core 2.1. Please specify '-Bootstrap' to install build dependencies."
36-
}
13+
'2.1' = @{
14+
MinimalPatch = '818'
15+
DefaultPatch = '818'
3716
}
38-
}
3917

40-
function Get-VersionCore($Version) {
41-
if ($Version -match '^\d+\.\d+\.\d+') {
42-
$Matches.0
43-
} else {
44-
throw "Unexpected version: '$Version'"
18+
# .NET SDK 3.1 is required by the Microsoft.ManifestTool.dll tool
19+
'3.1' = @{
20+
MinimalPatch = '417'
21+
DefaultPatch = '417'
4522
}
4623
}
4724

48-
function Test-DotnetSDK
25+
$GrpcToolsVersion = '2.27.0' # grpc.tools
26+
$GoogleProtobufToolsVersion = '3.11.4' # google.protobuf.tools
27+
28+
function Find-Dotnet
4929
{
50-
param($dotnetExePath)
30+
$listSdksOutput = dotnet --list-sdks
31+
$installedDotnetSdks = $listSdksOutput | ForEach-Object { $_.Split(" ")[0] }
32+
Write-Log "Detected dotnet SDKs: $($installedDotnetSdks -join ', ')"
5133

52-
if (Test-Path $dotnetExePath) {
53-
$installedVersion = Get-VersionCore (& $dotnetExePath --version)
54-
return [version]$installedVersion -ge [version]$MinimalSDKVersion
34+
foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys) {
35+
$minimalVersion = "$majorMinorVersion.$($DotnetSDKVersionRequirements[$majorMinorVersion].MinimalPatch)"
36+
37+
$firstAcceptable = $installedDotnetSdks |
38+
Where-Object { $_.StartsWith("$majorMinorVersion.") } |
39+
Where-Object { [System.Management.Automation.SemanticVersion]::new($_) -ge [System.Management.Automation.SemanticVersion]::new($minimalVersion) } |
40+
Select-Object -First 1
41+
42+
if (-not $firstAcceptable) {
43+
throw "Cannot find the dotnet SDK for .NET Core $majorMinorVersion. Version $minimalVersion or higher is required. Please specify '-Bootstrap' to install build dependencies."
44+
}
5545
}
56-
return $false
5746
}
5847

5948
function Install-Dotnet {
6049
[CmdletBinding()]
6150
param(
62-
[string]$Channel = 'release',
63-
[string]$Version = $DefaultSDKVersion
51+
[string]$Channel = 'release'
6452
)
6553

6654
try {
6755
Find-Dotnet
6856
return # Simply return if we find dotnet SDk with the correct version
6957
} catch { }
7058

71-
$logMsg = if (Get-Command 'dotnet' -ErrorAction SilentlyContinue) {
72-
"dotent SDK is not present. Installing dotnet SDK."
73-
} else {
74-
"dotnet SDK out of date. Require '$MinimalSDKVersion' but found '$dotnetSDKVersion'. Updating dotnet."
75-
}
76-
Write-Log $logMsg -Warning
77-
78-
$obtainUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain"
59+
$obtainUrl = "https://raw.githubusercontent.com/dotnet/install-scripts/main/src"
7960

8061
try {
81-
Remove-Item $LocalDotnetDirPath -Recurse -Force -ErrorAction SilentlyContinue
8262
$installScript = if ($IsWindowsEnv) { "dotnet-install.ps1" } else { "dotnet-install.sh" }
8363
Invoke-WebRequest -Uri $obtainUrl/$installScript -OutFile $installScript
8464

85-
if ($IsWindowsEnv) {
86-
& .\$installScript -Channel $Channel -Version $Version
87-
} else {
88-
bash ./$installScript -c $Channel -v $Version
65+
foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys) {
66+
$version = "$majorMinorVersion.$($DotnetSDKVersionRequirements[$majorMinorVersion].DefaultPatch)"
67+
Write-Log "Installing dotnet SDK version $version" -Warning
68+
if ($IsWindowsEnv) {
69+
& .\$installScript -Channel $Channel -Version $Version -InstallDir "$env:ProgramFiles/dotnet"
70+
} else {
71+
bash ./$installScript -c $Channel -v $Version --install-dir /usr/share/dotnet
72+
}
8973
}
9074
}
9175
finally {
@@ -131,7 +115,7 @@ function Resolve-ProtoBufToolPath
131115
{
132116
if (-not $Script:protoc_Path) {
133117
Write-Log "Resolve the protobuf tools for auto-generating code"
134-
$nugetPath = "~/.nuget/packages"
118+
$nugetPath = Get-NugetPackagesPath
135119
$toolsPath = "$RepoRoot/tools"
136120

137121
if (-not (Test-Path "$toolsPath/obj/project.assets.json")) {
@@ -205,6 +189,23 @@ function Write-Log
205189
Write-Host -ForegroundColor $foregroundColor "${indentPrefix}${Message}"
206190
}
207191

192+
function Get-NugetPackagesPath
193+
{
194+
if ($env:NUGET_PACKAGES)
195+
{
196+
return $env:NUGET_PACKAGES
197+
}
198+
199+
if ($IsWindowsEnv)
200+
{
201+
return "${env:USERPROFILE}\.nuget\packages"
202+
}
203+
else
204+
{
205+
return "${env:HOME}/.nuget/packages"
206+
}
207+
}
208+
208209
#region Start-ResGen
209210

210211
$generated_code_template = @'

0 commit comments

Comments
 (0)