Jenkins管道SH returnstdout不起作用 [英] Jenkins pipeline sh returnstdout not working

查看:52
本文介绍了Jenkins管道SH returnstdout不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jenkins管道sh命令的returnStdout功能.在此处定义 https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-sh-code-shell-script

I am attempting to use the returnStdout feature of the Jenkins pipeline sh command. defined here https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-sh-code-shell-script

从先前的问题和答案中拉出:

Pulling from previous question and answers: 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}"
}

及其结果.您会看到我正在获取退出代码,而不是将结果传递到我的变量中:

and its result. You can see that I am getting exit code instead of the result passed into my variable:

[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(),以便更新后的代码如下所示:

Based on the examples I figured I might need to add the .trim() so my updated code looks like this:

node{
def output = sh(returnStdout: true, script: 'pwd').trim()
println "output = ${output}"
}

但这会导致整个作业失败:

But this results in the entire job to fail:

[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

我确定我缺少一些基本的知识,但是对于我一生来说,我看不到这段简单的代码有什么问题.任何帮助将不胜感激.

Im sure i'm missing something basic but for the life of me I cant see whats wrong with this simple code. Any help would be appreciated.

詹金斯版2.32.2

Jenkins ver. 2.32.2

推荐答案

没有方法签名:java.lang.Integer.trim()适用

No signature of method: java.lang.Integer.trim() is applicable

该错误消息表示 sh 步骤正在返回数字值.
对我来说,只有在 sh 步骤调用中使用 returnStatus 参数而不是 returnStdout 时,才会发生这种情况.

That error message means that the sh step is returning a numeric value.
For me, this only occurs if I use the returnStatus parameter rather than returnStdout in the sh step invocation.

确保您的插件是最新的,并且使用正确的参数;例如,如果您要从远程SCM加载管道,则可以使用任何构建页面侧栏中的重播"链接来确切查看加载了哪些管道脚本.

Make sure that your plugins are up-to-date, and that you're using the correct parameter; you can use the "Replay" link in the sidebar on any build page to see exactly which Pipeline script(s) was loaded, if you're loading the Pipeline from a remote SCM, for example.

此外,如果您真的只需要在shell步骤中运行 pwd ,则可以使用

Also, in case you really only need to run pwd in the shell step, you can simplify a bit by using the pwd Pipeline step.

这篇关于Jenkins管道SH returnstdout不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆