Azure DevOps管道:在上一个任务中更新变量值后读取变量



我的管道中有两个任务,第一个是来自市场的基于powershell的任务。它使用日志记录命令(例如Write-Host '##vso[task.setvariable variable=test]abc'(更新构建变量,该命令在构建运行期间保持不变。你可以在";变量";Azure DevOps中构建的部分。

第二个任务是powershell任务,使用前一个任务中设置的变量的值并使用其值执行操作。它使用powershell环境语法,例如$env:TEST

我遇到的问题是,即使第一个任务正确地更新了变量,powershell任务似乎仍然获得了旧的值(就像第一个任务运行之前一样(。

我需要做什么才能使我的powershell任务正确读取变量的值?

此外,这是我的powershell任务的实际代码(我正试图用AutoVersion任务设置的版本变量标记最新的git提交,正如你可能知道的那样(:

- powershell: |
git tag $env:AUTOVERSION
git push origin $env:AUTOVERSION
workingDirectory: $(Build.SourcesDirectory)
displayName: 'Tag Sources'

好。让我们打开这个。

当使用##vso[task.setvariable...时,您有两个选项来创建变量(本讨论中忘记了秘密(-isOutput=true,而不是。如果是isOutput=true,则会将其添加到作业的输出中(通过作业和步骤名称(。如果不是,它将被设置为同一作业中后续步骤的环境变量。

如果没有isOutput=true,它将不可用于其他作业或阶段。有了而没有它,变量在写入控制台的任务之外才可用(因为变量是通过抓取控制台输出来处理的(。

尝试从以下内容创建一个管道-它展示了如何使用输出与环境变量,如何在同一作业中的步骤之间以及作业之间进行通信:

jobs:
- job: Job1
steps:
- pwsh: |

# isOutput=true makes the variable name accessible via StepName.VarName macro syntax
Write-Host "##vso[task.setvariable variable=SomeVar;isOutput=true]Hello (output), job 1, step 1"
# not using isOutput=true makes the variable accessible from the environment
Write-Host "##vso[task.setvariable variable=SomeEnvVar]Hello (environment), job 1, step 1"
name: Step1
- pwsh: |
Write-Host "##vso[task.setvariable variable=SomeVar;isOutput=true]Hello (output), job 1, step 2"

# This works because Step1.SomeVar has isOutput=true
Write-Host "Job1Step1OutputVar is $(Step1.SomeVar)"

# The next would fail the step because it doesn't have isOutput=true
# Write-Host "Job1Step1EnvVar is $(Step1.SomeEnvVar)"

# This works because SomeEnvVar is not declared isOutput=true
Write-Host "Job1Step1EnvVar is $($Env:SomeEnvVar)"
# This fails silently (step succeeeds, no value) because SomeVar is declared isOutput=true
Write-Host "Job1Step1OutputVarAsEnvVar is $($Env:SomeVar)"
name: Step2
- job: Job2
# Prevent the job from running in parallel with Job1
dependsOn: Job1 
# you have to map other jobs' outputs into your job with variables
variables: 
Job1Step1Var: $[dependencies.Job1.outputs['Step1.SomeVar']]
Job1Step2Var: $[dependencies.Job1.outputs['Step2.SomeVar']]
steps:
- pwsh: |
Write-Host "##vso[task.setvariable variable=SomeVar;isOutput=true]Hello (output), job 2, step 1"
name: Step1
- pwsh: |
Write-Host "##vso[task.setvariable variable=SomeVar;isOutput=true]Hello (output), job 2, step 2"
Write-Host "Job1Step1Var is $(Job1Step1Var)"
Write-Host "Job1Step2Var is $(Job1Step2Var)"
Write-Host "Job2Step1Var is $(Step1.SomeVar)"
name: Step2
- job: Job3
dependsOn: 
# Even though Job1 is a dependency of Job2, we have to declare it here to access its output!
- Job1 
- Job2
# you have to map other jobs' outputs into your job with variables
variables: 
Job1Step1Var: $[dependencies.Job1.outputs['Step1.SomeVar']]
Job1Step2Var: $[dependencies.Job1.outputs['Step2.SomeVar']]
Job2Step1Var: $[dependencies.Job2.outputs['Step1.SomeVar']]
Job2Step2Var: $[dependencies.Job2.outputs['Step2.SomeVar']]
steps:
- pwsh: Write-Host "##vso[task.setvariable variable=SomeVar]Hello (environment), job 3, step 1"
name: Step1
- pwsh: |
Write-Host "Job1Step1Var is $(Job1Step1Var)"
Write-Host "Job1Step2Var is $(Job1Step2Var)"
Write-Host "Job2Step1Var is $(Job2Step1Var)"
Write-Host "Job2Step2Var is $(Job2Step2Var)"
Write-Host "Job3Step1Var is $($Env:SomeVar)"
name: Step2 

相关内容

  • 没有找到相关文章

最新更新