Search code examples
yamlgithub-actionspull-request

Get github.rest.issues.createComment() to use an environment variable for multi-line comment


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

Solution

  • 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}}`