最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if object is empty in GitHub Actions workflow - Stack Overflow

programmeradmin2浏览0评论

I'm running a test using a GitHub Actions workflow, and I would like to fail the test if the resulting object produced by the test is empty. How do I check if a JSON object is empty?

My current workflow job contains the step:

- 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')

But the code above currently results in the error:

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 != {} 

It's confusing since I believe my code would work in regular javascript, but it doesn't appear to work here. I've also tried Object.keys(obj).length === 0 and JSON.stringify(obj) == '{}' but these don't work either (it doesn't recognize the Object or JSON classes).

I'm running a test using a GitHub Actions workflow, and I would like to fail the test if the resulting object produced by the test is empty. How do I check if a JSON object is empty?

My current workflow job contains the step:

- 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')

But the code above currently results in the error:

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 != {} 

It's confusing since I believe my code would work in regular javascript, but it doesn't appear to work here. I've also tried Object.keys(obj).length === 0 and JSON.stringify(obj) == '{}' but these don't work either (it doesn't recognize the Object or JSON classes).

Share asked Sep 7, 2021 at 22:08 wristbandswristbands 1,38714 silver badges28 bronze badges 11
  • What is Line: 34, Col: 11? – Dominik Commented Sep 7, 2021 at 22:20
  • It's the line containing the if: ${{ steps.securityTestsResults != {} }} – wristbands Commented Sep 7, 2021 at 23:48
  • Looks like a yaml escaping issue. Have you tried putting the line within double quotes? – Dominik Commented Sep 7, 2021 at 23:53
  • Ok, I tried changing it to if: "${{ steps.securityTestsResults != {} }}" but I still got exactly the same error. – wristbands Commented Sep 8, 2021 at 0:19
  • 1 Your steps.securityTestResults seems weird, it should be something like steps.<step_id>.outputs.<output_id>. Anyway, you can create another step (eg: validate_object, it should return true or false) using actions/github-script@v3 and use javascript to validate the object, and then use it in your next step as if ${{ steps.<step_id>.outputs.result }}. – soltex Commented Sep 9, 2021 at 11:38
 |  Show 6 more ments

2 Answers 2

Reset to default 3

Try toJSON:

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

(You don't need the wrapping ${{ }} in an if statement, as they're always evaluated as expressions.)

Docs: https://docs.github./en/actions/learn-github-actions/expressions#tojson

- name: Check HOST ENV
   uses: actions/github-script@v4
   with:
    script: |
      if( "${{env.HOST}}" == "ERR" ){
        core.setFailed('Error mnj')
      }
发布评论

评论列表(0)

  1. 暂无评论