是否可以在构建步骤中设置 VSTS 构建变量,以便可以在后续构建步骤中使用该值?

Is it possible to set an VSTS Build variable in a Build Step so that the value can be used in a subsequent Build Step?

我目前正在使用 Visual Studio Team Services 中的构建(Visual Studio 在线),并且希望能够在构建步骤中设置构建变量,以便新值可以在后续的构建步骤中使用。

显然您可以在构建开始之前设置它,但我希望在后续的构建步骤中延迟绑定变量。

这可能吗?

在脚本内部时,您可以通过在 ps1

中发出以下命令来更新变量
"##vso[task.setvariable variable=testvar;]testvalue"

然后您可以使用 $(testvar)

将变量传递到下一个脚本

来自 API 的文档讨论了您可以使用的 ##vso 命令。

不要忘记将 system.debug 设置为 true。似乎有一个错误使 stdout 静音,因此,所有 ##vso 都不起作用。

https://github.com/Microsoft/vso-agent-tasks/blob/master/docs/authoring/commands.md

您可以创建一个 powershell 脚本并将其作为构建任务引用。 然后在你的 powershell 脚本中添加:

"##vso[task.setvariable variable=key]value"

之后,您可以在所有任务中将变量读取为 $(key)。 如果你想保护你的变量,使用:

"##vso[task.setvariable variable=secretVar;issecret=true]value"

然后在您的下一个任务中将其用作 $(secretVar)。

我发现这个 link 有用:https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=powershell

这里包含您可以执行的操作的完整选项:https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch

您可以在任务之间重用设置变量,也可以在作业之间重用设置变量。我在舞台上找不到任何东西。

总结:

jobs:

# Set an output variable from job A
- job: A
  pool:
    vmImage: 'vs2017-win2016'
  steps:
  - powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
    name: setvarStep
  - script: echo $(setvarStep.myOutputVar)
    name: echovar

# Map the variable into job B
- job: B
  dependsOn: A
  pool:
    vmImage: 'ubuntu-16.04'
  variables:
    myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ]  # map in the variable
                                                                          # remember, expressions require single quotes
  steps:
  - script: echo $(myVarFromJobA)
    name: echovar