GitHub动作,PR上下文的触发工作流程

发布于 2025-02-07 13:19:16 字数 2594 浏览 2 评论 0原文

我们有一张在PRS上运行的检查:

name: check_time
on:
  pull_request:
    types:
      - synchronize
      - opened
      - reopened
  issue_comment:
    types:
      - created
jobs:
  check_time:
    runs-on: ubuntu-latest
    # run this if this event is not triggered by comment created (i.e. triggered by PR opened)
    # or run this if the comment was created on a PR
    if: ${{ !github.event.comment || (github.event.issue.pull_request && contains(github.event.comment.body, '/check_time')) }}
    steps:
    ...

我们希望能够按时间表触发此检查。因此,每隔一段时间,对于每一个开放式拉请请求,都请重新运行。这是以下尝试的实现:

name: trigger_check_time
on:
  workflow_dispatch:
    inputs:
      reason:
        description: 'Manual trigger of check-time'
        required: false
  schedule:
    - cron: '5 20 * * *'

jobs:
  provide_prs_json:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set_matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v3

      - name: List Pull Requests
        id: list_prs
        uses: actions/github-script@v6
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            const response = await github.rest.pulls.list({
              owner: 'xxxx',
              repo: 'yyyy',
            });
            const prs = response.data.map(pr => pr.number);
            console.log(`found ${prs.length} prs: ${JSON.stringify(prs)}`);
            return prs;

      - name: Set Matrix
        id: set_matrix
        run: echo ::set-output name=matrix::'${{ steps.list_prs.outputs.result }}'

  dispatch_event:
    needs: provide_prs_json
    runs-on: ubuntu-latest
    strategy:
      # the real magic happens here - create dynamic matrix from the json
      matrix:
        pr: ${{ fromJson(needs.provide_prs_json.outputs.matrix) }}
    steps:
      - name: Comment PR
        id: comment_pr
        uses: actions/github-script@v6
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          result-encoding: string
          script: |
            try {
              github.rest.issues.createComment({
                owner: 'xxxx',
                repo: 'yyyy',
                issue_number: ${{ matrix.pr }},
                body: "/check_time",
              });
            } catch (e) {}

上面的所有功能都很好,因为它在其线程中找到了所有PR和评论,但是该评论并未触发check_time工作流程运行。我认为这是因为事件github-actions不会触发其他事件?有什么方法可以评论用户?

我还考虑使用workflow_dispatch事件,但不确定如何让check_time Workflow在每个PR的上下文中运行(即,如果check_time工作流失败,PRS不再可合并)。

We have a check that runs on PRs:

name: check_time
on:
  pull_request:
    types:
      - synchronize
      - opened
      - reopened
  issue_comment:
    types:
      - created
jobs:
  check_time:
    runs-on: ubuntu-latest
    # run this if this event is not triggered by comment created (i.e. triggered by PR opened)
    # or run this if the comment was created on a PR
    if: ${{ !github.event.comment || (github.event.issue.pull_request && contains(github.event.comment.body, '/check_time')) }}
    steps:
    ...

We'd like to be able to trigger this check on a schedule. So, every so often, for every open pull request, have this check rerun. Here is the attempted implementation below:

name: trigger_check_time
on:
  workflow_dispatch:
    inputs:
      reason:
        description: 'Manual trigger of check-time'
        required: false
  schedule:
    - cron: '5 20 * * *'

jobs:
  provide_prs_json:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set_matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v3

      - name: List Pull Requests
        id: list_prs
        uses: actions/github-script@v6
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            const response = await github.rest.pulls.list({
              owner: 'xxxx',
              repo: 'yyyy',
            });
            const prs = response.data.map(pr => pr.number);
            console.log(`found ${prs.length} prs: ${JSON.stringify(prs)}`);
            return prs;

      - name: Set Matrix
        id: set_matrix
        run: echo ::set-output name=matrix::'${{ steps.list_prs.outputs.result }}'

  dispatch_event:
    needs: provide_prs_json
    runs-on: ubuntu-latest
    strategy:
      # the real magic happens here - create dynamic matrix from the json
      matrix:
        pr: ${{ fromJson(needs.provide_prs_json.outputs.matrix) }}
    steps:
      - name: Comment PR
        id: comment_pr
        uses: actions/github-script@v6
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          result-encoding: string
          script: |
            try {
              github.rest.issues.createComment({
                owner: 'xxxx',
                repo: 'yyyy',
                issue_number: ${{ matrix.pr }},
                body: "/check_time",
              });
            } catch (e) {}

The above all works fine in the sense that it finds all the PRs and comments in their threads but the comment does not trigger the check_time workflow to run. I assume this is because events github-actions do not trigger other events? Is there any way to comment as a user instead?

I also looked at using workflow_dispatch event, but not sure how to have the check_time workflow run in the context of each PR (i.e. if the check_time workflow fails, the PRs should no longer be mergeable).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文