How can I do string to integer conversion in a Jenkinsfile?

前端 未结 2 549
心在旅途
心在旅途 2021-01-13 05:58

I have the following in my Jenkinsfile:

pipeline {
    agent none
    environment {
        timeout_mins = 1
    }
    options {
        timeout(time: "$         


        
2条回答
  •  伪装坚强ぢ
    2021-01-13 06:26

    it's not the conversion that's failing, it's just that you can't reference environment variables in the options 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"
                }
            }
        }
    }
    

    this works:

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

提交回复
热议问题