簡體   English   中英

Github 操作:將評論發布到觸發當前工作流的 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完成時運行,使用如下的workflow_run觸發器:

on: 
  workflow_run:
    workflows: ["build"]
    types:
      - "completed"

第二個流程必須分開並在第一個流程之后運行。 完成后,它應該對觸發第一個工作流程的 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也應該有關於拉取請求的信息,但它們是空的。

我做錯了什么,我在哪里可以找到能夠評論我創建的拉取請求的必要信息?

您沒有做錯任何事情,只是第一個工作流的拉取請求數據不存在於第二個工作流的Github Context中。

要解決您的問題,您可以將您需要的拉取請求數據從第一個工作流發送到第二個工作流。

有不同的方法可以做到這一點,例如使用調度事件(而不是工作流運行)或工件。

對於工件,它看起來像下面這樣:

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 應用程序從 FIRST 工作流中獲取工件和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
        [...]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM