流水线中如何使用条件判断来决定某个stage是否需要执行呢?
答案是 使用 when 这个闭包。 when 放到 stage 中使用,条件成立 就会执行 下面的 step是中的语句的。
Conditionals in a Declarative Pipeline Jenkinsfile
例子1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| pipeline { agent any parameters { choice( choices: ['greeting' , 'silence'], description: '', name: 'REQUESTED_ACTION') }
stages { stage ('Speak') { when { // Only say hello if a "greeting" is requested expression { params.REQUESTED_ACTION == 'greeting' } } steps { echo "Hello, bitwiseman!" } } } }
|
例子2
1 2 3 4 5 6 7 8 9
| 条件判断,模拟 布尔 值判断,真假值判断 when { // case insensitive regular expression for truthy values expression { return token ==~ /(?i)(Y|YES|T|TRUE|ON|RUN)/ } } steps { /* step */ }
|
例子3
1 2 3 4 5 6 7 8 9
| 条件判断 or 或的意思。2个条件 A 和 B 有一个成立就会执行下面的steps的语句。 when { // A or B expression { return A || B } } steps { /* step */ }
|
例子4
1 2 3 4 5 6 7 8 9 10 11
| when { expression { return readFile('pom.xml').contains('mycomponent') } } steps { /* step */ }
|
例子5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| pipeline { agent any parameters { string ( defaultValue: '*', description: '', name : 'BRANCH_PATTERN') booleanParam ( defaultValue: false, description: '', name : 'FORCE_FULL_BUILD') }
stages { stage ('Prepare') { steps { checkout([$class: 'GitSCM', branches: [[name: "origin/${BRANCH_PATTERN}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'LocalBranch']], submoduleCfg: [], userRemoteConfigs: [[ credentialsId: 'bitwiseman_github', url: 'https://github.com/bitwiseman/hermann']]]) } }
stage ('Build') { when { expression { GIT_BRANCH = 'origin/' + sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim() return GIT_BRANCH == 'origin/master' || params.FORCE_FULL_BUILD } } steps { // Freestyle build trigger calls a list of jobs // Pipeline build() step only calls one job // To run all three jobs in parallel, we use "parallel" step // https://jenkins.io/doc/pipeline/examples/#jobs-in-parallel parallel ( linux: { build job: 'full-build-linux', parameters: [string(name: 'GIT_BRANCH_NAME', value: GIT_BRANCH)] }, mac: { build job: 'full-build-mac', parameters: [string(name: 'GIT_BRANCH_NAME', value: GIT_BRANCH)] }, windows: { build job: 'full-build-windows', parameters: [string(name: 'GIT_BRANCH_NAME', value: GIT_BRANCH)] }, failFast: false) } } stage ('Build Skipped') { when { expression { GIT_BRANCH = 'origin/' + sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim() return !(GIT_BRANCH == 'origin/master' || params.FORCE_FULL_BUILD) } } steps { echo 'Skipped full build.' } } } }
|
Optional Stages (≅ if)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| 这个例子,直接在 script 代码块中 直接使用 if 来判断。这个不是很优雅。当然还是更推荐 使用 when 来判断。 pipeline { agent any parameters { booleanParam(name: "RELEASE", defaultValue: false) } stages {
stage("Build") { steps { sh "./gradlew build" } } stage("Publish") { steps { script { if (params.RELEASE) { sh "./gradlew release" } } } } } }
如果执行这个流水线脚本,就算 条件不成立,这个 stage Publish 还是会执行的。 也就是说 这个 stage 执行了,但是 stage 里面的内容想当于没干什么事而已。 这个在 blue ocean 可视化界面下面还是可以看到 stage Publish 显示绿色的。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| 如果换成下面的使用when来判断就不会有这个现象的, 在 blue ocean 可视化界面下面还是可以看到 stage Publish 显示灰色,并且有个跳过的 动作的。 #!/usr/bin/env groovy // see https://jenkins.io/doc/book/pipeline/syntax/
pipeline { agent any parameters { booleanParam(name: "RELEASE", defaultValue: false) } stages {
stage("Build") { steps { sh "./gradlew build" } } stage("Publish") { when { expression { params.RELEASE } } steps { sh "./gradlew release" } } } }
|
对于这个我们也可以把这2个阶段 放到parallel 代码块中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #!/usr/bin/env groovy // see https://jenkins.io/doc/book/pipeline/syntax/
pipeline { agent any parameters { booleanParam(name: "RELEASE", defaultValue: false) } stages {
stage("Build") { steps { sh "./gradlew build" } } stage("Publish") { parallel { stage("Release") { when { expression { params.RELEASE } } steps { sh "./gradlew release" } } stage('Pre-Release') { when { expression { !params.RELEASE } } steps { sh "./gradlew preRelease" } } } } } }
|
One of Many (≅ switch)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #!/usr/bin/env groovy // see https://jenkins.io/doc/book/pipeline/syntax/
pipeline { agent any parameters { booleanParam(name: "RELEASE", defaultValue: false) choice(name: "DEPLOY_TO", choices: ["", "INT", "PRE", "PROD"]) } stages {
stage("Build") { steps { sh "./gradlew build" } } stage("Publish") { parallel { stage('Pre-Release') { when { expression { !params.RELEASE } } steps { sh "./gradlew preRelease" } } stage("Release") { when { expression { params.RELEASE } } steps { sh "./gradlew release" } } } }
stage("Deploy") { steps { script { switch(params.DEPLOY_TO) { case "INT": echo "./deploy.sh int"; break case "PRE": echo "./deploy.sh pre"; break case "PROD": echo "./deploy.sh prod"; break } } } } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #!/usr/bin/env groovy // see https://jenkins.io/doc/book/pipeline/syntax/
pipeline { agent any parameters { booleanParam(name: "RELEASE", defaultValue: false) choice(name: "DEPLOY_TO", choices: ["", "INT", "PRE", "PROD"]) } stages {
stage("Build") { steps { sh "./gradlew build" } } stage("Publish") { parallel { stage('Pre-Release') { when { expression { !params.RELEASE } } steps { sh "./gradlew preRelease" } } stage("Release") { when { expression { params.RELEASE } } steps { sh "./gradlew release" } } } }
stage("Deploy") { parallel { stage("INT") { when { expression { params.DEPLOY_TO == "INT" } } steps { sh "./deploy int" } } stage("PRE") { when { expression { params.DEPLOY_TO == "PRE" } } steps { sh "./deploy.sh pre" } } stage("PROD") { when { expression { params.DEPLOY_TO == "PROD" } } steps { sh "./deploy.sh prod" } } } } } }
|