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

由Workflow A触发的Workflow B运行分支不符,如何共享分支值?

解决Workflow B与Workflow A运行同一分支的问题

当通过workflow_run触发Workflow B时,默认会拉取仓库的默认分支(比如master),要让它和Workflow A运行在同一分支,需要通过GitHub事件上下文和API获取原工作流的分支信息,再指定给actions/checkout步骤。

方法一:使用GitHub API + jq解析

修改Workflow B的配置,添加获取分支的步骤,再拉取对应分支:

name: Workflow B
on:
  workflow_run:
    workflows: ['Workflow A']
    types:
      - completed
  workflow_dispatch:

jobs:
  first-workflow-job:
    runs-on: ubuntu-latest
    steps:
      - name: 获取原工作流分支
        id: get-branch
        run: |
          # 从PR事件中提取分支(适配Workflow A由PR触发的场景)
          BRANCH=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
            "${{ github.event.workflow_run.events_url }}" | jq -r '.[] | select(.event == "pull_request") | .pull_request.head.ref')
          
          # 如果不是PR触发,直接取工作流的head分支
          if [ -z "$BRANCH" ]; then
            BRANCH=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
            "${{ github.event.workflow_run.url }}" | jq -r '.head_branch')
          fi
          
          echo "branch=$BRANCH" >> $GITHUB_OUTPUT

      - uses: actions/checkout@v3
        with:
          ref: ${{ steps.get-branch.outputs.branch }}

      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          cache: 'npm'

方法二:使用actions/github-script简化操作

用官方的GitHub脚本action来调用API,无需手动处理curl和jq:

name: Workflow B
on:
  workflow_run:
    workflows: ['Workflow A']
    types:
      - completed
  workflow_dispatch:

jobs:
  first-workflow-job:
    runs-on: ubuntu-latest
    steps:
      - name: 获取原工作流分支
        id: get-branch
        uses: actions/github-script@v6
        with:
          script: |
            // 获取原工作流的运行信息
            const runInfo = await github.rest.actions.getWorkflowRun({
              owner: context.repo.owner,
              repo: context.repo.repo,
              run_id: context.payload.workflow_run.id
            });
            
            let targetBranch = runInfo.data.head_branch;
            
            // 如果是PR触发,获取PR的源分支
            if (runInfo.data.event === 'pull_request') {
              const prInfo = await github.rest.pulls.get({
                owner: context.repo.owner,
                repo: context.repo.repo,
                pull_number: runInfo.data.pull_requests[0].number
              });
              targetBranch = prInfo.data.head.ref;
            }
            
            return targetBranch;
          result-encoding: string

      - uses: actions/checkout@v3
        with:
          ref: ${{ steps.get-branch.outputs.result }}

      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          cache: 'npm'

关键说明

  • ${{ secrets.GITHUB_TOKEN }}是GitHub自动提供的内置令牌,具备读取仓库事件和工作流信息的权限,无需额外配置。
  • 两种方法都会优先适配PR触发的场景,提取PR的源分支;如果Workflow A是直接分支推送触发的,则获取对应的head分支。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 22:22:29