【问题标题】:Check if object is empty in GitHub Actions workflow检查 GitHub Actions 工作流程中的对象是否为空
【发布时间】:2021-11-04 18:02:24
【问题描述】:

我正在使用 GitHub Actions 工作流程运行测试,如果测试生成的结果对象为空,我希望测试失败。如何检查 JSON 对象是否为空?

我当前的工作流作业包含以下步骤:

- name: Fail if security tests found any issues
  if: ${{ steps.securityTestsResults != {} }}
  uses: actions/github-script@v3
  with:
    script: |
        core.setFailed('security tests failed: non-empty results')

但是上面的代码目前导致错误:

Invalid workflow file : .github/workflows/securityTests.yml#L34
The workflow is not valid. .github/workflows/securityTests.yml (Line: 34, Col: 11): Unexpected symbol: '{}'. Located at position 31 within expression: steps.securityTestsResults != {} 

这很令人困惑,因为我相信我的代码可以在常规 javascript 中运行,但它似乎在这里不起作用。我也尝试过Object.keys(obj).length === 0JSON.stringify(obj) == '{}',但它们也不起作用(它无法识别ObjectJSON 类)。

【问题讨论】:

  • 第 34 行,第 11 列是什么?
  • 这是包含if: ${{ steps.securityTestsResults != {} }}的行
  • 看起来像是 yaml 转义问题。您是否尝试将行放在双引号内?
  • 好的,我尝试将其更改为if: "${{ steps.securityTestsResults != {} }}",但仍然出现完全相同的错误。
  • 对不起,这可能使它成为一个不是你想要的字符串。您可以尝试的另一件事是使用管道 |并像在脚本键中一样在下一行添加代码

标签: javascript github javascript-objects github-actions


【解决方案1】:
- name: Check HOST ENV
   uses: actions/github-script@v4
   with:
    script: |
      if( "${{env.HOST}}" == "ERR" ){
        core.setFailed('Error mnj')
      }

【讨论】:

    猜你喜欢
    • 2022-12-13
    • 2022-01-11
    • 2020-04-03
    • 2022-11-02
    • 2022-01-10
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2016-10-20
    相关资源
    最近更新 更多