1

I was trying to set the package version using the following yml, however I keep getting the error ##[error]No value was found for the provided environment variable. when the dotnetcli task is executed.

trigger:
- master

name: 0.1.2-prerelease.$(Date:yyMM)$(DayOfMonth)$(Rev:rr)

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  nugetVersion: 0.1.2-prerelease.$(Date:yyMM)$(DayOfMonth)$(Rev:rr)

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'


- task: DotNetCoreCLI@2
  inputs:
    command: 'pack'
    packagesToPack: '$(Build.SourcesDirectory)/src/**/*.csproj'
    nobuild: true
    versionEnvVar: '$(nugetVersion)'
    versioningScheme: 'byEnvVar'
resp78
  • 1,414
  • 16
  • 37

2 Answers2

3

How to pack prerelease nuget packages through Azure DevOps (yml)?

There are couple of alternatives

1

If you want use the $(Date:yyMM)$(DayOfMonth)$(Rev:rr) in the nuget version, the directly way to achieve this is using byBuildNumber.

2

using $(build.BuildNumber) as mentioned by Shayki Abramczyk

- task: DotNetCoreCLI@2
  inputs:
    command: 'pack'
    packagesToPack: '$(Build.SourcesDirectory)/src/**/*.csproj'
    nobuild: true
    versionEnvVar: '$(build.BuildNumber)'
    versioningScheme: 'byEnvVar'

3

But if the byBuildNumber is not your choice, we need to create our own $(Date:yyMM) and $(Rev:rr). That because those $(Date:yyMM) and $(Rev:rr) variables could not be parsed in the Variables.

You could check my previous thread for the details info.

To create the $(Date:yyMM), we could parse the date of the pipeline.startTime to get the value of $(Date:yyMM)$(DayOfMonth)

variables:
  date: '$[format('{0:yyMMdd}', pipeline.startTime)]'

Then we create the $(Rev:rr), we could use a counter, like:

variables:
  InternalNumber: '1'
  CounterNumber: '$[counter(variables['InternalNumber'], 1)]'

Now, the variable of nugetVersion could be:

variables:
  date: '$[format('{0:yyMMdd}', pipeline.startTime)]'
  InternalNumber: '1'
  CounterNumber: '$[counter(variables['InternalNumber'], 1)]'
  nugetVersion: '0.1.2-prerelease.$(date)$(CounterNumber)'

As the test result:

enter image description here

resp78
  • 1,414
  • 16
  • 37
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    byBuildNumber can't seems to take `prerelease` etc text, and ignores it. If that is true, you should mention that. – resp78 Sep 17 '20 at 04:31
1

Because you specified byEnvVar you just need to give the variable name, when you put it with $() you give the variable value and not the name.

So, just change it to:

versionEnvVar: 'nugetVersion'
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • I tried that too, I get the compilation error `error MSB4018: The "GetPackOutputItemsTask" task failed unexpectedly. error MSB4018: System.ArgumentException: PackageVersion string specified '0.1.2-prerelease.$(Date:yyMM)$(DayOfMonth)$(Rev:rr)' is invalid. ` – resp78 Sep 15 '20 at 11:41
  • in the `nugetVersion` variable put the value: `$(build.buildnumber)` (the `name`) – Shayki Abramczyk Sep 15 '20 at 11:58
  • BTW - if the `nugetVersion` should be like the `name` you can give him `byBuildNumber` instead of `byEnvVar` – Shayki Abramczyk Sep 15 '20 at 12:00
  • Yesterday I tried `$(build.buildnumber)`, it works. However, `byBuildNumber` did not i think that was failing silently. – resp78 Sep 15 '20 at 23:34
  • Thanks so much! For others struggling, here's my full solution I used to get it working: https://stackoverflow.com/questions/54718866/azure-pipeline-nuget-package-versioning-scheme-how-to-get-1-0-revr#71241200 – SharpC Feb 24 '22 at 08:29