且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Jenkinsfile中将字符串转换为整数?

更新时间:2023-02-03 08:51:14

并不是转换失败,只是您不能在options块中引用环境变量.

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

这有效:

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