4

I am stuck at using build variables in azure devops pipelines.

What I try to achieve: Create variable with the current timestamp and use this variable to set the build name and the artifact version (for traceability).

In my current configuration the powershell script is executed successfully but the variable foo is empty in the npm step (see yml code below).

variables:
  system.debug: true

name: $(TeamProject)_$(Build.DefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)-$(Hours)$(Minutes)$(Seconds)

[...]

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Write-Host "Setting up the date time for build variable"
      $date=$(Get-Date -format yyyyMMdd-Hmmss)
      Write-Host "##vso[task.setvariable variable=foo]$date"'

- task: Npm@1
  inputs:
    command: 'custom'
    customCommand: '--no-git-tag-version version prerelease --preid=dev-$(foo)'
  displayName: 'npm version prerelease'

My questions: Why is the variable foo (introduced with powershell) empty in the npm step? Is there a way to set the build name with the self introduced variable foo (to use the same timestamp for build name and artifact version)?

riQQ
  • 9,878
  • 7
  • 49
  • 66
TylMH
  • 55
  • 1
  • 2
  • 7
  • I don't see this variable introduced in `variables` part of yaml. Have you tried it? – Karolina Ochlik Aug 17 '20 at 12:10
  • According to the docs set variables are available to downstream steps within the same job: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch Nevertheless I tried to set the variable like proposed and it still contains no text in the npm step – TylMH Aug 17 '20 at 12:56
  • https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-variables-in-scripts "To set a variable from a script, you use the task.setvariable logging command. This doesn't update the environment variables, but it does make the new variable available to downstream steps within the same job." – TylMH Aug 17 '20 at 13:02

2 Answers2

8

You are using the wrong format of your YAML pipeline. You could use below snippet:

steps:
- powershell: |
   Write-Host "Setting up the date time for build variable"
   $date=$(Get-Date -format yyyyMMdd-Hmmss)
   Write-Host "##vso[task.setvariable variable=foo]$date"
  displayName: 'PowerShell Script'

Then this foo variable should introduced with powershell succeed. You will see it expand in follow npm task.

enter image description here

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • Thanks for your help, it works now! I originally used the Azure predefined powershell tasks from the menu. Good to know that those tasks generate a wrong format ... – TylMH Aug 18 '20 at 12:52
  • Possibly the only problem with the original script was that they were missing the `|` multiline indicator. `- task: PowerShell@2` and `- powershell:` should do the same thing (with slightly different parameter names) – Nick.Mc Mar 03 '22 at 06:40
  • 1
    Thanks for your help. In 2022 this still works. By pasting this code in your yml file, you can use $(foo) elsewhere in your build definition. Make sure you run it on a Windows VM though. – yesman Jun 20 '22 at 08:20
0

the solution for Linux as test agent

- script: |
    echo "Setting up the date time for build variable"
    date=$(date +"%Y%m%d-%H%M%S")
    echo "##vso[task.setvariable variable=foo;]$date"
  displayName: set up timestamp variable
Reed_Xia
  • 1,300
  • 3
  • 17
  • 29