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}"
}
敌人总是会在你最不想它出现的地方出现!
原文地址:https://www.cnblogs.com/longhx/p/15741702.html