我有一个Azure DevOps管道,一个阶段依赖于另外两个阶段。如果以下两个条件都成立,则此阶段应有条件地运行-
这是我正在使用的条件。但是,它不起作用,并且在我希望它运行时会跳过该阶段。检查输出变量值的语法来自documentation。
condition: >-
and(
in(dependencies.Test_BVT.result, 'Skipped', 'Failed'),
eq(dependencies.Deploy_BVT_UKS_Internal.outputs['Swap_Slots.Output_Slot_Swap_Success_Variable.slotSwapped'], 'true')
)
为确认,我正在格式化输出变量检查,如下所示-
dependencies.<stageName>.outputs['<jobName>.<taskName>.<variableName>']
如果我仅使用in(dependencies.Test_BVT.result, 'Skipped', 'Failed')
作为条件,那么如果Test_BVT
阶段被跳过或失败,则该阶段运行。因此,这表明输出变量检查是问题所在,尽管由于日志似乎不再显示如何评估条件,所以我不能这样做。
令人困惑的同一个documentation显示了以两种不同方式格式化的阶段依赖关系,因此我添加了一个任务,以使用两种格式写入输出变量的值。使用stageDependencies
的格式会产生预期的结果,证明已按预期设置了输出变量。但是,使用dependencies
的格式不起作用。
- job: test
variables:
one: $[dependencies.Deploy_BVT_UKS_Internal.outputs['Swap_Slots.Output_Slot_Swap_Success_Variable.slotSwapped']]
two: $[stageDependencies.Deploy_BVT_UKS_Internal.Swap_Slots.outputs['Output_Slot_Swap_Success_Variable.slotSwapped']]
steps:
- script: |
echo $(one)
echo $(two)
displayName: Output SlotSwapped Value
我确实在情况下尝试使用stageDependencies
格式,以防万一,但这也失败了。
我在这里遗漏了一些东西吗,还是只是没有按照记录工作?
答案 0 :(得分:1)
这全部取决于您要在上一个阶段使用依赖项的上下文:
dependencies
中使用语法,如下所示:stages:
- stage: A
jobs:
- job: A1
steps:
- bash: echo "##vso[task.setvariable variable=shouldrun;isOutput=true]true"
# or on Windows:
# - script: echo ##vso[task.setvariable variable=shouldrun;isOutput=true]true
name: printvar
- stage: B
condition: and(succeeded(), eq(dependencies.A.outputs['A1.printvar.shouldrun'], 'true'))
dependsOn: A
jobs:
- job: B1
steps:
- script: echo hello from Stage B
stageDependencies
语法,如下所示:trigger: none
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: A
jobs:
- job: A1
steps:
- bash: echo "##vso[task.setvariable variable=shouldrun;isOutput=true]true"
# or on Windows:
# - script: echo ##vso[task.setvariable variable=shouldrun;isOutput=true]true
name: printvar
- stage: B
dependsOn: A
jobs:
- job: B1
condition: in(stageDependencies.A.A1.result, 'Succeeded', 'SucceededWithIssues', 'Skipped')
steps:
- script: echo hello from Job B1
- job: B2
condition: eq(stageDependencies.A.A1.outputs['printvar.shouldrun'], 'true')
steps:
- script: echo hello from Job B2
如您所提供的链接中所写:
对于工作和阶段,上下文称为
dependencies
,其工作方式非常类似于变量。在作业内部,如果您在另一个阶段引用作业的输出变量,则上下文称为stageDependencies
。
我进行了更多测试,它看起来像个错误。这完全取决于您的状况。请比较
condition: and(eq(dependencies.B.outputs['B1.printvar.shouldrun'], 'true'), in(dependencies.A.result, 'Failed', 'Skipped')) # it works
还有这个
condition: and(in(dependencies.A.result, 'Failed', 'Skipped'), eq(dependencies.B.outputs['B1.printvar.shouldrun'], 'true')) # it doesn't work
它应该提供相同的输出,但不是。
stages:
- stage: A
condition: false
jobs:
- job: A1
steps:
- script: echo Job A1
- stage: B
condition: always()
jobs:
- job: B1
steps:
- bash: echo "##vso[task.setvariable variable=shouldrun;isOutput=true]true"
# or on Windows:
# - script: echo ##vso[task.setvariable variable=shouldrun;isOutput=true]true
name: printvar
- stage: C
condition: and(eq(dependencies.B.outputs['B1.printvar.shouldrun'], 'true'), in(dependencies.A.result, 'Failed', 'Skipped')) # it works
dependsOn:
- A
- B
jobs:
- job: C1
steps:
- script: echo hello from Stage C
- stage: D
condition: and(in(dependencies.A.result, 'Failed', 'Skipped'), eq(dependencies.B.outputs['B1.printvar.shouldrun'], 'true')) # it doesn't work
dependsOn:
- A
- B
jobs:
- job: D1
steps:
- script: echo hello from Stage D