zoukankan      html  css  js  c++  java
  • Jenkins条件判断

    Jenkins条件判断

    Jenkins条件判断又两条关键指令:

    input

    when

    前者可以进行卡点交互,关键步骤可以设置二次确认。

    input语法

    详情请见官网文档。官网文档引用语法上可能有些问题,后面会有解释。

    https://www.jenkins.io/zh/doc/book/pipeline/syntax/#input
    

    input会弹出一个弹窗,有“继续”和“停止”两个按钮。其中“继续”按钮可以配置文本,点击后继续流水线;“停止”按钮则会在此处终端流水线,不再进行任何步骤。

    input配置项:

    • message弹窗的内容
    • id默认为stage名称
    • ok配置“继续”按钮的文本
    • submitter可选的以逗号分隔的用户列表或允许提交 input 的外部组名。默认允许任何用户。
    • submitterParameter环境变量的可选名称。如果存在,用 submitter 名称设置。(未验证)
    • parameters定义参数,也可以定义下拉选项,进行条件选择
    stage("stage 2: deploy") {
        input {
            message "Should we continue?"
            ok "Yes, we should."
            parameters {
                string(name: 'xxxxxxxxxxxxxxxxxxxxxxxxxx', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
            }
        }
        steps {
            sh 'echo select option: ${xxxxxxxxxxxxxxxxxxxxxxxxxx}'
            sh "env"
        }
    }
    

    注意:官方文档引用语法echo 'select option: ${xxxxxxxxxxxxxxxxxxxxxxxxxx}'经验证是无效的,改成sh 'echo select option: ${xxxxxxxxxxxxxxxxxxxxxxxxxx}'则能正常使用

    显示如下:

    Paramiter配置多选
    stage("stage 2: deploy") {
        input {
            message "Should we continue?"
            ok "Yes, we should."
            parameters {
                choice(name: 'PERSON', choices: ['Mr Jenkins', 'Mr Pipeline'], description: 'Who should I say hello to?')
            }
        }
        steps {
            echo 'Test dingding notify'
            sh 'echo select option: ${PERSON}'
            sh "env"
        }
    }
    

    效果图如下

    When语法

    语法详情参考官网

    https://www.jenkins.io/zh/doc/book/pipeline/syntax/#when
    

    when语句必须放在stage第一行,所有条件都满足才会执行该语句。可以用它来配置特性分支合并到主干的流程。

    参数如下

    • branch语法:when { branch 'master' }注意,这只适用于多分支流水线。
    • environment语法:when { environment name: 'DEPLOY_TO', value: 'production' }
    • expression语法:when { expression { return params.DEBUG_BUILD } }
    • not语法:when { not { branch 'master' } }
    • allOf语法:when { allOf { branch 'master'; environment name: 'DEPLOY_TO', value: 'production' } }
    • anyOf语法:when { anyOf { branch 'master'; branch 'staging' } }

    完整示例如下:

    注意:Input的parameter声明周期只在当前步骤,其它步骤如果要使用,需用脚本将其赋值到env全局变量上。

    pipeline {
        agent any
        parameters {
            string(name: 'testTag', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
        }
        stages {
            stage("stage 1: select project") {
                steps {
                    sh 'echo ${testTag}. Pleace select project'
                }
            }
            stage("stage 2: deploy") {
                input {
                    message "Should we continue?"
                    ok "Yes, we should."
                    parameters {
                        choice(name: 'PERSON', choices: ['Jenkins', 'Pipeline'], description: 'Who should I say hello to?')
                    }
                }
                steps {
                    echo 'Test dingding notify'
                    sh 'echo select option: ${PERSON}'
                    script {
                        env.PERSON = "${PERSON}"
                    }
                    sh 'env'
                }
            }
            stage("stage 3: When Jenkins") {
                when { 
                    expression { return PERSON == 'Jenkins' } 
                }
                steps {
                    sh 'echo ${PERSON}. exe'
                }
            }
            stage("stage 4: When Pipeline") {
                when { expression { return PERSON == 'Pipeline' } }
                steps {
                    sh 'echo ${PERSON}. exe'
                }
            }
        }
        post {
            always {
                echo 'This will always run'
                
            }
            success {
                echo 'successful'
            }
            failure {
                echo 'failed'
            }
        }
    }
    
    

    附录

    问题1:WorkflowScript: 15: Missing required parameter: "message"

    stage("stage 2: deploy") {
        steps {
            echo 'Test dingding notify'
            input {
                message "Should we continue?"
                ok "Yes, we should."
                submitter "alice,bob"
                parameters {
                    string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
            	}
        	}
        	echo 'select option: ${PERSON}'
        }
    }
    

    input报错,说缺失关键参数message。原因是input是一个step,与steps同级。将input移出steps即可。

    问题2:groovy.lang.MissingPropertyException: No such property: PERSON for class: groovy.lang.Binding

    见when的代码示例。原因是input参数声明周期只存在当前步骤,如果别的步骤想引用它的值,需添加script将变量赋值到全局上。

    script {
    	env.PERSON = "${PERSON}"
    }
    
    敌人总是会在你最不想它出现的地方出现!
  • 相关阅读:
    『TensorFlow』模型保存和载入方法汇总
    『cs231n』作业2选讲_通过代码理解Dropout
    『cs231n』作业2选讲_通过代码理解优化器
    『科学计算』图像检测微型demo
    『cs231n』作业1选讲_通过代码理解KNN&交叉验证&SVM
    大数据技术之_16_Scala学习_13_Scala语言的数据结构和算法_Scala学习之旅收官之作
    IDEA 取消参数名称(形参名)提示
    大数据技术之_16_Scala学习_12_设计模式+泛型、上下界、视图界定、上下文界定、协变逆变不变
    大数据技术之_16_Scala学习_11_客户信息管理系统+并发编程模型 Akka+Akka 网络编程-小黄鸡客服案例+Akka 网络编程-Spark Master Worker 进程通讯项目
    大数据技术之_16_Scala学习_10_使用递归的方式去思考,去编程+作业07/08/09
  • 原文地址:https://www.cnblogs.com/longhx/p/15741702.html
Copyright © 2011-2022 走看看