via*_*ico 6 conditional-statements azure-devops azure-pipelines multistage-pipeline
我有一个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"
Run Code Online (Sandbox Code Playgroud)
因此,有 2 个阶段,A有一项工作a,B有 2 个工作b和c。我希望仅在作业a执行后才执行作业c 。我尝试通过将作业a中的变量doThing设置为 Yes,然后在作业c中检查此变量来实现此目的。
但我收到一个错误:
阶段计划作业c取决于未知作业a。
变量定义和条件定义取自Azure文档
您对如何使其发挥作用有什么建议吗?
虽然 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)
Run Code Online (Sandbox Code Playgroud)
看起来微软现在提供了一些选项。
来自微软:
在此示例中,无论作业 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
Run Code Online (Sandbox Code Playgroud)
此外,还有另一种选择,您可以跨阶段使用输出变量。
来自微软网站:
阶段还可以使用另一个阶段的输出变量。在此示例中,阶段 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
Run Code Online (Sandbox Code Playgroud)
这是因为你不能依赖另一个阶段的工作,你可以将阶段 B 依赖于阶段 A 或工作 c 依赖于工作 b。
您无法使用 YAML 条件实现您的目标,因为您想使用在第一阶段声明的变量,第二阶段不知道该变量,Azure DevOps 尚不支持它:
目前,您无法指定阶段根据前一阶段中设置的输出变量的值运行。
您可以将阶段 B 依赖于 A,因此,如果在阶段 A 中只有一项工作,则阶段 B 依赖于阶段 A:
- stage: B
dependsOn: A
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18098 次 |
| 最近记录: |