Skip to content

Commit

Permalink
Merge pull request #405 from Lombiq/issue/OSOE-857
Browse files Browse the repository at this point in the history
OSOE-857: Fixing that during NuGet publishing baseline package validation was run for major version updates too, and fixing SourceLink package add
  • Loading branch information
sarahelsaig authored Dec 20, 2024
2 parents 0759b8d + f156b37 commit bae6803
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
30 changes: 28 additions & 2 deletions .github/actions/publish-nuget/Add-SourceLinkPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ else
$projectFiles = $projectPaths | ForEach-Object { Get-Item -Path $PSItem }
}

# We need multiple iterations below, because before we can add the SourceLink package to any of the projects, we need to
# make sure that its dependencies have NuGetBuild set too.

# Add the NuGetBuild property to all project files while keeping track of the original content.
foreach ($projectFile in $projectFiles)
{
Write-Output "Adding the NuGetBuild property to $($projectFile.FullName)."

# Below we first prepare the project file by adding the NuGetBuild=true property to the top of it. This is needed
# for dotnet add package which could otherwise fail due to conditions in the project file.
# We don't use a Directory.Build.props file for this because the project might have one already, and then we'd need
Expand All @@ -40,14 +46,34 @@ foreach ($projectFile in $projectFiles)

# Save the changes back to the .csproj file.
$projectXml.Save($projectFile)
}

# Run dotnet add package for each project.
foreach ($projectFile in $projectFiles)
{
Write-Output "Adding SourceLink package to $($projectFile.FullName)."

# We can't use --no-restore because not only would it skip checks, it'd also be incompatible with projects using
# Central Package Management (https://learn.microsoft.com/en-us/nuget/consume-packages/central-package-management).
# Unfortunately, this makes NuGet publishing a lot slower than using dotnet restore, and it's also more verbose
# (without the ability to configure that verbosity).
dotnet add $projectFile.FullName package 'Microsoft.SourceLink.GitHub'
# Due to output buffering, the order of output messages might be mixed up without saving the output to a variable.
$dotnetOutput = dotnet add $projectFile.FullName package 'Microsoft.SourceLink.GitHub'
Write-Output $dotnetOutput

if ($LASTEXITCODE -ne 0)
{
Write-Output "::error file=$($projectFile.FullName)::dotnet add package $($projectFile.FullName) 'Microsoft.SourceLink.GitHub' failed."
exit 1
}
}

# Remove the NuGetBuild property from all project files.
foreach ($projectFile in $projectFiles)
{
Write-Output "Removing the NuGetBuild property from $($projectFile.FullName)."

# The NuGetBuild property mustn't remain in the project file.
# The NuGetBuild property mustn't remain in the project file for NuGet publishing.
$projectXml = [xml](Get-Content $projectFile)
$projectXml.Project.RemoveChild($projectXml.Project.FirstChild)
$projectXml.Save($projectFile)
Expand Down
4 changes: 3 additions & 1 deletion .github/actions/publish-nuget/New-NuGetPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ function Get-ProjectProperty
}
}

# The last condition wouldn't be necessary if baseline package validation would skip major releases by default, see:
# https://github.com/dotnet/sdk/issues/40907.
$shouldDownloadBaseLinePackages = ($EnablePackageValidation -And
$PackageValidationBaselineVersion -And
-not ($Version -match '-(alpha|beta|preview|rc)[.-]') -And
$Version.Split('.')[0] -le $PackageValidationBaselineVersion.Split('.')[0])
[int]$Version.Split('.')[0] -le [int]$PackageValidationBaselineVersion.Split('.')[0])

$projects = (Test-Path *.sln) ? (dotnet sln list | Select-Object -Skip 2 | Get-Item) : (Get-ChildItem *.csproj)

Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/publish-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ on:
type: string
default: USE_GITHUB_REF_NAME
description: >
The desired NuGet package version used for publishing. The correct format is "vM.N.O" or
"vM.N.O-alpha.X.<issue-code>". If not specified, the GITHUB_REF_NAME environment variable is used
which is suitable if the version is derived from a git tag.
The desired NuGet package version used for publishing. If not specified, the GITHUB_REF_NAME environment variable
is used which is suitable if the version is derived from a git tag. If "USE_GITHUB_RUN_NUMBER" is used, then the
version is derived from the latest non-prerelease version tag and the current run number to create a preview
version (e.g. "1.0.1-preview-1" if the current version is 1.0.0). With "USE_NEXT_PATCH_VERSION" the version is
derived from the latest non-prerelease version tag to create the next patch version (e.g. "1.0.1" if the current
version is 1.0.0).
nuget-artifact-retention-days:
type: string
default: '14'
Expand Down

0 comments on commit bae6803

Please sign in to comment.