【问题标题】:Azure Pipelines overwrite pipeline variableAzure Pipelines 覆盖管道变量
【发布时间】:2020-01-25 23:47:12
【问题描述】:
如何覆盖管道变量或如何从作业创建管道变量?
我正在运行prepare 作业,我将当前的 git 标签提取到我在后续作业中需要的变量中,因此我决定创建一个管道变量并在第一个作业中覆盖它的值:
variables:
GIT_TAG: v0.0.1
jobs:
- job: job1
pool:
vmImage: 'ubuntu-16.04'
steps:
- powershell: |
Write-Host "##vso[task.setvariable variable=GIT_TAG]$(git describe --tags --always)"
但是,在下一个作业中,GIT_TAG 的初始值为v0.0.1。
【问题讨论】:
标签:
azure-devops
azure-pipelines
【解决方案1】:
默认情况下,如果您覆盖一个变量,则该值仅适用于他的作业,而不适用于序列作业。
在同一阶段的作业之间传递变量有点复杂,因为它需要使用输出变量。
与上面的示例类似,传递FOO 变量:
- 确保为工作命名,例如
job: firstjob
- 同样,确保您也为该步骤命名,例如:
name: mystep
- 用和之前一样的命令设置变量,但是加上
;isOutput=true,比如:echo "##vso[task.setvariable variable=FOO;isOutput=true]some value"
- 在第二个作业中,在作业级别定义一个变量,将其赋值为
$[ dependencies.firstjob.outputs['mystep.FOO'] ](请记住在表达式中使用单引号)
一个完整的例子:
jobs:
- job: firstjob
pool:
vmImage: 'Ubuntu-16.04'
steps:
# Sets FOO to "some value", then mark it as output variable
- bash: |
FOO="some value"
echo "##vso[task.setvariable variable=FOO;isOutput=true]$FOO"
name: mystep
# Show output variable in the same job
- bash: |
echo "$(mystep.FOO)"
- job: secondjob
# Need to explicitly mark the dependency
dependsOn: firstjob
variables:
# Define the variable FOO from the previous job
# Note the use of single quotes!
FOO: $[ dependencies.firstjob.outputs['mystep.FOO'] ]
pool:
vmImage: 'Ubuntu-16.04'
steps:
# The variable is now available for expansion within the job
- bash: |
echo "$(FOO)"
# To send the variable to the script as environmental variable, it needs to be set in the env dictionary
- bash: |
echo "$FOO"
env:
FOO: $(FOO)
更多信息您可以找到here。