1

我有一个pipeline.yaml看起来像这样的

pool:
  vmImage: image

stages:
  -stage: A
   jobs: 
     -job: a
      steps: 
     - script: |
          echo "This is stage build"
          echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes"
        name: BuildStageRun
  -stage: B
   jobs:
      -job: b
       steps: #do something in steps

      -job: c
       dependsOn: a
       condition: eq(dependencies.build.outputs['BuildStageRun.doThing'], 'Yes')
       steps:
        - script: echo "I am scripting" 

因此,有 2 个阶段,A有一个工作aB有 2 个工作bc。我希望仅在作业a执行后才执行作业c。我试图通过将作业a中的变量doThing设置为 Yes 来做到这一点,然后在作业c中检查此变量。

但我收到一个错误:

阶段计划作业c取决于未知作业a

变量定义和条件定义取自Azure 文档

你对如何让它工作有什么建议吗?

4

3 回答 3

4

虽然 Shayki 是正确的,但它不受支持 - 我目前正在使用一种解决方法。我在这个博客的帮助下使用了https://medium.com/microsoftazure/how-to-pass-variables-in-azure-pipelines-yaml-tasks-5c81c5d31763

基本上,您可以正常创建输出,然后将变量发布为管道工件。在下一阶段,您阅读第一份工作中的工件,并使用它来构建您的条件,例如

stages:

  - stage: firststage
    jobs:

      - job: setup_variables
        pool:
          vmImage: 'Ubuntu-16.04'
        steps:

          - powershell: |
              $ShouldBuildThing1 = $true
              # Write to normal output for other jobs in this stage
              Write-Output "##vso[task.setvariable variable=BuildThing1;isOutput=true]$ShouldBuildThing1"
              # Write to file to publish later
              mkdir -p $(PipelineWorkspace)/variables
              Write-Output "$ShouldBuildThing1" > $PipelineWorkspace/variables/BuildThing1
            name: variablesStep

          # Publish the folder as pipeline artifact
          - publish: $(Pipeline.Workspace)/variables
            artifact: VariablesArtifact

       - job: build_something
         pool:
          vmImage: 'Ubuntu-16.04'
         dependsOn: setup_variables
         condition: eq(dependencies.setup_variables.outputs['variablesStep.BuildThing1'], 'true')
         variables:
           BuildThing1: $[dependencies.setup_variables.outputs['variablesStep.BuildThing1']]
         steps:
           - powershell: |
               Write-Host "Look at me I can Read $env:BuildThing1"

           - somethingElse:
               someInputArgs: $(BuildThing1)


  - stage: secondstage
    jobs:

      - job: read_variables
        pool:
          vmImage: 'Ubuntu-16.04'
        steps:
          # If you download all artifacts then foldername will be same as artifact name under $(Pipeline.Workspace). Artifacts are also auto downloaded on deployment jobs.
          - task: DownloadPipelineArtifact@2
            inputs:
              artifact: "VariablesArtifact"
              path: $(Pipeline.Workspace)/VariablesArtifact

          - powershell: |
              $ShouldBuildThing1 = $(Get-Content $(Pipeline.Workspace)/VariablesArtifact/BuildThing1)
              Write-Output "##vso[task.setvariable variable=BuildThing1;isOutput=true]$ShouldBuildThing1"
            name: variablesStep

      - job: secondjob
        pool:
          vmImage: 'Ubuntu-16.04'
        dependsOn: read_variables
        condition: eq(dependencies.read_variables.outputs['variablesStep.BuildThing1'], 'true')
        variables:
           BuildThing1: $[dependencies.setup_variables.outputs['variablesStep.BuildThing1']]
         steps:
           - powershell: |
               Write-Host "Look at me I can Read $env:BuildThing1"

           - somethingElse:
               someInputArgs: $(BuildThing1)

于 2020-05-29T03:51:26.330 回答
2

这是因为您不能依赖另一个阶段的作业,您可以将阶段 B 依赖于阶段 A 或作业 c 依赖于作业 b。

您无法使用 YAML 条件实现目标,因为您想使用在第一阶段声明的变量,第二阶段不知道该变量,Azure DevOps 还不支持它

您当前无法指定基于前一阶段中设置的输出变量的值运行的阶段。

您可以将阶段 B 依赖于 A,因此如果在阶段 A 只有一项工作,您将阶段 B 依赖于阶段 A:

- stage: B
  dependsOn: A
于 2020-02-12T14:06:15.717 回答
1

看起来微软现在提供了一些选项。

首先是跨阶段的工作对工作的依赖关系

来自微软:

在此示例中,无论作业 A1 成功还是跳过,作业 B1 都将运行。作业 B2 将检查作业 A1 的输出变量的值,以确定它是否应该运行。

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

此外,还有另一种选择,您可以跨阶段使用输出变量

来自微软网站:

阶段也可以使用来自另一个阶段的输出变量。在此示例中,阶段 B 依赖于阶段 A 中的变量。

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
于 2021-07-29T19:31:09.823 回答