我有两个工作流程,第一个工作流程运行构建脚本并生成工件。 当创建拉取请求时会触发第一个请求,如下所示:
name: build
on:
pull_request:
types: [opened, edited, ready_for_review, reopened]
第二个流程在第一个流程完成后运行,通过使用 workflow_run
触发器,如下所示:
on:
workflow_run:
workflows: ["build"]
types:
- "completed"
第二个流程必须分开并在第一个流程之后运行。完成后,它应该对触发第一个工作流程的 PR 发表评论,但我不知道如何做。
根据Github Action Docs这是典型的用例之一,根据以下引用:
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
还应该有关于拉取请求的信息,但它们是空的。
我做错了什么,在哪里可以找到必要的信息来评论我创建的拉取请求?
最佳答案
您没有做任何错误,只是第一个工作流程中的拉取请求数据不存在于第二个工作流程的 Github 上下文
中。
要解决您的问题,您可以将所需的拉取请求数据从第一个工作流程发送到第二个工作流程。
有不同的方法可以做到这一点,例如使用 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 应用从第一个工作流程获取工件和拉取请求编号
:
- name: Download workflow artifact
uses: dawidd6/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88e9ebfce1e7e6a5ece7ffe6e4e7e9eca5e9fafce1eee9ebfcc8febaa6b9b9a6b8" rel="noreferrer noopener nofollow">[email protected]</a>
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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c3e292d28612a252029612d2f382523220c3a7d627c627c" rel="noreferrer noopener nofollow">[email protected]</a>
with:
path: ./pull_request_number/pull_request_number.txt
- name: Step to add comment on PR
[...]
关于Github 操作 : Post comment to PR workflow that triggered the current workflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68161639/