4
votes

I'm trying to generate a name for a NuGet package in an Azure DevOps YAML pipeline.

The context is creating packages on each automated build with the name of the package, the name of the branch, the date and the incrementing revision number.

packageName-branchName-year-month-day-revision

This way when a new feature branch is created it will generate a unique package which can be used for testing in projects which need it.

I'm struggling to set a variable in the YAML file from environmental variables such as the date or revision number.

Using the ##vso command I'm able to set 'myVariable' to hello and print it out.

- script: |
    echo '##vso[task.setvariable variable=myVariable]hello'

- script: |
    echo my variable is $(myVariable)

When I try setting the variable from PowerShell as below I get the following error '#$dateStr' is not recognized as an internal or external command'.

# Create a variable
- script: |
    #$dateStr = (Get-Date).ToString('yyyy-MM-dd') 
    echo '##vso[task.setvariable variable=myVariable]#$dateStr'

# Print the variable
- script: |
    echo my variable is $(myVariable) 

When I try to set a variable in the variables section of the YAML file as so.

variables:
  solution: '**/*.sln'
  foo: $(Date:yyyyMMdd)

- script: |
    echo my variable is $(foo)

The variable is not interpolated and it outputs as.

'my variable is $(Date:yyyyMMdd)'

How do I create variables based on environmental variables such as $(rev) and $(Date)?

3

3 Answers

4
votes

i dont think there is a built-in date variable, but for the powershell case you just need to drop # before variable and it has to be enclosed with " else powershell wont expand your variable

echo "##vso[task.setvariable variable=myVariable]$dateStr"
2
votes

Thanks for the help.

This is how I solved the issue in the end. With non release branches I use build# + beta + branch name to generate a unique name for the nuget packages I'm creating. With release branches I just use the build #.

# Create version number for non release package(s) - 1.0.xxx-beta-develop for example
- powershell: |
    [String]$buildNumber = $Env:BUILD_BUILDNUMBER.Substring($Env:BUILD_BUILDNUMBER.LastIndexOf('.') + 1)
    [String]$branchName = $Env:BUILD_BUILDNUMBER.Substring(0, $Env:BUILD_BUILDNUMBER.LastIndexOf('.'))
    Write-Host "##vso[task.setvariable variable=nugetVersion]1.0.$($buildNumber)-beta-$($branchName)"
  displayName: 'Create beta version number for production nuget packages - run when in non release branch' 
  condition: and(succeeded(), ne(variables['Build.SourceBranch'], 'release/'))

# Create version number for release package(s) - 1.0.xxx for example
- powershell: |
    [String]$buildNumber = $Env:BUILD_BUILDNUMBER.Substring($Env:BUILD_BUILDNUMBER.LastIndexOf('.') + 1)
    Write-Host "##vso[task.setvariable variable=nugetVersion]1.0.' + $($buildNumber)
  displayName: 'Create version number for production nuget packages - run when in release branch' 
  condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'release/'))


- task: NuGetCommand@2
  inputs:
    command: 'pack' # Options: restore, pack, push, custom
    versioningScheme: byEnvVar
    versionEnvVar: nugetVersion
    packDestination: '$(Build.ArtifactStagingDirectory)\nuget'
    packagesToPack: '**/Floww.*.nuspec'     
1
votes

using the -script is great for running commands that can be executed cross-platform like npm i

As the date is not a global variable you will need to create it. The issue is that depending on what build agent you are running on (window|mac|linx) you will have different ways to set that variable, see the documentation...

The following will allow you to use a $d variable like other variables

 steps:
    - bash: |
        export d=$(date +%F)
        echo "The Date is $d"
        echo "##vso[task.setvariable variable=fileName]$d"
      condition: eq( variables['Agent.OS'], 'Linux' )
    - powershell: |
        Set-Variable -Name d -Value (Get-Date).ToString('yyyy-MM-dd')
        Write-Host "##vso[task.setvariable variable=fileName]$d"
      condition: eq( variables['Agent.OS'], 'Windows_NT' )