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

如何将GitHub Actions的createWorkflowDispatch调用关联至现有PR?

如何将通过createWorkflowDispatch触发的工作流与PR关联为检查项?

你可以通过传递PR关键信息到目标工作流,并利用GitHub Checks API创建/更新PR检查项来实现关联,具体步骤如下:

1. 修改触发工作流的脚本,传递PR信息

在调用createWorkflowDispatch时,把PR的编号、head SHA等信息通过inputs传递给目标工作流,同时确保Token拥有足够权限:

call_deploy_stage_muca:
  runs-on: ubuntu-latest
  # 配置必要权限
  permissions:
    actions: write
    checks: write
    pull-requests: read
  steps:
    - name: Trigger Workflow
      uses: actions/github-script@v6
      with:
        script: |
          const pr = context.payload.pull_request;
          if (!pr) {
            console.log("当前工作流并非由PR触发,终止操作");
            return;
          }
          await github.rest.actions.createWorkflowDispatch({
            ...context.repo,
            workflow_id: 'deploy-stage.yaml',
            ref: "develop", // 保留你原有的部署分支
            inputs: {
              'chosen': 'muca',
              'pr_number': pr.number.toString(),
              'pr_head_sha': pr.head.sha
            }
          })

2. 在目标工作流中配置PR检查项的创建与更新

在deploy-stage.yaml中,接收传递的PR参数,通过Checks API初始化检查状态,并在部署完成后更新状态:

name: Deploy to Stage
on:
  workflow_dispatch:
    inputs:
      chosen:
        type: string
        required: true
      pr_number:
        type: string
        required: false
      pr_head_sha:
        type: string
        required: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      # 初始化PR检查项(仅当传递了PR信息时执行)
      - name: Initialize PR Check
        if: ${{ inputs.pr_number }}
        uses: actions/github-script@v6
        id: init-check
        with:
          script: |
            const checkRun = await github.rest.checks.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: 'Stage Deployment (Muca)',
              head_sha: '${{ inputs.pr_head_sha }}',
              status: 'in_progress',
              started_at: new Date().toISOString()
            });
            return checkRun.data.id;

      # 执行你的部署逻辑
      - name: Perform Muca Deployment
        run: |
          echo "开始部署Muca到Stage环境..."
          # 替换为你的实际部署命令

      # 部署成功时更新检查状态
      - name: Update Check (Success)
        if: ${{ success() && inputs.pr_number }}
        uses: actions/github-script@v6
        with:
          script: |
            await github.rest.checks.update({
              owner: context.repo.owner,
              repo: context.repo.repo,
              check_run_id: ${{ steps.init-check.outputs.result }},
              status: 'completed',
              conclusion: 'success',
              completed_at: new Date().toISOString(),
              output: {
                title: 'Stage部署成功',
                summary: 'Muca已成功部署到Stage环境'
              }
            });

      # 部署失败时更新检查状态
      - name: Update Check (Failure)
        if: ${{ failure() && inputs.pr_number }}
        uses: actions/github-script@v6
        with:
          script: |
            await github.rest.checks.update({
              owner: context.repo.owner,
              repo: context.repo.repo,
              check_run_id: ${{ steps.init-check.outputs.result }},
              status: 'completed',
              conclusion: 'failure',
              completed_at: new Date().toISOString(),
              output: {
                title: 'Stage部署失败',
                summary: 'Muca部署到Stage环境失败,请检查日志'
              }
            });

原理说明

通过GitHub Checks API,我们直接针对PR的head SHA创建检查项,让部署流程的运行状态直接关联到PR页面。这样部署的成功/失败状态会在PR的"检查"区域显示,实现和其他PR检查项的协同运作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 16:13:13