温馨提示×

centos jenkins如何换行符

小樊
41
2026-09-15 10:54:09
栏目: 智能运维

CentOS 上的 Jenkins 里,“换行符”通常涉及 三种场景,下面分别说明:


一、Shell 脚本 / Jenkinsfile 中的换行

1️⃣ Shell 步骤(sh)

sh '''
echo "第一行"
echo "第二行"
'''

sh "echo 第一行 \n echo 第二行"

Linux 默认换行符是 LF(\n


2️⃣ Jenkins Pipeline 多行命令

推荐用 '''(三单引号):

steps {
    sh '''
        echo hello
        echo world
    '''
}

二、Git 仓库中的换行符(CRLF / LF)

问题来源

  • Windows:CRLF(\r\n
  • Linux(CentOS):LF(\n

Jenkins + Git 推荐配置

方法 1:Jenkins 全局设置

Manage Jenkins → Configure System → Git

  • Checkout over CIFS 不勾
  • 勾选:
Convert CRLF to LF during checkout

方法 2:.gitattributes

仓库根目录添加:

* text=auto eol=lf

或针对脚本:

*.sh text eol=lf

三、文件内容中的换行(如写文件)

Groovy 写文件(LF)

writeFile file: 'test.txt', text: "line1\nline2\n"

Shell 写文件

cat > test.txt <<EOF
line1
line2
EOF

四、查看 / 转换换行符

查看

file file.txt
cat -A file.txt

转换(dos2unix)

yum install dos2unix
dos2unix file.txt

五、常见坑

❌ Jenkins 在 Windows Agent 上跑 Shell
✅ 用 Linux Agent 或 sh 步骤

❌ 脚本在 Windows 编辑后上传
✅ 用 dos2unix


如果你是指 “Jenkins 控制台输出换行”“邮件内容换行”“参数换行”,可以告诉我具体场景,我可以给你更精确的写法。

0