Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Devops - Setting variable in YAML script to datetime

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)?

like image 407
CountZero Avatar asked Mar 05 '23 16:03

CountZero


1 Answers

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"
like image 84
4c74356b41 Avatar answered Mar 28 '23 06:03

4c74356b41