[英]How to set only major and minor NuGet package version in Azure Pipelines from *.nuspec?
Currently, I have *.yaml variables like this, with an auto-increment with a counter:目前,我有这样的 *.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'])]
and *.nuspec like this:和 *.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>
What do I want to achieve is to pass major and minor version variables from the *.nuspec to the *.yaml and also to keep auto-increment logic something like this:我想要实现的是将主要和次要版本变量从 *.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>
And use them something like this:并像这样使用它们:
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'])]
Is it possible to get the behaviour as I described?是否有可能获得我所描述的行为?
The closest question I found is this one , but there's no accepted answer.我发现的最接近的问题是this one ,但没有公认的答案。
You can try to define version variable in variable group.您可以尝试在变量组中定义版本变量。 Then get the value of the patchVersion1
variable through Get Variable Groups By Id rest api, and automatically increment the value through the script.然后通过Get Variable Groups By Id rest api 获取patchVersion1
变量的值,并通过脚本自动递增该值。 For example:例如:
Sample script:示例脚本:
$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)"
Then update the value of the version variable in the variable group through Variablegroups-Update rest api.然后通过Variablegroups-Update rest api 更新变量组中version变量的值。 For details , you can refer to this case .具体可以参考这个案例。 Finally use the variables in the nuget pack task.最后使用 nuget pack 任务中的变量。
In addition, you can see if the Automatic package versioning option in nuget pack task meets your needs.此外,您可以查看 nuget pack 任务中的自动包版本控制选项是否满足您的需求。
What do I want to achieve is to pass major and minor version variables from the *.nuspec to the *.yaml我想要实现的是将主要和次要版本变量从 *.nuspec 传递到 *.yaml
I am afraid there is no such out of box way to pass variables from the .nuspec
to the *.yaml
.恐怕没有这种开箱即用的方式将变量从.nuspec
到*.yaml
。
That because the Azure pipeline could not parse the .nuspec
file directly.那是因为 Azure 管道无法直接解析.nuspec
文件。 So we need to simply parse the .nuspec
file by some scripts, like powershell.所以我们需要通过一些脚本简单地解析.nuspec
文件,比如 powershell。
Our team had similar requirements before.我们团队以前也有类似的要求。 We will receive pre-release versions of nuget packages developed by other groups.我们将收到其他团体开发的 nuget 软件包的预发布版本。 We need to test the package and change the package version from the pre-release version to the official version.我们需要测试包并将包版本从预发布版本更改为正式版本。 So we need to get the package version from the nuspec
file and modify it.所以我们需要从nuspec
文件中获取包版本并进行修改。
To get the version from the *.nuspec
, we could use following powershell scripts to parse it:要从*.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"
Then if we want to keep auto-increment for those variables, we could auto-increment those variables and use Logging Command in above powershell task to set the variable, so that we could use it in the next task:然后,如果我们想保持这些变量的自动递增,我们可以自动递增这些变量,并在上面的 powershell 任务中使用Logging 命令来设置变量,以便我们可以在下一个任务中使用它:
$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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.