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

Github Actions多行输出写入$GITHUB_OUTPUT报错求助

解决Github Actions中多行输出写入GITHUB_OUTPUT的错误

问题根源是git diff返回的多行内容直接通过echo写入$GITHUB_OUTPUT时,每一行会被当作独立的输出指令,导致解析失败。以下是几种可行的解决方案:

方法1:转义换行符为单行输出

使用printf的%q参数将特殊字符(包括换行)转义,把多行内容转为单行存储,后续使用时可还原换行:

- name: Get files 
  id: files
  run: |
    test=$(git diff --name-only origin/${{ github.event.pull_request.base.ref }} origin/${{ github.event.pull_request.head.ref }})
    printf "test=%q\n" "$test" >> $GITHUB_OUTPUT

后续步骤中还原换行的示例:

- name: Use files list
  run: |
    echo -e "${{ steps.files.outputs.test }}"

方法2:使用heredoc格式写入多行输出

利用Github Actions支持的heredoc语法,直接将多行内容作为输出值:

- name: Get files 
  id: files
  run: |
    cat <<EOF >> $GITHUB_OUTPUT
    test<<EOF
    $(git diff --name-only origin/${{ github.event.pull_request.base.ref }} origin/${{ github.event.pull_request.head.ref }})
    EOF
    EOF

这种方式无需转义,输出值会完整保留原始的多行格式,直接使用${{ steps.files.outputs.test }}即可获取所有差异文件。

方法3:通过JSON转义处理

使用jq将多行内容转为JSON字符串,后续通过fromJSON解析:

- name: Get files 
  id: files
  run: |
    test=$(git diff --name-only origin/${{ github.event.pull_request.base.ref }} origin/${{ github.event.pull_request.head.ref }})
    echo "test=$(jq -n --arg t "$test" '$t')" >> $GITHUB_OUTPUT

后续使用时解析JSON:

- name: Use files list
  run: |
    echo "${{ fromJSON(steps.files.outputs.test) }}"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 19:28:09