簡體   English   中英

如何測試工作流中自定義 GitHub 操作應該失敗的輸入?

[英]How to test input that should fail for a custom GitHub action in a workflow?

我有一個自定義的 GitHub 操作,它使用 Dockerfile 封裝了一個 linter。在推送時,我想驗證 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,整個作業也會被計為失敗。

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: true不隨continue-on-error改變outcome屬性結合使用似乎可以解決問題。 現在唯一缺少的是我只想將退出代碼 2(驗證錯誤)報告為成功,同時將退出代碼 1(運行時錯誤)保持為錯誤,但這似乎無法通過此方法實現。

有不同級別的測試:系統、集成、系統。

您的方法 - 它正在 GitHub 上運行系統測試。明顯的缺點是您無法在本地測試您的操作,並且您有一個很長的反饋循環提交並將每個更改推送到回購協議,錯誤的嘗試使其膨脹。

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

  • 您可以根據用於測試操作的單獨單元的技術,使用熟悉的工具進行單元測試
  • 對於您在問題中嘗試做的事情(測試整個操作),您可以使用https://github.com/cardinalby/github-action-ts-run-api TS 庫在本地和 CI 上運行測試(使用您的工作流程)。 該庫模擬 GitHub runner 並具有 API 來傳遞不同的輸入、分析輸出和退出代碼,而無需將其推送到 repo。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM