美文网首页
配置流水线时,配置了参数,如何在jenkinsfile中覆盖参数

配置流水线时,配置了参数,如何在jenkinsfile中覆盖参数

作者: 87d6dc4b11a7 | 来源:发表于2022-04-08 15:00 被阅读0次

    配置流水线时,配置了参数,如何在jenkinsfile中覆盖参数的值?
    如下图,我在流水线中配置了fullTime参数,默认值为11111111


    如何在jenkinsfile中覆盖该参数的值呢?
    node('openjdk8') {
        def date = new Date()
        def time = date.format('yyyyMMddHHmmss')
        def msec = System.currentTimeMillis()
        
        if (fullTime == "11111111"){
            fullTime = "${time}" + "${msec}"
            echo "${fullTime}"
        }
        
        stage('test') {
            echo "${fullTime}"
        }
    }
    

    输出结果中:

    2022-04-08 13:45:11.199 | . [Pipeline] {
    2022-04-08 13:45:11.224 | . [Pipeline] echo
    2022-04-08 13:45:11.230 | . 202204080545111649396711212
    2022-04-08 13:45:11.248 | . [Pipeline] stage
    2022-04-08 13:45:11.253 | . [Pipeline] { (test)
    2022-04-08 13:45:11.273 | . [Pipeline] echo
    2022-04-08 13:45:11.276 | . 202204080545111649396711212
    2022-04-08 13:45:11.279 | . [Pipeline] }
    2022-04-08 13:45:11.291 | . [Pipeline] // stage
    2022-04-08 13:45:11.298 | . [Pipeline] }
    2022-04-08 13:45:11.313 | . [Pipeline] // node
    2022-04-08 13:45:11.323 | . [Pipeline] End of Pipeline
    2022-04-08 13:45:11.356 | . Finished: SUCCESS
    

    但是,如果使用下面的jenkinsfile再次定义fullTime,此时jenkinsfile中定义的fullTime作用范围只在if结构内,所以输出仍为11111111

    node('openjdk8') {
        def date = new Date()
        def time = date.format('yyyyMMddHHmmss')
        def msec = System.currentTimeMillis()
        
        if (fullTime == "11111111"){
            def fullTime = "${time}" + "${msec}"
            echo "${fullTime}"
        }
        
        stage('test') {
            echo "${fullTime}"            //输出结果为11111111
        }
    }
    

    相关文章

      网友评论

          本文标题:配置流水线时,配置了参数,如何在jenkinsfile中覆盖参数

          本文链接:https://www.haomeiwen.com/subject/didysrtx.html