Github 操作:Post 评论触发当前工作流程的 PR 工作流程
Github actions: Post comment to PR workflow that triggered the current workflow
我有两个工作流程,第一个 运行 是一个构建脚本并生成一个工件。
当像这样创建拉取请求时触发第一个:
name: build
on:
pull_request:
types: [opened, edited, ready_for_review, reopened]
第二个流程在第一个流程完成后 运行 秒,通过使用 workflow_run
触发器,如下所示:
on:
workflow_run:
workflows: ["build"]
types:
- "completed"
第二个流程必须分开并且 运行 在第一个流程之后。完成后,它应该 post 对触发第一个工作流程的 PR 发表评论,但我无法找出如何。
根据 Github Action Docs 这是典型的用例之一,按照这个 qoute:
For example, if your pull_request workflow generates build artifacts, you can create
a new workflow that uses workflow_run to analyze the results and add a comment to the
original pull request.
但我似乎无法找出方法。我可以在第二个工作流的 context.payload.workflow_run.id
中获得第一个工作流的 ID,但是 workflow_run
也应该有关于拉取请求的信息,但它们是空的。
我做错了什么,我在哪里可以找到必要的信息来评论我创建的拉取请求?
您没有做错任何事,只是第一个工作流程中的 Pull Request 数据没有出现在第二个工作流程的 Github Context
中。
要解决您的问题,您可以将您需要的 Pull Request 数据从第一个工作流程发送到第二个工作流程。
有不同的方法可以做到这一点,例如使用 dispatch event(而不是工作流 运行)或工件。
对于神器,它看起来像下面这样:
在 FIRST 工作流程 中,您从 github.event 中获取 PR 编号。然后将该数字保存到文件中并将其作为工件上传。
- name: Save the PR number in an artifact
shell: bash
env:
PULL_REQUEST_NUMBER: ${{ github.event.number }}
run: echo $PULL_REQUEST_NUMBER > pull_request_number.txt
- name: Upload the PULL REQUEST number
uses: actions/upload-artifact@v2
with:
name: pull_request_number
path: ./pull_request_number.txt
在 第二个工作流程 中,您使用以下 GitHub 应用程序从第一个工作流程中获取工件和 Pull Request number
:
- name: Download workflow artifact
uses: dawidd6/action-download-artifact@v2.11.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
workflow: <first_workflow_name>.yml
run_id: ${{ github.event.workflow_run.id }}
- name: Read the pull_request_number.txt file
id: pull_request_number_reader
uses: juliangruber/read-file-action@v1.0.0
with:
path: ./pull_request_number/pull_request_number.txt
- name: Step to add comment on PR
[...]
我有两个工作流程,第一个 运行 是一个构建脚本并生成一个工件。 当像这样创建拉取请求时触发第一个:
name: build
on:
pull_request:
types: [opened, edited, ready_for_review, reopened]
第二个流程在第一个流程完成后 运行 秒,通过使用 workflow_run
触发器,如下所示:
on:
workflow_run:
workflows: ["build"]
types:
- "completed"
第二个流程必须分开并且 运行 在第一个流程之后。完成后,它应该 post 对触发第一个工作流程的 PR 发表评论,但我无法找出如何。
根据 Github Action Docs 这是典型的用例之一,按照这个 qoute:
For example, if your pull_request workflow generates build artifacts, you can create
a new workflow that uses workflow_run to analyze the results and add a comment to the
original pull request.
但我似乎无法找出方法。我可以在第二个工作流的 context.payload.workflow_run.id
中获得第一个工作流的 ID,但是 workflow_run
也应该有关于拉取请求的信息,但它们是空的。
我做错了什么,我在哪里可以找到必要的信息来评论我创建的拉取请求?
您没有做错任何事,只是第一个工作流程中的 Pull Request 数据没有出现在第二个工作流程的 Github Context
中。
要解决您的问题,您可以将您需要的 Pull Request 数据从第一个工作流程发送到第二个工作流程。
有不同的方法可以做到这一点,例如使用 dispatch event(而不是工作流 运行)或工件。
对于神器,它看起来像下面这样:
在 FIRST 工作流程 中,您从 github.event 中获取 PR 编号。然后将该数字保存到文件中并将其作为工件上传。
- name: Save the PR number in an artifact
shell: bash
env:
PULL_REQUEST_NUMBER: ${{ github.event.number }}
run: echo $PULL_REQUEST_NUMBER > pull_request_number.txt
- name: Upload the PULL REQUEST number
uses: actions/upload-artifact@v2
with:
name: pull_request_number
path: ./pull_request_number.txt
在 第二个工作流程 中,您使用以下 GitHub 应用程序从第一个工作流程中获取工件和 Pull Request number
:
- name: Download workflow artifact
uses: dawidd6/action-download-artifact@v2.11.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
workflow: <first_workflow_name>.yml
run_id: ${{ github.event.workflow_run.id }}
- name: Read the pull_request_number.txt file
id: pull_request_number_reader
uses: juliangruber/read-file-action@v1.0.0
with:
path: ./pull_request_number/pull_request_number.txt
- name: Step to add comment on PR
[...]