gpt4 book ai didi

azure - 有没有办法循环 Azure DevOps 中 stageDependency 的所有输出?

转载 作者:行者123 更新时间:2023-12-03 03:37:01 30 4
gpt4 key购买 nike

我想循环遍历依赖阶段的所有输出,并检查其中是否有任何输出被设置为表明存在问题的特定值。这可能吗?如果可能的话,如何实现?

除了在工作条件中使用它们之外,我无法找到有关 stageDependency 的任何相关文档。我知道下面的代码不起作用,但我尝试了一些变体,但我什至没有看到 stageDependency 对象。

- task: PowerShell@2
displayName: Check for Failures
inputs:
targetType: inline
script: |
foreach ($dependency in $[stageDependencies]) {
foreach ($job in $dependency) {
foreach ($output in $job.outputs) {
if ("$output" -eq "Started") {
write-host "##vso[task.logissue type=error]Task failed in a previous step"
}
}
}
}

最佳答案

管道可以通过logging命令输出变量,但变量只能是字符串:

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-variables-in-scripts

All variables set by this method are treated as strings.

解析字符串并放入数组并不难,只需使用字符串类型自带的函数(split())进行分割和恢复即可。

这是一个例子:

trigger:
- none

# 1
stages:
- stage: s1
displayName: setvars
jobs:
- job: testJob
steps:
- task: PowerShell@2
name: setvar
inputs:
targetType: 'inline'
script: |
# logic here. For example you get the vars and put it into this format:
$testvars = "testvar1,testvar2,testvar3"
Write-Host "##vso[task.setvariable variable=outputvars;isOutput=true]$testvars"


# 2
- stage: s2
displayName: getvars
dependsOn: s1
variables:
vars: $[ stageDependencies.s1.testJob.outputs['setvar.outputvars'] ]
jobs:
- job:
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$varsArr = "$(vars)".Split(',')
foreach ($var in $varsArr)
{
Write-Host "$var`r`n"
if($var -eq 'testvar1')
{
Write-Host 'The value is correct.'
}
}

结果:

enter image description here

关于azure - 有没有办法循环 Azure DevOps 中 stageDependency 的所有输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72957248/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com