Skip to content

Update pipeline logic to generate the SBOM for release builds (#767) #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,15 @@ steps:
env:
BuildSourceBranch: $(Build.SourceBranch)

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

- pwsh: ./build.ps1 -NoBuild -Bootstrap
displayName: 'Running ./build.ps1 -NoBuild -Bootstrap'

- pwsh: |
$ErrorActionPreference = "Stop"
if ($isReleaseBuild)
{
./build.ps1 -Clean -Configuration Release -BuildNumber "$(buildNumber)" -AddSBOM -SBOMUtilSASUrl $env:SBOMUtilSASUrl
}
else
{
./build.ps1 -Clean -Configuration Release -BuildNumber "$(buildNumber)"
}
$shouldAddSBOM = [bool]"$(IsReleaseBuild)"

./build.ps1 -Clean -Configuration Release -BuildNumber "$(buildNumber)" -AddSBOM:$shouldAddSBOM -SBOMUtilSASUrl "$(SBOMUtilSASUrl)"
displayName: 'Build worker code'
env:
SBOMUtilSASUrl: $(SBOMUtilSASUrl)

- pwsh: ./build.ps1 -NoBuild -Test
displayName: 'Running UnitTest'
Expand Down
107 changes: 54 additions & 53 deletions tools/helper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,69 @@ using namespace System.Runtime.InteropServices

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

function Find-Dotnet
{
$dotnetFile = if ($IsWindowsEnv) { "dotnet.exe" } else { "dotnet" }
$dotnetExePath = Join-Path -Path $LocalDotnetDirPath -ChildPath $dotnetFile

# If dotnet is already in the PATH, check to see if that version of dotnet can find the required SDK.
# This is "typically" the globally installed dotnet.
$foundDotnetWithRightVersion = $false
$dotnetInPath = Get-Command 'dotnet' -ErrorAction SilentlyContinue
if ($dotnetInPath) {
$foundDotnetWithRightVersion = Test-DotnetSDK $dotnetInPath.Source
}
$DotnetSDKVersionRequirements = @{

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

function Get-VersionCore($Version) {
if ($Version -match '^\d+\.\d+\.\d+') {
$Matches.0
} else {
throw "Unexpected version: '$Version'"
# .NET SDK 3.1 is required by the Microsoft.ManifestTool.dll tool
'3.1' = @{
MinimalPatch = '417'
DefaultPatch = '417'
}
}

function Test-DotnetSDK
$GrpcToolsVersion = '2.27.0' # grpc.tools
$GoogleProtobufToolsVersion = '3.11.4' # google.protobuf.tools

function Find-Dotnet
{
param($dotnetExePath)
$listSdksOutput = dotnet --list-sdks
$installedDotnetSdks = $listSdksOutput | ForEach-Object { $_.Split(" ")[0] }
Write-Log "Detected dotnet SDKs: $($installedDotnetSdks -join ', ')"

if (Test-Path $dotnetExePath) {
$installedVersion = Get-VersionCore (& $dotnetExePath --version)
return [version]$installedVersion -ge [version]$MinimalSDKVersion
foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys) {
$minimalVersion = "$majorMinorVersion.$($DotnetSDKVersionRequirements[$majorMinorVersion].MinimalPatch)"

$firstAcceptable = $installedDotnetSdks |
Where-Object { $_.StartsWith("$majorMinorVersion.") } |
Where-Object { [System.Management.Automation.SemanticVersion]::new($_) -ge [System.Management.Automation.SemanticVersion]::new($minimalVersion) } |
Select-Object -First 1

if (-not $firstAcceptable) {
throw "Cannot find the dotnet SDK for .NET Core $majorMinorVersion. Version $minimalVersion or higher is required. Please specify '-Bootstrap' to install build dependencies."
}
}
return $false
}

function Install-Dotnet {
[CmdletBinding()]
param(
[string]$Channel = 'release',
[string]$Version = $DefaultSDKVersion
[string]$Channel = 'release'
)

try {
Find-Dotnet
return # Simply return if we find dotnet SDk with the correct version
} catch { }

$logMsg = if (Get-Command 'dotnet' -ErrorAction SilentlyContinue) {
"dotent SDK is not present. Installing dotnet SDK."
} else {
"dotnet SDK out of date. Require '$MinimalSDKVersion' but found '$dotnetSDKVersion'. Updating dotnet."
}
Write-Log $logMsg -Warning

$obtainUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain"
$obtainUrl = "https://raw.githubusercontent.com/dotnet/install-scripts/main/src"

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

if ($IsWindowsEnv) {
& .\$installScript -Channel $Channel -Version $Version
} else {
bash ./$installScript -c $Channel -v $Version
foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys) {
$version = "$majorMinorVersion.$($DotnetSDKVersionRequirements[$majorMinorVersion].DefaultPatch)"
Write-Log "Installing dotnet SDK version $version" -Warning
if ($IsWindowsEnv) {
& .\$installScript -Channel $Channel -Version $Version -InstallDir "$env:ProgramFiles/dotnet"
} else {
bash ./$installScript -c $Channel -v $Version --install-dir /usr/share/dotnet
}
}
}
finally {
Expand Down Expand Up @@ -131,7 +115,7 @@ function Resolve-ProtoBufToolPath
{
if (-not $Script:protoc_Path) {
Write-Log "Resolve the protobuf tools for auto-generating code"
$nugetPath = "~/.nuget/packages"
$nugetPath = Get-NugetPackagesPath
$toolsPath = "$RepoRoot/tools"

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

function Get-NugetPackagesPath
{
if ($env:NUGET_PACKAGES)
{
return $env:NUGET_PACKAGES
}

if ($IsWindowsEnv)
{
return "${env:USERPROFILE}\.nuget\packages"
}
else
{
return "${env:HOME}/.nuget/packages"
}
}

#region Start-ResGen

$generated_code_template = @'
Expand Down