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

如何在GitHub可复用工作流中获取调用方指定的引用

解决可复用工作流获取调用时指定引用的问题

要让可复用工作流拿到调用它时指定的引用(比如v1.0.0),核心思路是通过参数传递把引用值从调用方工作流传给可复用工作流,具体步骤如下:

1. 修改可复用工作流,定义输入参数

在可复用工作流的workflow_call部分添加必填的字符串参数,用来接收调用时的引用值:

name: reusable workflows
on:
  workflow_call:
    inputs:
      target_ref:
        type: string
        required: true  # 强制调用方传递该参数

jobs:
  first-job:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3.1.0
        with:
          repository: owner/public-repo
          ref: ${{ inputs.target_ref }}  # 使用传入的引用值

      - name: composite action
        uses: ./actions/my-composite-action

2. 调用方工作流传递引用参数

在调用可复用工作流时,通过with字段把要使用的引用(比如v1.0.0)传递过去:

name: Caller workflow
on:
  workflow_dispatch:

jobs:
  caller:
    uses: owner/public-repo/.github/workflows/reusable-workflow.yml@v1.0.0
    with:
      target_ref: v1.0.0  # 明确传递可复用工作流的引用

补充说明

由于GitHub上下文始终关联调用方工作流,可复用工作流无法直接读取自身被调用时的引用,因此参数传递是最可靠的解决方案。如果调用方需要动态传递引用(比如调用方本身是从标签触发的),也可以用${{ github.ref_name }}作为参数值传入,比如:

# 调用方工作流从标签触发时的写法
name: Caller workflow
on:
  release:
    types: [published]

jobs:
  caller:
    uses: owner/public-repo/.github/workflows/reusable-workflow.yml@${{ github.ref_name }}
    with:
      target_ref: ${{ github.ref_name }}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 21:45:32