
在 Jenkins Pipeline 中SSH 执行的结果通常以字符串文本返回。要从中找出error核心思路是对返回的文本进行模式匹配/解析。以下是几种常见场景和对应的解决方案1. 使用sshCommand/sshScript(SSH Pipeline Steps 插件)如果你使用的是sshCommand步骤它会返回命令的输出内容groovydef result sshCommand remote: remote, command: some-command // 检查输出中是否包含 error不区分大小写 if (result.toLowerCase().contains(error)) { error(SSH 命令执行结果中发现 error输出内容\n${result}) }2. 使用shssh命令如果你用sh步骤执行sshgroovydef result sh(script: ssh userhost some-command, returnStdout: true).trim() // 正则匹配 error 关键字 if (result ~ /(?i)error/) { error(发现错误信息\n${result}) }3. 更精确地提取 Error 信息如果输出格式比较固定可以用正则提取具体的错误内容groovydef result sshCommand remote: remote, command: your-command // 提取包含 error 的行 def errorLines result.readLines().findAll { line - line.toLowerCase().contains(error) } if (errorLines) { echo 发现 ${errorLines.size()} 处错误 errorLines.each { line - echo - ${line} } // 根据需求决定是报错还是仅记录 currentBuild.result UNSTABLE // 或 FAILURE }4. 结合命令退出码判断更可靠的方式是同时检查退出码groovydef exitCode sh( script: ssh userhost some-command || echo EXIT_CODE$?, returnStdout: true ) // 或者使用 returnStatus def status sh(script: ssh userhost some-command, returnStatus: true) if (status ! 0) { error(SSH 命令执行失败退出码${status}) }5. 完整示例封装为函数groovydef runSshWithErrorCheck(remote, command) { def output sshCommand remote: remote, command: command // 检查 stderr 或 stdout 中的 error def errors output.readLines().findAll { it ~ /(?i)\berror\b/ } if (errors) { echo ⚠️ 检测到错误信息 errors.each { echo it } // 抛出异常终止构建 throw new Exception(SSH 执行结果包含错误信息) } return output } // 使用 node { def remote [:] remote.name test-server remote.host 192.168.1.100 remote.user deploy remote.identityFile /path/to/key stage(Deploy) { runSshWithErrorCheck(remote, deploy.sh) } }关键要点总结表格方法适用场景contains(error)简单关键字检查正则匹配(?i)error不区分大小写更灵活readLines().findAll提取具体出错行returnStatus: true检查命令退出码两者结合最可靠退出码 输出内容双重检查建议不要只依赖文本中的error关键字因为有些命令即使成功也可能输出包含 error 的日志如搜索类命令。最佳实践是同时检查命令退出码$?和输出内容。你目前用的是哪种 SSH 方式我可以给更具体的代码。Execute shell script on remote host using ssh 这个插件的编辑复制分享明白了你用的是Execute shell script on remote host using ssh这个 Jenkins 插件通常指SSH Agent Plugin或Publish Over SSH插件中的 SSH 执行功能。这个插件执行远程脚本后返回的是命令的标准输出stdout字符串。要从中找出error你需要在 Pipeline 中对这个输出字符串进行处理。核心方案方案一基本关键字检查最简单groovystage(Remote SSH) { steps { script { // 执行远程命令捕获输出 def result sshCommand( remote: [ name: my-server, host: 192.168.1.100, user: jenkins, identityFile: /var/jenkins_home/.ssh/id_rsa ], command: sh /path/to/your-script.sh ) // 检查输出中是否包含 error不区分大小写 if (result.toLowerCase().contains(error)) { error(❌ 远程脚本输出中发现 error 关键字\n输出内容\n${result}) } echo ✅ 远程执行成功输出\n${result} } } }方案二提取具体的 Error 行推荐groovystage(Remote SSH with Error Parse) { steps { script { def result sshCommand( remote: remoteServer, // 你定义的 remote 变量 command: sh /path/to/script.sh ) // 逐行检查提取包含 error 的行 def errorLines result.readLines().findAll { line - line.toLowerCase().contains(error) } if (errorLines) { echo ⚠️ 发现 ${errorLines.size()} 处包含 error 的输出 errorLines.each { line - echo ${line} } // 设置为构建失败 currentBuild.result FAILURE error(远程脚本执行结果包含错误信息终止构建) } } } }方案三同时检查退出码 输出内容最可靠这个插件的sshCommand默认如果远程命令返回非 0 退出码Jenkins 构建会直接失败。但如果你想在命令成功退出码 0的情况下进一步检查输出内容中的 error用方案二即可。如果你想手动控制退出码检查可以这样做groovystage(Remote SSH Advanced) { steps { script { // 使用 returnStatus 获取退出码 def status sh( script: ssh -i /var/jenkins_home/.ssh/id_rsa userhost sh /path/script.sh; echo EXIT_CODE$?, returnStatus: true ) // 或者更简单地让 sshCommand 抛出异常后 catch try { def output sshCommand(remote: remoteServer, command: sh /path/script.sh) // 检查输出中的 error if (output ~ /(?i)\berror\b/) { throw new Exception(输出中包含 error 关键字) } echo 执行成功${output} } catch (Exception e) { echo 执行失败或包含错误${e.message} currentBuild.result FAILURE throw e } } } }方案四如果用的是 Publish Over SSH 插件的sshPublisher如果你用的是Publish Over SSH插件配置在 Jenkins 系统管理里的 SSH Servers通常是这样用的groovystage(Publish Over SSH) { steps { sshPublisher( publishers: [ sshPublisherDesc( configName: my-server, // 你在 Jenkins 配置里定义的 SSH Server 名称 transfers: [ sshTransfer( execCommand: sh /path/to/script.sh, execTimeout: 120000 ) ] ) ] ) } }问题sshPublisher这个步骤不直接返回命令输出它只根据退出码判断成功/失败。解决方案改用sshCommandSSH Pipeline Steps 插件或者把远程脚本的输出写入文件再拉取回来检查groovystage(SSH with Log File) { steps { script { // 远程执行把输出写入文件 sshCommand( remote: remoteServer, command: sh /path/to/script.sh /tmp/ssh_output.log 21; echo EXIT_CODE$? /tmp/ssh_output.log ) // 把远程文件拉取到 Jenkins 工作区 sshGet remote: remoteServer, from: /tmp/ssh_output.log, into: ssh_output.log, override: true // 读取并分析 def output readFile(ssh_output.log) // 检查 exit code def exitCode output.readLines().find { it.startsWith(EXIT_CODE) }?.replace(EXIT_CODE, )?.trim() echo 远程命令退出码: ${exitCode} // 检查 error def errors output.readLines().findAll { it.toLowerCase().contains(error) } if (errors || exitCode ! 0) { error(远程执行异常退出码: ${exitCode}, 错误行: ${errors}) } } } }快速对比表格插件/方式能否获取输出推荐检查 error 的方式sshCommand(SSH Pipeline Steps)✅ 直接返回字符串方案一/二直接contains或正则sshPublisher(Publish Over SSH)❌ 不返回输出方案四重定向到文件再拉取sh ssh ...✅ 通过returnStdout方案三灵活但需手动处理你最常用的是哪种方式如果是sshCommand直接用方案二即可如果是sshPublisher需要用方案四的重定向文件方式。