【问题标题】:Azure DevOps Build Pipeline - Unable to access $(Build.BuildNumber) in ScriptAzure DevOps 构建管道 - 无法在脚本中访问 $(Build.BuildNumber)
【发布时间】:2018-10-19 20:35:11
【问题描述】:

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

  1. 从 Git 恢复
  2. Powershell 脚本 - 检索构建 编号并将其写入 json 文件之前....
  3. 构建解决方案
  4. 存档文件
  5. 发布工件。

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

定义的环境变量:

Name: buildNumber  Value: $(Build.BuildNumber)
Name: rootPath Value:$(Build.ArtifactStagingDirectory)

代码:

$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."
}

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

有什么想法吗?

【问题讨论】:

    标签: powershell azure-devops azure-pipelines


    【解决方案1】:

    使用$env:BUILD_BUILDNUMBER 而不是$(...) 表示法。

    请参阅different notations for different script types in the docs

    【讨论】:

    • 哈成功了!!谢谢!!为了澄清......我删除了上述环境变量并直接在脚本中使用了这个符号:$env:BUILD_BUILDNUMBER。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-01-22
    • 2022-01-19
    • 1970-01-01
    • 2022-12-18
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多