从 Azure Pipeline .yaml 干净退出? [英] Clean Exit from an Azure Pipeline .yaml?
问题描述
是否有比让脚本抛出错误更好/更干净/更惯用的方法来退出基于 .yaml 的 Azure Pipeline?
Is there a better/cleaner/more idiomatic way to exit a .yaml-based Azure Pipeline than say having a script throw an error?
例如,这可行,但感觉很笨重:
e.g., this works but it feels clunky:
- task: PowerShell@2
displayName: "Exit"
inputs:
targetType: 'inline'
script: |
throw 'Exiting';
推荐答案
- powershell: |
write-host "##vso[task.complete result=Failed;]The reason you quit"
会更整洁,但仍会失败.
Would be neater, but would still fail the job.
没有等同于跳过剩余的工作,除非你有条件根据变量值跳过所有未来的任务:
There is no equivalent to skip the rest of the job, unless you work with conditions to skip all future tasks based on a variable value:
variables:
skiprest: false
- powershell: |
write-host "##vso[task.setvariable variable=skiprest]true"
- powershell:
condition: and(succeeded(), eq(skiprest, 'false'))
- powershell:
condition: and(succeeded(), eq(skiprest, 'false'))
- powershell:
condition: and(succeeded(), eq(skiprest, 'false'))
- powershell:
condition: and(succeeded(), eq(skiprest, 'false'))
您可以使用 YAML 迭代插入 从模板中应用该条件到我认为的工作中的所有任务.我手头没有工作示例,但文档显示了如何注入 dependsOn:
,我想这个技巧非常相似:
You could use a YAML iterative insertion from a template to apply that condition to all tasks in a job I suppose. I don't have a working sample at hand, but the docs show how to inject a dependsOn:
, the trick would be very similar I suppose:
# job.yml
parameters:
- name: 'jobs'
type: jobList
default: []
jobs:
- job: SomeSpecialTool # Run your special tool in its own job first
steps:
- task: RunSpecialTool@1
- ${{ each job in parameters.jobs }}: # Then do each job
- ${{ each pair in job }}: # Insert all properties other than "dependsOn"
${{ if ne(pair.key, 'dependsOn') }}:
${{ pair.key }}: ${{ pair.value }}
dependsOn: # Inject dependency
- SomeSpecialTool
- ${{ if job.dependsOn }}:
- ${{ job.dependsOn }}
这篇关于从 Azure Pipeline .yaml 干净退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!