简体   繁体   English

无法从 powershell 脚本访问构建变量

[英]Can't access build variables from powershell script

I'm trying to use the Build variables in a script.我正在尝试在脚本中使用Build 变量 According to this documentation I should be able to use the following:根据此文档,我应该能够使用以下内容:

Write-Host "BUILD_DATE: $Env:BUILD_DATE"
Write-Host "BUILD_REV: $Env:BUILD_REV"

However, I only get the following output但是,我只得到以下输出

BUILD_DATE:
BUILD_REV:

I've also tried this syntax:我也试过这个语法:

Write-Host "BUILD_DATE: $(Env:BUILD_DATE)"
Write-Host "BUILD_REV: $(Env:BUILD_REV)"

Write-Host "BUILD_DATE: $(Build.Date)"
Write-Host "BUILD_REV: $(Build.Rev)"

But the first segment gives The term 'Env:BUILD_DATE' is not recognized and the second segment gives The term 'Build.Date' is not recognized但是第一段给出了The term 'Env:BUILD_DATE' is not recognized ,第二段给出了The term 'Build.Date' is not recognized

How can I use the build variables in my script?如何在脚本中使用构建变量?

Disclaimer: I know virtually nothing about Azure pipelines, so my answer is based on reading the docs.免责声明:我对 Azure 管道几乎一无所知,所以我的回答是基于阅读文档。 Do let us know if I got things wrong.如果我做错了,请告诉我们。

Your first command uses the correct syntax for referencing environment variables in PowerShell (also inside an expandable (double-quoted) string).您的第一个命令使用正确的语法在 PowerShell 中引用环境变量(也在可扩展(双引号)字符串中)。
(The other commands, based on subexpression operator $(...) , mistakenly try to execute commands named Env:BUILD_DAT , ... rather than referencing variables.) (其他命令,基于子表达式运算符$(...) ,错误地尝试执行名为Env:BUILD_DAT命令,...而不是引用变量。)

Your problem seems to be that the targeted environment variables do not exist .您的问题似乎是目标环境变量不存在

The list of predefined variables that are exposed as environment variables does not contain variables named Build.Date / $env:BUILD_DATE and Build.Rev / $env:BUILD_REV . 作为环境变量公开的预定义变量列表包含名为Build.Date / $env:BUILD_DATEBuild.Rev / $env:BUILD_REV

By contrast, variables named Date and Rev seemingly do exist - as you state, they are used in the default format definition for the Build.BuildNumber / $Env:BUILD_BUILDNUMBER build variable , $(Date:yyyyMMdd)$(Rev:.r) - but are seemingly of a different kind not exposed as env.相比之下,名为DateRev变量似乎确实存在 - 正如您所说,它们用于Build.BuildNumber / $Env:BUILD_BUILDNUMBER构建变量$(Date:yyyyMMdd)$(Rev:.r) Build.BuildNumber的默认格式定义中- 但似乎是另一种没有作为 env 暴露的。 vars.变量。 (unlike Build.BuildNumber / $Env:BUILD_BUILDNUMBER itself, which is exposed). (不像Build.BuildNumber / $Env:BUILD_BUILDNUMBER本身,它暴露)。

(I don't know where these variables are defined or how they are classified, and where this is documented - do tell us if you know.) (我不知道这些变量在哪里定义或它们是如何分类的,以及记录在哪里 - 如果你知道,请告诉我们。)

A quick workaround would be to split the value of $Env:BUILD_BUILDNUMBER into its constituent parts:一个快速的解决方法是$Env:BUILD_BUILDNUMBER的值拆分为其组成部分:

# Split the build number into date and revision, by "."
$date, $rev = $Env:BUILD_BUILDNUMBER -split '\.'

"BUILD_DATE: $date"
"BUILD_REV: $rev"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM