How can I convert string to integer in Jenkinsfile?

My Jenkins file has the following:

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"
            }
        }
    }
}

      

I would like to reuse environment parameters like timeout_mins in multiple places, but you need to convert them to integers in specific places for specific plugins. The error I am getting with the above example looks like this:

Processing environment for org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty java.lang.IllegalArgumentException: Failed to 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 got java.lang.String class on org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate (describe Model.java.: 264) on 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. DSL.invokeMethod (DSL.java:126) at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod (CpsScript.java:108) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call (PogoMetaClassSite.java:48) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultcodeCall (CallSiteArray.java:48 .runtime.callsite.AbstractCallSite.call (AbstractCallSite.java:113) at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall (DefaultInvoker.java:18) when

Does anyone have a way to convert from String to int to Jenkinsfile?

I've tried time: env.timeout_mins

but this gives a null value.

time: ${env.timeout_mins}

, gives: WorkflowScript: 7: method call arguments @ line 7, column 23. timeout (time: $ {env.timeout_mins}, unit: 'MINUTES')

time: ${env.timeout_mins}.toInteger()

, as mentioned above

time: ${env.timeout_mins.toInteger()}

, as mentioned above

Any other things I can try?

+3


source to share


1 answer


it is not a conversion that fails, it is just that you cannot reference environment variables in a parameter block.

this also fails (nullpointer exception):

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"
            }
        }
    }
}

      



it works:

def timeout_in_minutes = 1

pipeline {
    agent none
    options {
        timeout(time: timeout_in_minutes, unit: 'MINUTES')
    }
    stages {
        stage("test print") {
            steps {
                echo "timeout_mins: ${env.timeout_mins}"
                sh "sleep 120"
            }
        }
    }
}

      

+1


source







All Articles