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

如何在GitHub Actions中完整获取yarn audit输出并生成PR评论

解决GitHub Actions中yarn audit完整输出无法作为PR评论的问题

问题根源在于你用::set-output传递输出时,换行符会被GitHub Actions解析为命令结束,导致仅传递了第一行内容。以下是两种可行的解决方法:

方法一:使用GitHub Actions推荐的多行输出方式(推荐)

GitHub Actions现在建议用$GITHUB_OUTPUT环境变量传递输出,配合EOF语法可完整保留所有换行和特殊字符:

- name: Run audit
  id: audit
  run: |
    log=$(yarn audit)
    # 用EOF语法将完整日志写入输出
    echo "log<<EOF" >> $GITHUB_OUTPUT
    echo "$log" >> $GITHUB_OUTPUT
    echo "EOF" >> $GITHUB_OUTPUT

- name: Create comment
  uses: peter-evans/create-or-update-comment@v2
  if: github.event_name == 'pull_request'
  with:
    issue-number: ${{ github.event.pull_request.number }}
    body: |
      ```
      ${{ steps.audit.outputs.log }}
      ```

方法二:转义换行符(兼容旧版set-output)

如果坚持使用::set-output,需要把输出里的换行符转义成GitHub能识别的%0A:

- name: Run audit
  id: audit
  run: |
    log=$(yarn audit)
    # 转义换行符后传递输出
    echo "::set-output name=log::$(echo "$log" | sed -z 's/\n/%0A/g')"

- name: Create comment
  uses: peter-evans/create-or-update-comment@v2
  if: github.event_name == 'pull_request'
  with:
    issue-number: ${{ github.event.pull_request.number }}
    body: |
      ```
      ${{ steps.audit.outputs.log }}
      ```

两种方法都能让PR评论显示完整的yarn audit输出,且用代码块包裹后格式与你预期一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 00:15:44