获取 github.rest.issues.createComment() 以使用环境变量进行多行注释
Get github.rest.issues.createComment() to use an environment variable for multi-line comment
我一直在尝试了解如何使用 github 操作将多行评论写入 PR。我正在尝试使用 github.rest.issues.createComment(),如 and then handling the multi-line issue by using an environmement variable as shown here: 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
@riqq 提出的使用 back tics 的建议解决了这个问题。所以只需要将 body: ${{env.SCRIPT_OUTPUT}}
更改为:
body: `${{env.SCRIPT_OUTPUT}}`
我一直在尝试了解如何使用 github 操作将多行评论写入 PR。我正在尝试使用 github.rest.issues.createComment(),如 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
@riqq 提出的使用 back tics 的建议解决了这个问题。所以只需要将 body: ${{env.SCRIPT_OUTPUT}}
更改为:
body: `${{env.SCRIPT_OUTPUT}}`