从 Azure Pipeline .yaml 干净退出?

2024-02-24

有没有比让脚本抛出错误更好/更干净/更惯用的方法来退出基于 .yaml 的 Azure Pipeline?

例如,这可行,但感觉很笨拙:

- task: PowerShell@2
  displayName: "Exit"
  inputs:
    targetType: 'inline'
    script: |
      throw 'Exiting';

- powershell: |
  write-host "##vso[task.complete result=Failed;]The reason you quit"

会更整洁,但仍然会失败。

没有等同于跳过作业的其余部分的方法,除非您使用条件来根据变量值跳过所有未来的任务:

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'))

你可以使用从模板中将该条件应用于我认为工作中的所有任务。我手头没有工作示例,但文档显示了如何注入dependsOn:,我想这个技巧非常相似:

# 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 }}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Azure Pipeline .yaml 干净退出? 的相关文章

随机推荐