小编Ma3*_*uct的帖子

如何在 Jenkinsfile 中进行字符串到整数的转换?

我的 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"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在多个地方重复使用诸如 timeout_mins 之类的环境参数,但需要在某些插件的某些地方将其转换为整数。我在上面的例子中得到的错误如下:

Processing environment for org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty
java.lang.IllegalArgumentException: Could not instantiate {time=null, unit=MINUTES} for TimeoutStep(time: int, unit?: TimeUnit[NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS]): java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.TimeoutStep.time expects int but received class java.lang.String
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:264)
    at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:194)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:181)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:126)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
    at …
Run Code Online (Sandbox Code Playgroud)

jenkins jenkins-pipeline

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

jenkins ×1

jenkins-pipeline ×1