如何在Github Actions步骤中强制退出 [英] How to force to exit in Github Actions step

查看:186
本文介绍了如何在Github Actions步骤中强制退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果满足特定条件,我想退出工作:

I want to exit job if a specific condition is met:

jobs:
  foo:
    steps:
      ...
      - name: Early exit
        run: exit_with_success # I want to know what command I should write here
        if: true
      - run: foo
      - run: ...
 ...

该怎么做?

推荐答案

当前无法任意退出作业,但是可以使用条件:

There is currently no way to exit a job arbitrarily, but there is a way to allow skipping subsequent steps if an earlier step failed, by using conditionals:

jobs:
  foo:
    steps:
      ...
      - name: Early exit
        run: exit_with_success # I want to know what command I should write here
      - if: failure()
        run: foo
      - if: failure()
        run: ...
 ...

这里的想法是,如果第一步失败,那么其余部分将运行,但是如果第一步没有失败,则其余部分将不运行.

The idea here is that if the first step fails, then the rest will run, but if the first step doesn't fail the rest will not run.

但是,需要注意的是,如果后续步骤中的任何一个失败,则后续步骤仍将继续运行,这可能是不希望的.

However, it comes with the caveat that if any of the subsequent steps fail, the steps following them will still run, which may or may not be desirable.

另一种选择是使用

Another option is to use step outputs to indicate failure or success:

jobs:
  foo:
    steps:
      ...
      - id: s1
        name: Early exit
        run: # exit_with_success
      - id: s2
        if: steps.s1.conclusion == 'failure'
        run: foo
      - id: s3
        if: steps.s2.conclusion == 'success'
        run: ...
 ...

此方法效果很好,可让您非常精细地控制允许运行哪些步骤以及何时运行,但是变得非常冗长.

This method works pretty well and gives you very granular control over which steps are allowed to run and when, however it became very verbose.

另一种选择是拥有两个工作:

Yet another option is to have two jobs:

  • 一个检查您状况的人
  • 另一个依赖它的东西:
jobs:
  check:
    outputs:
      status: ${{ steps.early.conclusion }}
    steps:
      - id: early
        name: Early exit
        run: # exit_with_success
  work:
    needs: check
    if: needs.check.outputs.status == 'success'
    steps:
      - run: foo
      - run: ...
 ...

通过将支票移到单独的作业中并让另一个作业等待并检查状态,后一种方法效果很好.但是,如果您有更多的工作,则必须在每一项中重复相同的检查.与在每个步骤中进行检查相比,这还不错.

This last method works very well by moving the check into a separate job and having another job wait and check the status. However, if you have more jobs, then you have to repeat the same check in each one. This is not too bad as compared to doing a check in each step.

这篇关于如何在Github Actions步骤中强制退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆