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

复合动作传递多行环境变量至工作流输出为空问题排查

问题

编写了如下复合动作:

name: 'test'
description: 'test'
outputs:
  out:
    description: output
    value: ${{ env.out }}
runs:
  using: 'composite'
  steps:
    - name: "Setup - Checkout"
      uses: actions/checkout@v3

    - name: test
      id: plan
      shell: bash
      run: |
        out="hello \n world"
        
        echo "out<<EOF" >> $GITHUB_ENV
        echo "$out" >> $GITHUB_ENV
        echo "EOF" >> $GITHUB_ENV

在后续调用的GitHub Actions工作流步骤中遇到问题:

name: hello-world
jobs:
  my-job:
    outputs:
      out: ${{ steps.test.outputs.out }}
    runs-on: ubuntu-latest
    steps:
      # Checkout the code
      - name: "Setup - Checkout"
        uses: actions/checkout@v2


      - name: test
        id: test
        uses: ./.github/actions/test

  my-job2:
    needs: [my-job]
    runs-on: ubuntu-latest
    steps:
      - name: "test"
        shell: bash
        run: echo ${{ needs.my-job.outputs.out }} 

执行后my-job2中echo ${{ needs.my-job.outputs.out }}输出为空,询问是否未正确从复合动作传递输出变量。

问题分析与解决

问题出在复合动作的输出传递方式上,具体原因和修正方法如下:

  • 复合动作无法直接引用环境变量作为输出:复合动作的outputs字段不能直接读取env变量,必须通过步骤输出或GitHub Actions的输出设置命令传递值。
  • 多行字符串输出需用正确方式传递:传递多行内容时,推荐使用$GITHUB_OUTPUT文件(GitHub官方推荐的新方式),同时处理好换行的转义问题。

修正后的复合动作代码:

name: 'test'
description: 'test'
outputs:
  out:
    description: output
    value: ${{ steps.plan.outputs.out }} # 改为引用对应步骤的输出
runs:
  using: 'composite'
  steps:
    - name: "Setup - Checkout"
      uses: actions/checkout@v3

    - name: test
      id: plan
      shell: bash
      run: |
        # 直接使用真实换行,若要保留转义\n可改用printf转换
        out="hello
 world"
        
        # 通过$GITHUB_OUTPUT设置步骤输出,兼容多行内容
        printf "out<<EOF\n%s\nEOF\n" "$out" >> "$GITHUB_OUTPUT"

另外,在my-job2的echo命令中,建议用引号包裹输出变量,避免换行被截断:

- name: "test"
  shell: bash
  run: echo "${{ needs.my-job.outputs.out }}"

修改后即可正确传递并显示多行字符串输出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 14:30:18