在azure devops管道中使用时间戳变量

Tyl*_*lMH 4 powershell azure-pipelines azure-devops-server

我一直坚持在 azure Devops 管道中使用构建变量。

我试图实现的目标:使用当前时间戳创建变量,并使用此变量设置构建名称和工件版本(用于可追溯性)。

在我当前的配置中,powershell 脚本已成功执行,但变量 foo 在 npm 步骤中为空(请参阅下面的 yml 代码)。

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'
Run Code Online (Sandbox Code Playgroud)

我的问题:为什么 npm 步骤中的变量 foo (通过 powershell 引入)为空?有没有办法使用自我引入的变量 foo 设置构建名称(对构建名称和工件版本使用相同的时间戳)?

Pat*_*SFT 8

您使用的 YAML 管道格式错误。您可以使用下面的代码片段:

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'
Run Code Online (Sandbox Code Playgroud)

然后这个 foo 变量应该通过 powershell 成功引入。您将看到它在后续 npm 任务中展开。

在此输入图像描述