我正在尝试使用Jenkins pipeline sh命令的returnStdout功能。这里定义https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-sh-code-shell-script
从上一个问题和答案中提取:Is it possible to capture the stdout from the sh DSL command in the pipeline
我的原始代码:
node{
def output = sh(returnStdout: true, script: 'pwd')
println "output = ${output}"
}
及其结果。您可以看到我正在获取退出代码而不是传递给我的变量的结果:
[Pipeline] node {
[Pipeline] sh
[Update_Stageing_DB] Running shell script
+ pwd
/mnt/storage/jenkins/workspace/Update_Stageing_DB
[Pipeline] echo
output = 0
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
Finished: SUCCESS
基于我想到的示例,我可能需要添加.trim(),因此我的更新代码如下所示:
node{
def output = sh(returnStdout: true, script: 'pwd').trim()
println "output = ${output}"
}
但这会导致整个工作失败:
[Pipeline] node {
[Pipeline] sh
[Update_Stageing_DB] Running shell script
+ pwd
/mnt/storage/jenkins/workspace/Update_Stageing_DB
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.trim() is applicable for argument types: () values: []
Possible solutions: wait(), grep(), wait(long), times(groovy.lang.Closure), div(java.lang.Character), print(java.io.PrintWriter)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:15)
at WorkflowScript.run(WorkflowScript:3)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:55)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:106)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:74)
at sun.reflect.GeneratedMethodAccessor771.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:58)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:277)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(CpsThreadGroup.java:77)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:186)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:184)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE
我确定我遗漏了一些基本的东西,但对于我的生活,我看不出这个简单的代码有什么问题。任何帮助将不胜感激。
Jenkins ver。 2.32.2
答案 0 :(得分:1)
没有方法签名:java.lang.Integer.trim()适用
该错误消息表示sh
步骤正在返回一个数值
对我来说,只有在returnStatus
步骤调用中使用returnStdout
参数而不是sh
时才会出现这种情况。
确保您的插件是最新的,并且您正在使用正确的参数;你可以使用"重播"例如,如果您要从远程SCM加载管道,请在任何构建页面的侧栏中链接以确切地查看已加载的管道脚本。
此外,如果您真的只需要在shell步骤中运行pwd
,则可以使用pwd
管道步骤简化一点。