【问题标题】:Get github.rest.issues.createComment() to use an environment variable for multi-line comment获取 github.rest.issues.createComment() 以使用环境变量进行多行注释
【发布时间】:2022-01-10 23:56:31
【问题描述】:

我一直在尝试了解如何使用 github 操作将多行评论写入 PR。我试图使用 github.rest.issues.createComment() 如Commenting a pull request... 所示,然后使用环境变量处理多行问题,如下所示:workflow commands。最终目标是从 python 脚本(或日志文件)中获取一些多行输出标准输出,并将其作为注释放回运行工作流的 PR。下面的 yml 文件运行良好,直到最后一步我尝试访问我创建的环境变量并将其用作 createComment() 的主体。环境变量已创建并且似乎可用,但当我尝试将其用于评论正文时失败。来自 github 操作的错误位于代码下方。如果我添加像body: "${{env.SCRIPT_OUTPUT}}" 这样的引号,那么我会得到同样的错误。如果可能的话,我想使用 createComment(),我知道 Peter Evans 有一个创建评论,我可能会在接下来尝试,但试图理解为什么这不起作用。

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

【问题讨论】:

  • 您是否尝试在字符串 var 中使用换行符而不是多行 env var?
  • 我认为问题在于这必须符合 JavaScript 语法。因此,您需要对多行字符串使用 JavScript 语法。可能最好使用“多行字符串”。另见:stackoverflow.com/questions/805107/…
  • @riQQ 使用背部抽动的建议奏效了!

标签: yaml github-actions pull-request


【解决方案1】:

@riqq 使用 back tic 的建议解决了这个问题。所以只需将body: ${{env.SCRIPT_OUTPUT}} 更改为:

body: `${{env.SCRIPT_OUTPUT}}`

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 2018-06-26
    • 1970-01-01
    • 2018-12-04
    • 2023-02-17
    • 2020-12-22
    • 2016-08-11
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多