简体   繁体   English

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

[英]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.

相关问题 使用 Azure Pipelines 从 nuspec 构建 nuget 文件 - Building nuget file from nuspec with Azure Pipelines 如何使用Yaml配置从Azure管道生成特定版本的nuget软件包 - How to generate specific version of nuget package from azure pipelines using yaml configuration Azure 管道和 Nuget 包 - 将包版本设置为标记版本 - Azure Pipelines & Nuget Packages - Set pack version to tag version 如何在 Azure 管道中添加 NuGet Package 源? - How is it possible to add a NuGet Package source inside Azure Pipelines? Azure Pipelines未使用指定的NuGet版本 - Azure Pipelines is not using the NuGet version specified Azure DevOps / Pipelines: Set the “Build Action” and “Copy To Output Directory” properties of a content file in a NuGet package - Azure DevOps / Pipelines: Set the “Build Action” and “Copy To Output Directory” properties of a content file in a NuGet package 从 csproj 获取 Nuget package 步骤中 Azure DevOps 中的版本号 - Fetch version number in Nuget package step in Azure DevOps from csproj 忽略 Azure Pipelines 中 .vdproj 项目的 NuGet 包还原 - Ignore NuGet package restore on .vdproj projects in Azure Pipelines 在 Azure DevOps Git 存储库中使用来自 Azure Pipelines 的 Python 包版本标记 Git 存储库 - Tag Git repo with Python Package version from Azure Pipelines in Azure DevOps Git repo 如何捕获和保留cd的Azure管道中通用工件的工件软件包版本 - How to capture and retain the artifact package version for Universal artifacts in azure pipelines for cd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM