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

前端 未结 2 550
心在旅途
心在旅途 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:08

    As @burnettk says it’s a limitation with the options block. It's not the most elegant but you can use timeout as a step e.g.

    pipeline {
        agent none
        environment {
            timeout_mins = 1
        }
        stages {
            stage("test print") {
                steps {
                    timeout(time: env.timeout_mins.toInteger(), unit: 'MINUTES') {
                        echo "timeout_mins: ${env.timeout_mins}"
                        sh "sleep 120"
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 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"
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题