1
votes

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

1
I don't see this variable introduced in variables part of yaml. Have you tried it?Grzegorz Ochlik
According to the docs set variables are available to downstream steps within the same job: docs.microsoft.com/en-us/azure/devops/pipelines/process/… Nevertheless I tried to set the variable like proposed and it still contains no text in the npm stepTylMH
docs.microsoft.com/en-us/azure/devops/pipelines/process/… "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

1 Answers

3
votes

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