0

I have the following simple job:

name: Issue comment handler
on: [issue_comment, workflow_dispatch]

jobs:
  issue_commented:
    if: github.event.comment.body == 'test'
    name: Issue comment
    runs-on: ubuntu-latest
    steps:
    - name: echo-out
      run: echo '${{ toJSON(github.event) }}'

When using that if conditional with this expression, it invariably skips the job. I haven't found anywhere in the docs that say why it skips, but I'm guessing it is because the body is untrusted user input and may contain malicious code if executed directly?

As suggested, I add an env variable instead:

name: Issue comment handler
on: [issue_comment, workflow_dispatch]

jobs:
  issue_commented:
    if: ${{ env.COMMENT == 'test' }}
    name: Issue comment
    runs-on: ubuntu-latest
    steps:
    - name: echo-out
      run: echo '${{ toJSON(github.event) }}'
    env: 
      COMMENT: ${{ github.event.comment.body }} # Added env variable here

When I try to use:

if: env.COMMENT == 'test'

I get: Unrecognized named-value: 'env'. Located at position 1 within expression: env.COMMENT == 'test'

When I try:

if: ${{ env.COMMENT == 'test' }}

I get: Unrecognized named-value: 'env'. Located at position 1 within expression: env.COMMENT == 'test'

When I try to use:

if: ${{ $COMMENT == 'test' }}

I get: Unexpected symbol: '$COMMENT'. Located at position 1 within expression: $COMMENT == 'test'

What am I doing wrong? Also, is it just me or are the docs quite bad and have lots of holes?

DILP
  • 759
  • 9
  • 14
  • echo-ing that out, it has the proper value. The problem is if I have the syntax like that it will skip the job. Searching the rest of github for similar patterns [search results](https://github.com/search?q=github.event.comment.body+extension%3Ayml+extension%3Ayaml+path%3A.github%2Fworkflows&type=Code) clicking into every one of the ones I spot-checked, they all skipped. It seems to be a pattern – DILP Nov 04 '22 at 16:47
  • strangely enough, when I run your original yaml snippet, I am able to run the job without it skipping – tg0h Nov 06 '22 at 04:29

1 Answers1

0

The Unrecognized named-value: 'env' errors are explained by the fact that if doesn't have access to the env scope (scroll to the jobs.<job_id>.if section).

As for the skipping, I found a workaround. For the first job I just check the conditions in the shell script, and set a variable for the next job to consume like so:

...
run: |
      if [[ $COMMENT =~ "test" ]]; then 
        echo Proceeding...
        echo "proceed=true" >> $GITHUB_OUTPUT
      else 
        echo Ending job...
      fi
# This adds the variable set above to the output
outputs:
  proceed: ${{ steps.check_the_comment.outputs.proceed }}
...

Then in the next job, I check if that output variable is true:

...
needs: <first_job_name>
open_issues:
  if: ${{ needs.check_comment.outputs.proceed }}
...

The effect is the second job is skipped if the if evaluates to false and will proceed if it's true.

DILP
  • 759
  • 9
  • 14