【问题标题】:Clean Exit from an Azure Pipeline .yaml?从 Azure Pipeline .yaml 清除退出?
【发布时间】:2021-03-19 05:10:50
【问题描述】:

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

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

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

【问题讨论】:

    标签: yaml azure-pipelines exit azure-pipelines-yaml


    【解决方案1】:
    - 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'))
    

    您可以使用模板中的YAML iterative insertion 将该条件应用于我认为的作业中的所有任务。我手头没有工作样本,但文档显示了如何注入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 }}
    

    【讨论】:

    • 这太好了,谢谢。工作失败我很好。出于好奇,result=Succeeded 不会成功阻止管道吗?看着docs.microsoft.com/en-us/azure/devops/pipelines/scripts/…##vso[task.complete result=Succeeded;]DONE
    • 不,它只是将“任务”标记为成功。就这样继续工作,好像什么事都没有。
    • 我真的很喜欢在 azure pipelines yaml 中工作,因为它的范围不是很大,但它仍然有很多意想不到的变化。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2020-06-16
    • 2019-10-27
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多