2

I have been trying to understand how to get a multi-line comment written to a PR using github actions. I was trying to use github.rest.issues.createComment() as shown in Commenting a pull request... and then handling the multi-line issue by using an environmement variable as shown here: workflow commands. Ultimate goal is to take some multi-line output stdout from a python script (or a log file) and place that as a comment back to the PR that the workflow is running on. The yml file below runs fine up until the last step where I try to access the environment variable I created and use it as the body of the createComment(). The environment variable is created and appears to be available but fails when I try to use it for the body of the comment. Error from github actions is below the code. If I add quotes like body: "${{env.SCRIPT_OUTPUT}}" then I get same error. I would like to use createComment() if possible, I know there is a create comment from Peter Evans that I will likely try next but trying to understand why this is not working.

name: GitHub Actions Test Multi-line
on:
  pull_request:
    branches:
      - Dev
jobs:
  Run-check-references:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - run: |
          SCRIPT_OUTPUT=$(cat << EOF
          first line
          second line
          third line
          EOF
          )
          echo "SCRIPT_OUTPUT<<EOF" >> $GITHUB_ENV
          echo "$SCRIPT_OUTPUT" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV
      - run: |
          echo "${{env.SCRIPT_OUTPUT}}"
          echo $SCRIPT_OUTPUT
      - uses: actions/github-script@v5
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: ${{env.SCRIPT_OUTPUT}}
            })

Run actions/github-script@v5
SyntaxError: Invalid or unexpected token
    at new AsyncFunction (<anonymous>)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4706:56)
    at main (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4761:26)
    at Module.272 (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4745:1)
    at __webpack_require__ (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:24:31)
    at startup (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:43:19)
    at /home/runner/work/_actions/actions/github-script/v5/dist/index.js:49:18
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:52:10)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
Error: Unhandled error: SyntaxError: Invalid or unexpected token
Zak Keirn
  • 807
  • 13
  • 22
  • Did you try using new line characters in a string var instead of multiline env var? – frennky Jan 09 '22 at 15:01
  • 1
    I think the problem is that this has to conform to JavaScript syntax. So you need to use the JavScript syntax for multi line strings. Probably the best to use \`multi line string\`. See also: https://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript – riQQ Jan 09 '22 at 16:14
  • The suggestion from @riQQ to use back tics worked! – Zak Keirn Jan 10 '22 at 15:44

1 Answers1

2

The suggestion from @riqq to use back tics solved the issue. So just had to change body: ${{env.SCRIPT_OUTPUT}} to:

body: `${{env.SCRIPT_OUTPUT}}`
Zak Keirn
  • 807
  • 13
  • 22