10

Is it possible to use Azure DevOps pipeline variables in .nuspec files, which are used for packages creation?

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>MyTemplate</id>
    <version>$(var1).$(var2).$(var3)</version>
    <description>
      Some template
    </description>
    <authors>Test Test</authors>
    <packageTypes>
      <packageType name="Template" />
    </packageTypes>
  </metadata>
</package>

Or is it a way to ovveride version specified in .nuspec file(it is required element) by those one in .yaml task?

task: NuGetCommand@2
  displayName: Pack template
  inputs:
    command: pack
    packagesToPack: '**/Template/*.nuspec'
    packDestination: $(Build.ArtifactStagingDirectory)
    versioningScheme: byPrereleaseNumber
    majorVersion: '$(var1)'
    minorVersion: '$(var2)'
    patchVersion: '$(var3)'

But with versioningScheme: byPrereleaseNumber we will get timestamp added to our numbers.

riQQ
  • 9,878
  • 7
  • 49
  • 66
user3132547
  • 193
  • 2
  • 13
  • 2
    You could overwrite those settings by using one of the token replace tasks: https://marketplace.visualstudio.com/search?term=replac&target=AzureDevOps&category=Azure%20Pipelines&sortBy=Relevance – Rob Bos Dec 02 '18 at 16:23
  • I had a similar issue - see here for my solution: https://stackoverflow.com/questions/54718866/azure-pipeline-nuget-package-versioning-scheme-how-to-get-1-0-revr#71241200 – SharpC Mar 15 '22 at 10:09

1 Answers1

4

Pop-up ToolTip for Build Properties:

Specifies a list of token=value pairs, separated by semicolons, where each occurrence of $token$ in the .nuspec file will be replaced with the given value. Values can be strings in quotation marks.

I figured out the following YAML by going into the UI builder for editing the pipeline visually, there's an "Advanced" pane inside the NuGet > Pack task. It allows you to specify additional token replacement values under "build properties" (buildProperties)

I have a feeling it's one of those things where you have to transform one type of token into another.. see the buildProperties on the last line:

variables:
  Parameters.requestedMajorVersion: '1'
  Parameters.requestedMinorVersion: '0'
  Parameters.requestedPatchVersion: '6'

steps:
- task: NuGetCommand@2
  displayName: 'NuGet pack'
  inputs:
    command: pack
    packagesToPack: '**/*.nuspec'
    versioningScheme: byPrereleaseNumber
    majorVersion: '$(Parameters.requestedMajorVersion)'
    minorVersion: '$(Parameters.requestedMinorVersion)'
    patchVersion: '$(Parameters.requestedPatchVersion)'
    includeSymbols: true
    buildProperties: 'id=$(Build.Variable)'

Example .nuspec

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata minClientVersion="2.5">
    <id>$id$</id>
bkwdesign
  • 1,953
  • 2
  • 28
  • 50
  • A problem i wasn't able to solve is, if you try to add an Url as a value to any token which contains an Equals Sign '='. Then the parser of Nuget Pack has a Problem to resolve the correct value. Are there any known Escape sequences for this kind of Problem? – phifi Sep 08 '20 at 08:52
  • 2
    I successfully used the method described by @bkwdesign to replace the version variables from the Yaml to the .nuspec This should be marked as the correct answer. Example that worked for me: buildProperties: 'vmajor=$(Major);vminor=$(Minor);vpatch=$(Patch)' .nuspec: $vmajor$.$vminor$.$vpatch$ – Gerrie Pretorius Nov 10 '20 at 15:21
  • This method worked for me to replace the $version$ in .nuspec file with the version variable that I have in my yaml pipeline. Nuspec file - $version$ Then, in yaml pipeline use buildProperties in Nuget task as- buildProperties: 'version=$(nugetVersion)' Where nugetVersion is your pipeline variable – Jitendra Panchal Jul 05 '22 at 13:42