获取 github.rest.issues.createComment() 使用环境变量进行多行注释

问题描述 投票:0回答:2

我一直在尝试了解如何使用 github actions 将多行评论写入 PR。我尝试使用 github.rest.issues.createComment(),如评论拉取请求...所示,然后使用环境变量处理多行问题,如下所示:工作流命令。最终目标是从 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
yaml github-actions pull-request
2个回答
6
投票

@riqq 使用背部抽动的建议解决了这个问题。所以只需将

body: ${{env.SCRIPT_OUTPUT}}
更改为:

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

0
投票

另一种方法是通过环境变量传递多行变量。如果它包含带有反引号的 markdown,则特别有用:`,如 @stray.leone 所指出的。

因为里面的脚本是 JavaScript 你可以这样做:

- uses: actions/github-script@v7
  env:
    COMMENT_BODY: ${{env.SCRIPT_OUTPUT}}
  with:
    script: |
      github.rest.issues.createComment({
        issue_number: context.issue.number,
        owner: context.repo.owner,
        repo: context.repo.repo,
        body: process.env.COMMENT_BODY
      })
© www.soinside.com 2019 - 2024. All rights reserved.