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

如何在GitHub Actions同Job步骤间传递Action输出变量

实现方法

要把其他目录Action的变量作为输出在Job1后续步骤中使用,只需完成Action定义输出和Workflow引用输出这两步配置:

1. 在被调用的Action中声明并设置输出

找到目标Action所在目录的action.yml(或action.yaml)文件,先通过outputs字段声明要暴露的变量,再在Action的执行步骤中给该变量赋值。

示例:Action的action.yml配置

name: "自定义输出Action"
description: "生成并输出自定义变量"

# 声明要暴露的输出变量
outputs:
  target_var:
    description: "需要传递到Job后续步骤的变量"
    value: ${{ steps.set_output.outputs.target_var }}

runs:
  using: "composite"
  steps:
    - name: 生成并设置输出值
      id: set_output
      shell: bash
      run: |
        # 这里替换成你的业务逻辑,生成需要输出的变量值
        RESULT="example-value-from-action"
        # 使用GitHub Actions推荐的新语法设置输出
        echo "target_var=$RESULT" >> $GITHUB_OUTPUT

注:如果需要兼容旧版语法,可替换为echo "::set-output name=target_var::$RESULT",但优先推荐GITHUB_OUTPUT的方式。

2. 在可重用Workflow的Job1中引用输出

在Job1调用该Action时,给这个步骤指定一个唯一id,后续步骤就可以通过${{ steps.步骤id.outputs.输出变量名 }}的格式获取该值。

示例:可重用Workflow配置

name: 可重用工作流模板
on:
  workflow_call:

jobs:
  Job1:
    runs-on: ubuntu-latest
    steps:
      - name: 调用自定义Action
        id: call_custom_action
        # 假设Action在当前仓库的./.github/actions/my-action目录下,路径按需调整
        uses: ./../.github/actions/my-action

      - name: 使用Action的输出变量
        shell: bash
        run: |
          echo "从Action拿到的变量值:${{ steps.call_custom_action.outputs.target_var }}"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 13:52:04