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

如何让GitHub Actions调用的build.yml使用update_workflow.yml最新更新?

解决GitHub Actions调用子工作流使用旧代码的问题

问题核心:通过workflow_call调用build.yml时,子工作流默认会 checkout 触发父工作流(update_workflow.yml)的初始提交版本,而非你刚推送到main分支的最新代码——即便指定了ref: main,GitHub Actions在触发工作流时已锁定初始commit,不会自动拉取后续推送的最新内容。

修改步骤:

1. 更新update_workflow.yml,传递最新commit SHA给子工作流

在update_fork步骤中,新增获取最新commit SHA的逻辑并设为输出,调用build.yml时传入该值:

jobs:
  update_fork:
    runs-on: ubuntu-latest
    outputs:
      latest_commit: ${{ steps.get_commit.outputs.sha }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3
        with:
          ref: main
          fetch-depth: 0  # 拉取完整历史,保障rebase正常执行
      # 你的仓库更新命令
      - name: Update repo
        run: |
          git remote add upstream <你的上游仓库地址>
          git checkout main
          git rebase upstream/main || git diff
          git push -f origin main
          echo "Rebase successful!"
      - name: Get latest commit SHA
        id: get_commit
        run: echo "sha=$(git rev-parse main)" >> $GITHUB_OUTPUT

  build_now:
    needs: update_fork
    uses: ./.github/workflows/build.yml
    secrets: inherit
    with:
      target_commit: ${{ needs.update_fork.outputs.latest_commit }}

2. 修改build.yml,接收并使用传递的commit SHA

先在workflow_call中定义输入参数,再在checkout步骤用该参数作为ref:

on:
  workflow_call:
    inputs:
      target_commit:
        required: true
        type: string
  # 保留原有触发条件,不影响手动/自动触发的原有逻辑
  push:
    branches: [main]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.target_commit || github.sha }}
          # 手动/push触发时默认使用github.sha,兼容原有场景
      # 你的其他构建步骤

原理说明

通过父工作流获取推送后的最新commit SHA并显式传递给子工作流,让子工作流精准checkout到该最新提交,而非父工作流触发时的旧版本。全程基于GitHub Actions原生workflow_call机制实现,无需依赖curl调用API。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 01:17:29