我的Jenkinsfile中有以下内容:
pipeline { agent none environment { timeout_mins = 1 } options { timeout(time: "${env.timeout_mins}", unit: 'MINUTES') } stages { stage("test print") { steps { echo "timeout_mins: ${env.timeout_mins}" sh "sleep 120" } } } }
我想在多个地方重用诸如timeout_mins之类的环境参数,但是对于某些插件,需要在某些地方将其转换为整数。我在上面的示例中遇到的错误如下:
org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty java.lang.IllegalArgumentException的处理环境:无法为TimeoutStep实例化{time = null,unit = MINUTES}(时间:int,unit ?: TimeUnit [NANOSECONDS,MICROSECONDS, [MILLISECONDS,SECONDS,MINUTES,HOURS,DAYS]):java.lang.ClassCastException:org.jenkinsci.plugins.workflow.steps.TimeoutStep.time需要int,但在org.jenkinsci.plugins.structs上收到了java.lang.String类。 org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.org)上的describable.DescribableModel.instantiate(DescribableModel.java:264)在org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:194)在org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL。 org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:126)上的org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)在org.codehaus.groovy上的org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)在org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)位于com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:18)的.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
在Jenkinsfile中,有人可以将String转换为int吗?
我试过了 time: env.timeout_mins,但这产生了一个空值。
time: env.timeout_mins
time: ${env.timeout_mins},产生:WorkflowScript:7:方法调用参数@第7行,第23列,超时(时间:$ {env.timeout_mins},单位:“ MINUTES”)
time: ${env.timeout_mins}
time: ${env.timeout_mins}.toInteger(),与上述相同
time: ${env.timeout_mins}.toInteger()
time: ${env.timeout_mins.toInteger()},与上述相同
time: ${env.timeout_mins.toInteger()}
我还能尝试其他方法吗?
并不是失败的转换,只是您不能在options块中引用环境变量。
这也会失败(nullpointer异常):
pipeline { agent none environment { timeout_mins = 'MINUTES' } options { timeout(time: 1, unit: env.timeout_mins) } stages { stage("test print") { steps { echo "timeout_mins: ${env.timeout_mins}" sh "sleep 120" } } } }
这有效:
def timeout_in_minutes = 1 pipeline { agent none options { timeout(time: timeout_in_minutes, unit: 'MINUTES') } stages { stage("test print") { steps { echo "timeout_in_minutes: ${env.timeout_in_minutes}" sh "sleep 120" } } } }