有没有一种方法可以在Azure DevOps中循环所有来自stageprocedencies的输出?

drkbr07n  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(96)

我想循环所有相关阶段的输出,并检查是否有任何一个被设置为一个特定的值,这将表明有问题。这是可能的,如果是这样,怎么做?
除了在工作环境中使用stageDependencies之外,我很难找到任何有关stageDependencies的相关文档。我知道下面的代码不起作用,但我已经尝试了一些变体,我甚至没有看到stageDependencies对象。

- 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"
              }
            }
          }
        }

字符串

xwbd5t1u

xwbd5t1u1#

还需要从前面的阶段中获取所有变量的列表,结果通过在管道中将stageDependencies转换为JSON,然后在Powershell中返回到对象来解决问题。
下面是一个流水线的例子,它在第一阶段设置了两个变量,并在第二阶段通过初始变量名someVariableanother.variable.with.dots读取它们,而不需要使用阶段/作业/步骤名称。

trigger:
  - master

stages:
  - stage: SetVars
    jobs:
      - job: SetVarsJob
        steps:
          - script: echo "##vso[task.setvariable variable=someVariable;isOutput=true]firstVarValue"
          - script: echo "##vso[task.setvariable variable=another.variable.with.dots;isOutput=true]secondVarValue"
  - stage: ReadVars
    dependsOn: SetVars
    jobs:
      - job: ReadVars
        variables:
          stageDependenciesSerialized: $[convertToJson(stageDependencies)]
        steps:
          - powershell: |
              # extract only initial variables as hashtable
              function ExtractHashtableByVariableNames {
                  param ( [Parameter(Position=0)] [string] $stageDependenciesJson )

                  $result = @{}
                  $stageDependencies = $stageDependenciesJson | ConvertFrom-Json
                  foreach($stage in $stageDependencies.psobject.Properties) {
                      foreach($job in $stage.Value.psobject.Properties) {
                          foreach($step in $job.Value.outputs.psobject.Properties) {
                              $varName = $step.Name.Substring($step.Name.IndexOf('.') + 1)
                              $result[$varName] = $step.Value
                          }
                      }
                  }
                  return $result
              }
              $myHashtable = ExtractHashtableByVariableNames '$(stageDependenciesSerialized)'
              Write-Host $myHashtable["someVariable"]
              Write-Host $myHashtable["another.variable.with.dots"]

字符串
在它打印的结果中

firstVarValue
secondVarValue

svujldwt

svujldwt2#

管道可以通过logging命令输出变量,但变量只能是字符串:
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-variables-in-scripts
此方法设置的所有变量都被视为字符串。
解析字符串并将其放入数组并不难,只需使用字符串类型附带的函数(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.'
              }
            }

字符串
测试结果:


的数据

相关问题