resp78
resp78

Reputation: 1534

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

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'

Upvotes: 3

Views: 5639

Answers (2)

Leo Liu
Leo Liu

Reputation: 76670

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

Upvotes: 6

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

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'

Upvotes: 2

Related Questions