0

I am having difficulty getting this to work.

I am extracting major and minor from AssemblyInfo.

I am not able to figure out how to extract the Date and run number.

I am blind because I cannot see the pipeline varables.

Thanks Andy

I have tried to create a variable named packedversion as you suggested.

I added the following task to YAML

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Set packversion number
      
      $packageversion = '$(AssemblyInfo.AssemblyVersion.Major).$(AssemblyInfo.AssemblyVersion.Minor).$(Build.BuildNumber)'
      echo 'packageversion: $packageversion'
      Write-host "##vso[task.setvariable variable=packageversion]$packageversion"
      echo 'Major: $(AssemblyInfo.AssemblyVersion.Major)'
      echo 'Minor: $(AssemblyInfo.AssemblyVersion.Minor)'
      echo 'buildNum: $(Build.BuildNumber)'
      echo 'completed packageversion: $(packageversion)'

The results data echoed from the PowerShell is :

packageversion: $packageversion
Major: 0
Minor: 15
buildNum: 20200722.8
completed packageversion: $(packageversion)

Please point me to a description of what this syntax does

  Write-host "##vso[task.setvariable variable=packageversion]$packageversion"

Then changed the nuget pack to:

- task: NuGetCommand@2
  displayName: 'NuGet pack JRTestLib'
  inputs:
      command: pack
      packagesToPack: JLReyLibrary/JRTestLib/JRTestLib.csproj
      versioningScheme: byEnvVar
      versionEnvVar: $(packageversion)
      includeSymbols: true
      toolPackage: true
  enabled: true

The nuget task says it cannot fine provided environment variable.

Thanks for the help

jl-rey
  • 1
  • 3

2 Answers2

0

I am not able to figure out how to extract the Date and run number.

The simplest way is using the predefined run (build) number variable : $(Build.BuildNumber)

The default value for run number is $(Date:yyyyMMdd).$(Rev:r). Please refer to Configure run or build numbers for details. It already includes the date in the run number. So, the requested scheme can be $(major).$(minor).$(Build.BuildNumber)

Please follow below steps to do that :

  1. We can use the extension Assembly Info Reader to read the assembly attributes from the AssemblyInfo file and makes them available as build variables.

  2. Add a Powershell task in your pipeline to set custom variable $packageversion using logging command :

    $packageversion = "$(AssemblyInfo.AssemblyVersion.Major).$(AssemblyInfo.AssemblyVersion.Minor).$(Build.BuildNumber)"

    Write-Host "##vso[task.setvariable variable=packageversion]$packageversion"

  3. Use the variable packageversionin NuGet pack task: (Pack Options -> Use an environment variable -> packageversion ) enter image description here

  4. Publish to the specific Artifact feed. enter image description here

Update:

Yaml for your reference :

- task: PowerShell@2
  displayName: Set packageversion variable
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "AssemblyVersion:" $(AssemblyInfo.AssemblyVersion)
      Write-Host "Major:" $(AssemblyInfo.AssemblyVersion.Major)
      Write-Host "Minor:" $(AssemblyInfo.AssemblyVersion.Minor)
      Write-Host "BuildNumber:" $(Build.BuildNumber)
      $packageversion = "$(AssemblyInfo.AssemblyVersion.Major).$(AssemblyInfo.AssemblyVersion.Minor).$(Build.BuildNumber)"
      Write-Host "packageversion:" $packageversion
      Write-Host "##vso[task.setvariable variable=packageversion]$packageversion"
- task: NuGetCommand@2
  displayName: NuGet pack
  inputs:
    command: 'pack'
    packagesToPack: '**/*.csproj'
    versioningScheme: 'byEnvVar'
    versionEnvVar: 'packageversion'

The syntax Write-host "##vso[task.setvariable variable=packageversion]$packageversion" is for setting the variable packageversion here. After defined the variables, we can use them in the subsequent tasks within the same session. Reference this thread for details: Azure DevOps - Setting and Using Variables in PowerShell Scripts

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
0

Solved

The final problem was as follows:

The use of nuget pack with the byEnvVar option

The versionEnvVar takes the name of the variable not the variable value.

Note: this is different than the byPrerelease option that accepts values.

I am still having difficulties with defining and seeing variables but that is a different topic.

Thanks for the help

jl-rey
  • 1
  • 3