Azure DevOps构建管道-无法通过脚本访问$(Build.BuildNumber)

Kic*_*Mhl 3 powershell azure-devops azure-pipelines

在我的构建管道中,我正在执行以下操作:

  1. 从Git还原
  2. Powershell脚本-检索内部版本号,然后将其写入json文件。
  3. 建立解决方案
  4. 存档文件
  5. 发布工件。

在步骤2中,Powershell脚本非常简单:

定义的环境变量:

Name: buildNumber  Value: $(Build.BuildNumber)
Name: rootPath Value:$(Build.ArtifactStagingDirectory)
Run Code Online (Sandbox Code Playgroud)

码:

$theFile = Get-ChildItem -Path $rootPath -Recurse -Filter "host.json" | Select-Object -First 1
$propertyName = "BuildNumber"

if($theFile)
{
    $json = Get-Content "$theFile" | Out-String | ConvertFrom-Json
    if($json.$propertyName)
    { 
        $json.$propertyName = $buildNumber
    }else{    
        Add-Member -InputObject $json -MemberType NoteProperty -Name $propertyName -Value $buildNumber
    }
    $json | ConvertTo-Json -depth 100 | Out-File "$theFile"

}
else
{
    Write-Warning "Found no files."
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我的$ buildNumber返回空值。$ rootPath正在工作。我无法在构建步骤之外访问$(Build.BuildNumber)吗?内部版本号格式在“管道的选项”中定义,在标记内部版本时可以很好地工作,但是我无法在Powershell脚本中访问它。

有什么想法吗?

jes*_*ing 5

使用$env:BUILD_BUILDNUMBER代替$(...)符号。

请参阅文档中针对不同脚本类型不同符号

  • 哈哈!谢谢!!为了澄清...。我删除了上述环境变量,并在脚本中直接使用了这种表示法:$ env:BUILD_BUILDNUMBER。再次感谢! (2认同)