Jenkins conditional judgment
Jenkins condition judgment has two more key instructions:
input,
when
The former can perform card point interaction, and the key step can set secondary confirmation.
input syntax
Please refer to the official website for details. There may be some problems with the reference syntax of the official website documents, which will be explained later.
https://www.jenkins.io/zh/doc/book/pipeline/syntax/#input
input will pop up a pop-up window with "continue" and "stop" buttons. The "continue" button can configure the text, and click to continue the pipeline; The "stop" button will terminate the pipeline here without any steps.
input configuration item:
- Contents of the message pop-up window
- The default id is the stage name
- ok configure the text of the Continue button
- submitter optional comma separated list of users or external group names that allow submission of input. Any user is allowed by default.
- submitterParameter optional name of the environment variable. If it exists, set it with the submitter name. (not verified)
- Parameters defines parameters. You can also define drop-down options to select conditions
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" } }
Note: the official document reference syntax echo 'select option: ${xxxxxxxxxxxxxxxxxxxxx}' is verified to be invalid. Change it to SH 'echo select option: ${xxxxxxxxxxxxxxxxxxxxxx}' and it can be used normally
The display is as follows:
Parameter configure multiple selections
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" } }
The renderings are as follows
When syntax
Please refer to the official website for syntax details
https://www.jenkins.io/zh/doc/book/pipeline/syntax/#when
The when statement must be placed on the first line of the stage, and the statement will not be executed until all conditions are met. It can be used to configure the process of merging feature branches into the trunk.
The parameters are as follows
- Branch syntax: when {branch 'master'} note that this applies only to multi branch pipelines.
- Environment syntax: when {environment name: 'deploy_to', value: 'production'}
- Expression syntax: when {expression {return params. Debug_build}}
- Not syntax: when {not {branch 'master'}}
- allOf syntax: when {allOf {branch 'master'; environment name: 'deploy_to', value: 'production'}} and
- anyOf syntax: when {anyOf {branch 'master'; branch 'staging'}} or
A complete example is as follows:
Note: the parameter declaration cycle of Input is only in the current step. If other steps are to be used, they need to be assigned to the env global variable by script.
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' } } }
appendix
Question 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}' } }
An error is reported in input, saying that the key parameter message is missing. The reason is that input is a step, which is the same level as steps. Just move input out of steps.
Question 2: groovy lang.MissingPropertyException: No such property: PERSON for class: groovy. lang.Binding
See the code example for when. The reason is that the input parameter declaration cycle only exists in the current step. If other steps want to reference its value, you need to add script to assign the variable to the global.
script { env.PERSON = "${PERSON}" }