更新:还需要在 repo 设置 - Action - 开启读写权限,不然还是无权限。感谢 @innei-4525 大佬提醒
周三向 xLog 提交 PR 时,发现一项检测没有通过。打开报错信息一看,是 Next.js Bundle Analysis 工作流尝试创建评论失败。
Run peter-evans/create-or-update-comment@v2
Error: Resource not accessible by integration
Error: See this action's readme for details about this error
晚上发现 Innei 大佬还在解决该问题,我知道贴贴的机会来了(逃
在查阅了 peter-evans/create-or-update-comment
的 README 和 issue 之后,我发现在 pull_request
事件触发器下,第三方 fork 仓库在提交 PR 时缺乏写权限,无法创建 PR 评论。README 建议使用 pull_request_target
作为事件触发器以解决该问题。
注意:在公共仓库中,当由 forks 触发时,此操作在 pull_request 工作流中不起作用。任何尝试都会遇到错误,Resource not accessible by integration。这是由于 GitHub Actions 设置的令牌限制。私有仓库可以配置为允许来自 forks 的工作流无任何限制地运行。有关更多解释,请参见此处。或者,使用 pull_request_target 事件对拉取请求进行评论。
于是又去查看 github 官方文档对于 pull_request_target
的说明,发现有一段警告。
警告:对于由 pull_request_target 事件触发的工作流,GITHUB_TOKEN 被授予读 / 写仓库权限,除非指定了 permissions 键,并且工作流可以访问机密,即使它是从 fork 触发的。尽管工作流在拉取请求的基础上下文中运行,但您应该确保不从该事件中检出、构建或运行不受信任的代码。此外,任何缓存与基础分支共享相同的作用域。为了帮助防止缓存中毒,如果存在缓存内容被更改的可能性,则不应保存缓存。有关更多信息,请参见 GitHub Security Lab 网站上的 “保持您的 GitHub Actions 和工作流安全:防止 pwn 请求”。
大致意思是 pull_request_target
为来自 fork 仓库提交的 PR 提供了对目标仓库的读写权限,因此恶意 PR 可能引发安全隐患,使用时需谨慎选择适用场景。在警告中的文章 Keeping your GitHub Actions and workflows secure: Preventing pwn requests 中,详细阐述了这个问题,并提供了一个既能更新 PR 评论,又能避免使用 pull_request_target
的解决方案。该方案将整个工作流拆分为两个部分:首先,使用 pull_request
触发可能包含不安全代码的工作流,上传需要的结果(如代码测试结果、覆盖率);接着,在具有写权限的 workflow_on
中下载这些结果并更新 PR。
根据该方案,我们把 Next.js Bundle Analysis 工作流拆分成两个文件。
nextjs_bundle_analysis.yml
name: "Next.js Bundle Analysis"
on:
pull_request:
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# build process
- name: Upload analysis
if: success() && github.event.number
uses: actions/upload-artifact@v2
with:
name: analysis_comment
path: .next/analyze/__bundle_analysis_comment.txt
- name: Save PR number
if: ${{ always() }}
run: echo ${{ github.event.number }} > ./pr-id.txt
- name: Upload PR number
if: ${{ always() }}
uses: actions/upload-artifact@v2
with:
name: pr
path: ./pr-id.txt
comment-pr.yml
name: Comment on the pull request
# read-write repo token
# access to secrets
on:
workflow_run:
workflows: ["Next.js Bundle Analysis"]
types:
- completed
jobs:
upload:
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- name: download pr artifact
uses: dawidd6/action-download-artifact@v2
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
name: pr
- name: save PR id
id: pr
run: echo "::set-output name=id::$(<pr-id.txt)"
- name: download analysis comment
uses: dawidd6/action-download-artifact@v2
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
name: analysis_comment
path: .next/analyze
- name: Get Comment Body
id: get-comment-body
if: success() && steps.pr.outputs.id
run: |
echo "body<<EOF" >> $GITHUB_OUTPUT
echo "$(cat .next/analyze/__bundle_analysis_comment.txt)" >> $GITHUB_OUTPUT
echo EOF >> $GITHUB_OUTPUT
- name: Find Comment
uses: peter-evans/find-comment@v2
if: success() && steps.pr.outputs.id
id: fc
with:
issue-number: ${{ steps.pr.outputs.id }}
body-includes: "<!-- __NEXTJS_BUNDLE -->"
- name: Create Comment
uses: peter-evans/create-or-update-comment@v2
if: success() && steps.pr.outputs.id && steps.fc.outputs.comment-id == 0
with:
issue-number: ${{ steps.pr.outputs.id }}
body: ${{ steps.get-comment-body.outputs.body }}
- name: Update Comment
uses: peter-evans/create-or-update-comment@v2
if: success() && steps.pr.outputs.id && steps.fc.outputs.comment-id != 0
with:
issue-number: ${{ steps.pr.outputs.id }}
body: ${{ steps.get-comment-body.outputs.body }}
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
至此问题解决。