Gheorghe Volosenco
Gheorghe Volosenco

Reputation: 89

How to Forbid the usage of a PreRelease version of a nuget package in Release Pipeline

Is it possible to "fail" the release if there are nugget packages in pre-release versions?

Maybe there is a task already for this in Azure DevOps, or maybe there's a way to do it with Powershell?

Upvotes: 5

Views: 1418

Answers (2)

Gheorghe Volosenco
Gheorghe Volosenco

Reputation: 89

A solution (if you want to take packets from SLN file) for this can be the following:

Get-Content .\SolutionName.sln |
where { $_ -match "Project.+, ""(.+)""," } |
foreach { $matches[1] } |
% { Get-Content $_ -ErrorAction SilentlyContinue |
Find "<PackageReference Include" } |
Sort-Object -Unique |
% { if($_ -match "-test") { Write-Host "You're using a PreRelease Version for the following Package $($_)"} }

Change SolutionName with the name of your solution. Change -test with the suffix of your prerelease package(in my case it was Version="4.1.2-test").

Or if you want to take nugets from csproj files recursively

get-childitem "$(get-location)" -recurse |
where {$_.extension -eq ".csproj"}|
% { Get-Content $_.FullName -ErrorAction SilentlyContinue |
Find "<PackageReference Include" } |
Sort-Object -Unique |
% { if($_ -match "-test") { Write-Error "You're using a PreRelease Version for the following Package $($_)"} }

Again, change the -test with what you need.

Upvotes: 1

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

You can read the .csproj file with PowerShell and check if pre-release exist, if yes make an error:

[xml]$csproj = Get-Content path/to/csproj/file # e.g. $(Agent.ReleaseDirectory)/myproject/myproject.csproj
$versions = $csproj.Projects.ItemGroup.PackageReference.Version
$versions.ForEach({

   # Pre-releases are with '-' symbol, e.g. 1.0.0-beta
   if($_ -match "(?<number>\d-)")
   {
       Write-Error "Pre-release exist: $_"
   }

})

Upvotes: 3

Related Questions