【问题标题】:How to test input that should fail for a custom GitHub action in a workflow?如何测试工作流中自定义 GitHub 操作应该失败的输入?
【发布时间】:2023-01-03 04:53:20
【问题描述】:

我有一个使用 Dockerfile 封装 linter 的自定义 GitHub 操作。在推送时,我想验证 linter 是否正常工作,即它应该在正确输入时成功并在错误输入时失败:

.github/workflows/test-action.yml

name: Test Action
 
on:
  workflow_dispatch:
  push:
    branches:
      - master
 
jobs:
  test-correct:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Lint correct input
        uses: ./
        with:
          file: should-succeed.ex
  test-incorrect:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Lint incorrect input
        uses: ./
        with:
          file: should-fail.ex

但是在 GitHub 上,例如https://github.com/myorg/myrepo/actions/runs/123456789 这当然会将成功的工作涂成绿色,将不成功的工作涂成红色。我如何告诉 GitHub 反转颜色,以便失败导致成功,成功导致失败?

编辑:我尝试了以下但它不起作用,因为 if: failure() 将不会触发:

[...]
      - name: Lint incorrect input
        uses: ./
        continue-on-error: true
        with:
          file: should-fail.ex
      - if: failure()
        run: true
      - if: success()
        run: false

另一方面,如果我删除 continue-on-error: true 行,那么它也不会工作,因为即使我返回 true,整个作业也会被计为失败。

【问题讨论】:

  • 使用uses,我认为你做不到。如果您是 running 一个命令,您可以捕获非零退出,但这不是这里的选项。

标签: github-actions building-github-actions


【解决方案1】:
jobs:
  test-correct:
  [...]
  test-incorrect:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: This should fail
        id: lint-incorrect
        uses: ./
        continue-on-error: true
        with:
          file: should-fail.ex
      - name: Invert success and failure
        run: if [[ ${{ steps.lint-incorrect.outcome }} == "failure" ]]; then exit 0; else exit 1; fi

continue-on-error: trueoutcome 属性 which does not change with continue-on-error 结合使用似乎可以解决问题。现在唯一缺少的是我只想将退出代码 2(验证错误)报告为成功,同时将退出代码 1(运行时错误)保持为错误,但这似乎无法通过此方法实现。

【讨论】:

    【解决方案2】:

    有不同级别的测试:系统、集成、系统。

    您的方法 - 它在 GitHub 上运行系统测试。明显的缺点是您无法在本地测试您的操作,并且您有一个很长的反馈循环,将每个更改提交并推送到存储库,错误的尝试使其膨胀。

    我在article 中描述了我的方法。主要思想是:

    • 您可以根据您用来测试操作的单独单元的技术,使用熟悉的工具进行单元测试
    • 对于您在问题中尝试做的事情(测试整个操作),您可以使用https://github.com/cardinalby/github-action-ts-run-api TS 库在本地和 CI 上运行测试(使用您的工作流程)。该库模拟 GitHub runner 并具有 API 来传递不同的输入、分析输出和退出代码,而无需将其推送到 repo。

    【讨论】:

      猜你喜欢
      • 2023-01-05
      • 2020-10-26
      • 2020-09-29
      • 2021-05-03
      • 2020-08-11
      • 2021-11-14
      • 2021-07-20
      • 2020-02-29
      • 1970-01-01
      相关资源
      最近更新 更多