如何在 *.nuspec 的 Azure Pipelines 中仅设置主要和次要 NuGet 包版本?

How to set only major and minor NuGet package version in Azure Pipelines from *.nuspec?

目前,我有这样的 *.yaml 变量,带有一个带有计数器的自动增量:

 variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  majorVersion: '1'  
  minorVersion: '1'
  patchVersion: $[counter(format('{0}.{1}', variables['majorVersion'], variables['minorVersion']), 0)]
  productVersion: $[format('{0}.{1}.{2}', variables['majorVersion'], variables['minorVersion'], variables['patchVersion'])]    

和 *.nuspec 像这样:

<metadata>
    <id>$id$</id>
    <version>1.0.0</version> <!--just a placeholder because can't be empty-->
    <authors>$author$</authors>
    <description>some description</description>
    <releaseNotes>some release notes</releaseNotes>
</metadata>

我想要实现的是将主要和次要版本变量从 *.nuspec 传递到 *.yaml 并保持自动递增逻辑,如下所示:

<metadata>
    <id>$id$</id>
    <version>1.0.0</version> <!--just a placeholder because can't be empty-->

    <customVar_MajorVersion>1</customVar_MajorVersion>
    <customVar_MinorVersion>1</customVar_MinorVersion>

    <authors>$author$</authors>
    <description>some description</description>
    <releaseNotes>some release notes</releaseNotes>
</metadata>

并像这样使用它们:

variables:
 solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

  majorVersion: $(fromMyNuspec.customVar_MajorVersion) 
  minorVersion: $(fromMyNuspec.customVar_MinorVersion)

  patchVersion: $[counter(format('{0}.{1}', variables['majorVersion'], variables['minorVersion']), 0)]
  productVersion: $[format('{0}.{1}.{2}', variables['majorVersion'], variables['minorVersion'], variables['patchVersion'])]

是否有可能得到我描述的行为?

我找到的最接近的问题是 this one,但没有可接受的答案。

您可以尝试在变量组中定义版本变量。然后通过Get Variable Groups By Idrestapi获取patchVersion1变量的值,通过脚本自动自增该值。例如:

示例脚本:

$token = "{PAT token}"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url1="https://dev.azure.com/{org}/{}pro/_apis/distributedtask/variablegroups?groupIds=5&api-version=6.0-preview.2"
    
$response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get 
$rev=[int] $response1.value.variables.patchVersion1.value
$rev++

Write-Host "result = $($rev | ConvertTo-Json -Depth 100)"

然后通过Variablegroups-Update rest api. For details , you can refer to this 更新变量组中版本变量的值。最后使用nuget pack任务中的变量。

此外,您可以查看nuget pack任务中的Automatic package versioning选项是否满足您的需求。

What do I want to achieve is to pass major and minor version variables from the *.nuspec to the *.yaml

恐怕没有这种开箱即用的方法可以将变量从 .nuspec 传递到 *.yaml

那是因为 Azure 管道无法直接解析 .nuspec 文件。所以我们需要简单地通过一些脚本来解析.nuspec文件,比如powershell.

我们团队之前也有类似的需求。我们将收到 pre-release 个版本的其他组开发的 nuget 包。我们需要测试包,将包版本从pre-release版本修改为正式版。所以我们需要从nuspec文件中获取包版本并修改

要从 *.nuspec 获取版本,我们可以使用以下 powershell 脚本来解析它:

$customVar_MajorVersion = ([xml](Get-Content "$(System.DefaultWorkingDirectory)\Test\MyCustom.nuspec")).package.metadata.customVar_MajorVersion
$customVar_MinorVersion = ([xml](Get-Content "$(System.DefaultWorkingDirectory)\Test\MyCustom.nuspec")).package.metadata.customVar_MinorVersion

echo "The customVar_MajorVersion is $customVar_MajorVersion"
echo "The customVar_MinorVersion is $customVar_MinorVersion"

然后如果我们想为这些变量保留auto-increment,我们可以auto-increment这些变量并在上面的powershell任务中使用Logging Command来设置变量,这样我们就可以使用它在下一个任务中:

$customVar_MajorVersion = ([xml](Get-Content "$(System.DefaultWorkingDirectory)\Test\MyCustom.nuspec")).package.metadata.customVar_MajorVersion
$customVar_MinorVersion = ([xml](Get-Content "$(System.DefaultWorkingDirectory)\Test\MyCustom.nuspec")).package.metadata.customVar_MinorVersion

$increment_MajorVersion= 1+"$customVar_MajorVersion"

$increment_MinorVersion= 1+"$customVar_MajorVersion"

Write-Host "##vso[task.setvariable variable=MajorVersion]$increment_MajorVersion"
Write-Host "##vso[task.setvariable variable=MinorVersion]$increment_MinorVersion"