You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现GitHub Actions的Reporter workflow仅在Benchmarks job特定状态触发?

基于工作流中特定作业状态触发Reporter Workflow的解决方案

方案1:在CI Workflow中主动触发Reporter Workflow(推荐)

直接在CI的Benchmarks Job完成且成功时,通过GitHub API触发Reporter Workflow,从根源上精准控制触发时机,完全避免依赖workflow_run的模糊过滤。

步骤:

  1. 在CI Workflow的Benchmarks Job末尾添加触发步骤:
jobs:
  Benchmarks:
    if: <你的Benchmarks触发条件>
    runs-on: ubuntu-latest
    steps:
      # 你的Benchmarks执行步骤...
      - name: 触发Reporter Workflow
        if: success()
        uses: actions/github-script@v6
        with:
          script: |
            await github.rest.actions.createWorkflowDispatch({
              owner: context.repo.owner,
              repo: context.repo.repo,
              workflow_id: 'reporter.yml', // Reporter Workflow的文件名
              ref: context.ref,
              inputs: {
                ci_run_id: "${{ github.run_id }}",
                benchmarks_status: "success"
              }
            })
  1. 修改Reporter Workflow的触发方式为workflow_dispatch,接收CI传递的参数:
on:
  workflow_dispatch:
    inputs:
      ci_run_id:
        required: true
        type: string
      benchmarks_status:
        required: true
        type: string

jobs:
  report_end:
    runs-on: ubuntu-latest
    steps:
      - name: 生成Benchmarks报告
        run: |
          echo "基于CI工作流${{ inputs.ci_run_id }}的Benchmarks作业生成报告"

优势:完全精准控制触发时机,无需复制任何条件规则,还能传递CI上下文参数给Reporter,逻辑清晰且维护成本低。

方案2:在Reporter中主动查询CI的作业状态

通过workflow_run触发Reporter后,调用GitHub API查询CI Workflow中Benchmarks Job的实际运行状态,以此判断是否执行报告逻辑。

步骤:

on:
  workflow_run:
    workflows: ['CI']
    types:
      - completed

jobs:
  check_and_report:
    runs-on: ubuntu-latest
    steps:
      - name: 查询CI中Benchmarks作业状态
        uses: actions/github-script@v6
        id: check_benchmarks
        with:
          script: |
            const jobs = await github.rest.actions.listJobsForWorkflowRun({
              owner: context.repo.owner,
              repo: context.repo.repo,
              run_id: ${{ github.event.workflow_run.id }}
            });
            const benchmarksJob = jobs.data.jobs.find(job => job.name === 'Benchmarks');
            // 仅当作业存在且成功时返回true
            return benchmarksJob && benchmarksJob.conclusion === 'success';
      - name: 执行报告逻辑
        if: steps.check_benchmarks.outputs.result == 'true'
        run: |
          echo "Benchmarks作业已成功完成,生成报告"

优势:无需修改CI的触发逻辑,Reporter自主判断是否执行,避免复制条件规则;唯一不足是需要额外的API调用,但GitHub的API配额完全满足这类场景需求。

方案3:优化现有复制规则方案(临时过渡)

如果暂时不想大改,可以把Benchmarks的触发条件抽象成可复用的变量文件,同时在CI和Reporter中引用,减少重复维护成本。

步骤:

  1. 在仓库创建.github/workflows/benchmarks-trigger.yml,统一定义触发条件:
trigger_condition: "github.event.pull_request.labels.*.name == 'run-benchmarks' || github.base_ref == 'main'"
  1. CI Workflow中引用该变量:
jobs:
  variables:
    runs-on: ubuntu-latest
    outputs:
      trigger_condition: ${{ steps.read_vars.outputs.trigger_condition }}
    steps:
      - name: 读取触发条件
        id: read_vars
        run: |
          content=$(cat .github/workflows/benchmarks-trigger.yml)
          echo "trigger_condition=$(echo "$content" | grep trigger_condition | cut -d '"' -f2)" >> $GITHUB_OUTPUT
  Benchmarks:
    needs: [Build, variables]
    if: ${{ fromJSON(needs.variables.outputs.trigger_condition) }}
    # Benchmarks作业逻辑...
  1. Reporter Workflow中同样引用该变量过滤:
on:
  workflow_run:
    workflows: ['CI']
    types:
      - completed

jobs:
  report_end:
    runs-on: ubuntu-latest
    steps:
      - name: 读取触发条件
        id: read_vars
        run: |
          content=$(cat .github/workflows/benchmarks-trigger.yml)
          echo "trigger_condition=$(echo "$content" | grep trigger_condition | cut -d '"' -f2)" >> $GITHUB_OUTPUT
      - name: 执行报告逻辑
        if: ${{ fromJSON(steps.read_vars.outputs.trigger_condition) && github.event.workflow_run.conclusion == 'success' }}
        run: |
          echo "生成Benchmarks报告"

优势:修改触发条件只需维护一个文件,降低重复代码的脆弱性;本质还是复用规则,但比直接复制更易维护。

内容的提问来源于stack exchange,提问作者rschristian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 06:25:08