【问题标题】:How to run a script at the end of a job, even if the job was cancelled?即使作业被取消,如何在作业结束时运行脚本?
【发布时间】:2021-07-16 18:55:39
【问题描述】:

这是我目前在 GitHub 上使用的工作流程

name: Windows10 - CI
on: [ push ]
jobs:
  run-test:
    runs-on: [ self-hosted, windows, DO ]
    steps:
      - uses: actions/checkout@v2
        with:
          clean: false

      - name: Run nds2 CI - Sanity Test
        if: github.ref == 'refs/heads/master'
        run: cd c:\actions-runner\_work\nds2\nds2 ; python3 ci_host.py --master

      - name: Run nds2 CI - Build Installer
        if: github.ref != 'refs/heads/master'
        run: cd c:\actions-runner\_work\nds2\nds2 ; python3 ci_host.py

我有一台 Windows 10 计算机,它使用 GitHub 运行程序侦听传入的作业。 在传入作业时,如果对主分支进行推送,则脚本 ci_host.py 将使用“--master”标志运行,该标志会启动 VM 并在其上运行多个测试。最终,在测试结束时,脚本会将 VM 恢复到预先配置的快照。

所以基本上我想要实现的是,当通过 GitHub 操作 Web 界面取消作业时,处理测试的脚本在作业中被取消并且没有机会恢复 VM恢复到之前的干净状态(快照)。

即使作业被取消,如何运行将在工作流结束时运行的脚本? 因此,无论发生什么情况,我的虚拟机都可以恢复到干净状态

提前感谢您的帮助:)

【问题讨论】:

标签: github continuous-integration yaml workflow github-actions


【解决方案1】:

您可以使用Job status check functions 来执行一个步骤,具体取决于它之前的工作(或没有)发生了什么:

  • success:当前面的步骤都没有失败或被取消时返回true

  • always:总是返回true,即使被取消。当严重故障阻止任务运行时,作业或步骤将不会运行。例如,如果获取源失败。

  • cancelled:如果工作流被取消,则返回true

  • failure:当作业的任何前一步失败时返回true

使用示例:

steps:
  ...
  - name: Execute if the job succeeded
    if: ${{ success() }}
  
  - name: Execute if the job failed
    if: ${{ failure() }}
  
  - name: Execute if the job was cancelled
    if: ${{ cancelled() }}
  
  - name: Always execute
    if: ${{ always() }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 2011-12-05
    • 2019-04-19
    • 2021-11-25
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多