1

Currently, I have *.yaml variables like this, with an auto-increment with a counter:

 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:

<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:

<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.

hiichaki
  • 834
  • 9
  • 19

2 Answers2

1

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. For example:

enter image description hereenter image description here

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. For details , you can refer to this case. Finally use the variables in the nuget pack task.

In addition, you can see if the Automatic package versioning option in nuget pack task meets your needs.

enter image description here

Hugh Lin
  • 17,829
  • 2
  • 21
  • 25
  • This thing seems working as described but it really makes things more complicated for me. The main purpose of my question is if it is possible to configure everything from one file *.nuspec. If there's still need to open/execute something else it's easier to do as I was doing - write release notes in *.nuspec and change minor/major version in *.yaml. – hiichaki Sep 01 '20 at 11:48
1

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

I am afraid there is no such out of box way to pass variables from the .nuspec to the *.yaml.

That because the Azure pipeline could not parse the .nuspec file directly. So we need to simply parse the .nuspec file by some scripts, like powershell.

Our team had similar requirements before. We will receive pre-release versions of nuget packages developed by other groups. 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.

To get the version from the *.nuspec, we could use following powershell scripts to parse it:

$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:

$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"
Leo Liu
  • 71,098
  • 10
  • 114
  • 135