在Github Actions中重试失败的作业

15

我正在尝试使用GitHub Actions进行CI测试,目前我的测试工作流程如下:

name: test

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: npm install
      - name: test
        run: |
          npm run lint
          npm test
        env:
          CI: true

.github/workflows/test.yml

一切都运作良好,除了当测试失败时,我希望可以重试test步骤(或整个任务)一次。

基本上,我需要的是与travis-retry相同的行为:

script:
  - npm run lint
  - travis_retry npm test

或使用Gitlab CI:

test:
  stage: test
  retry: 1
  script:
    - npm run lint
    - npm test

不确定是否有一种方法或合理简单的解决方法

6个回答

11

对于您特定的用例,只需执行:

npm test || npm test

3

这应该是被接受的答案,因为它是一个通用解决方案,不依赖于代码。 - Amin Ya

2
我认为你需要在代码层面上管理重试逻辑。也就是说,实现/集成这样的机制来处理并仅执行失败的测试。我担心简单地想要重试测试步骤(或整个作业)一次,如果测试失败,将会执行所有测试,甚至可能覆盖第一次运行的输出,如报告和日志。根据我的经验,我使用了一个包装器(shell)脚本。以下是如何实现的:
#!/usr/bin/env bash
{ # try
    # your npm test command here, that saves as output -  failed tests
} || { # catch
    # retry failed tests
      if [ -f ./rerun-failed-file ]; then
        echo "============= [WARN] Rerun file found! =============="
        echo "============= [WARN] Rerunning FAILED tests mode. =============="
        # run npm test command that picks up the failed tests & aggregate test artifacts
      fi
} 

1
请注意,您可以使用GitHub CLI gh从命令行重新运行失败的作业。
自gh 2.6.0以来:
重新运行失败的作业已经得到改进,gh run rerun增加了两个新标志:
- --failed - --job@cdb (Cameron Booth)添加。
现在,您可以有选择地重新运行给定工作流运行中失败的作业!
运行gh run rerun --failed会提示您选择要处理的运行。
这是从 Web GUI 支持的:

通过 GitHub Actions 的部分重新运行节省时间

现在可以仅重新运行失败的作业或单个作业。

如果工作流程运行中存在失败的作业,则现在会看到一个新的下拉菜单,您可以选择“重新运行失败的作业”,除了现有的“重新运行所有作业”。

对于已完成的运行,侧边栏中列出的每个作业都有一个重新运行图标,当您将鼠标悬停在其上时。还可以直接从日志视图重新运行作业。

re-reun this job icon -- https://github.blog/wp-content/uploads/2022/03/partial-reruns-github-actions-3.png


0

请查看wretry.action

该操作可以重试单个Github操作或shell命令。但有一些限制。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接