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

如何让GitHub Actions中github-script的输出换行显示?

解决GitHub Actions PR评论换行问题

你的代码存在两处关键问题导致换行不生效,按以下方案修改即可:

  1. 修正模板字符串引号不匹配问题
    你代码里的const output = line one \n line two'`结尾用了单引号,需和开头的反引号保持一致,改为:

    const output = `line one \n line two`
    
  2. 适配GitHub Markdown换行规则
    GitHub评论采用Markdown语法,单纯的\n仅在代码层面换行,显示时不会生效,可通过两种方式解决:

    • 方式一:用\n\n实现段落分隔(Markdown中空一行才会显示换行):
      const output = `line one \n\n line two`
      
    • 方式二:使用HTML<br>标签强制换行:
      const output = `line one <br> line two`
      

修改后的完整流水线示例:

jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        with:
          script: |
            const output = `line one \n\n line two`
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: output
            })

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 05:42:35