Getting object length? #27125
-
How do I get an object’s length? I have a pull request event that shows I have two labels on a PR. echo ‘${{ toJson(github.event.pull_request.labels.*.name) }}’ # shows me an array of two I have tried all of these and they all return ‘false’. echo ${{ github.event.pull_request.labels.*.name == 2 }} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
For my use case, I only needed to check whether labels existed or not so I was able to do this ugly hack: jobs: test: if: “!contains(toJson(github.event.pull_request.labels), ‘[]’)” But it seems like there should be an easy way to check an object’s length. Piping it to jq would work under FYI: ‘github.event.pull_request.labels != null’ did not work |
Beta Was this translation helpful? Give feedback.
-
@chingc Sorry to tell you that Github Actions context and expression syntax doesn’t provide a way to get an array or object length directly. As a work around , use jq to get the labels object length in a “run” step (like you mentioned above), then you could set the value to an output variable by using set-output command. And check the output variable in step if condition. There is my example: |
Beta Was this translation helpful? Give feedback.
-
@yanjingzhu Thanks for the workaround. I do hope GitHub will add a |
Beta Was this translation helpful? Give feedback.
-
Another easy trick is to check if the label at a given index is null: Etc. |
Beta Was this translation helpful? Give feedback.
@chingc Sorry to tell you that Github Actions context and expression syntax doesn’t provide a way to get an array or object length directly.
As a work around , use jq to get the labels object length in a “run” step (like you mentioned above), then you could set the value to an output variable by using set-output command. And check the output variable in step if condition.
There is my example: