Jenkins Pipeline 多分支流水线 Input length = 1
jenkins,pipeline,多分,支流,水线,input,length
·
浏览次数 : 254
小编点评
**错误分析:**
```
java.nio.charset.MalformedInputException: Input length = 1
```
**问题:**
Pipeline 中的 `node[Pipeline]` 端点可能遇到 Input长度为 1 的错误,因为该端点可能尝试读取来自远程节点的输入,但输入长度为 1,可能无法读取任何内容。
**解决方案:**
1. **检查远程节点的输入长度:**使用 `node.getNodeInfo()` 方法获取远程节点的输入长度,并确保它正确。
2. **调整 Jenkins Pipeline 中的读取操作:**在 `node[Pipeline]` 端点之前,使用 `node.read()` 或 `node.readFully()` 方法读取远程输入,并确保其长度符合预期。
3. **设置文件编码:**确保所有输入文件的编码与输出文件的编码一致,通常使用 `UTF-8`。
4. **使用 `try-catch` 块捕获异常:**在 `node[Pipeline]` 端点中使用 `try-catch` 块捕获任何 `MalformedInputException`,并处理错误。
**示例:**
```groovy
pipeline {
agent any
stages {
stage("Remote Input") {
steps {
node(task: "Get Remote Input") {
script {
// 获取远程节点的输入字符串
String input = node.read()
// 检查输入长度
if (input.length == 1) {
throw new MalformedInputException("Input length should be greater than 1")
}
// 将输入字符串写入本地文件
writeFile(file: "remote-input.txt", text: input)
}
}
}
}
stage("Build Pipeline") {
steps {
// 使用 node.read() 读取远程输入文件
String pipelineInput = node.read()
// 使用 pipelineInput 进行构建
// ...
}
}
}
}
```
**注意:**
* `node.getNodeInfo()` 方法可能需要在执行之前执行一次 `node.getNodeInfo()`。
* `writeFile()` 方法会自动设置文件编码为 `UTF-8`。
正文
Jenkins 多分支流水线 构建过程中报错。
[Pipeline] // node
[Pipeline] End of Pipeline
java.nio.charset.MalformedInputException: Input length = 1
at java.base/java.nio.charset.CoderResult.throwException(CoderResult.java:274)
at java.base/java.lang.StringCoding.newStringNoRepl1(StringCoding.java:1056)
at java.base/java.lang.StringCoding.newStringNoRepl(StringCoding.java:1006)
at java.base/java.lang.System$2.newStringNoRepl(System.java:2193)
at java.base/java.nio.file.Files.readString(Files.java:3287)
at hudson.FilePath$ReadToString.invoke(FilePath.java:2377)
at hudson.FilePath$ReadToString.invoke(FilePath.java:2372)
at hudson.FilePath.act(FilePath.java:1192)
at hudson.FilePath.act(FilePath.java:1175)
注意要将文件编码设成对应的,Windows 编码要设成 ANSI 否则 Jenkins Pipeline 多分支流水线构建时,控制台输出乱码
与Jenkins Pipeline 多分支流水线 Input length = 1相似的内容: