检查 GitHub Actions 工作流程中的对象是否为空

问题描述 投票:0回答:2

我正在使用 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 === 0
JSON.stringify(obj) == '{}'
但这些也不起作用(它无法识别
Object
JSON
类)。

javascript github javascript-objects github-actions
2个回答
3
投票

尝试

toJSON

if: toJSON(steps.securityTestsResults) == '{}'

(您不需要在

${{ }}
语句中包装
if
,因为它们始终被评估为表达式。)

文档:https://docs.github.com/en/actions/learn-github-actions/expressions#tojson


2
投票
- name: Check HOST ENV
   uses: actions/github-script@v4
   with:
    script: |
      if( "${{env.HOST}}" == "ERR" ){
        core.setFailed('Error mnj')
      }
© www.soinside.com 2019 - 2024. All rights reserved.