首页 > 其他 > 详细

DEVOPS技术实践_21:Pipeline的嵌套以及流程控制的if和case语句

时间:2019-11-19 22:56:33      阅读:111      评论:0      收藏:0      [点我收藏+]

1 if控制语句

使用一个简单的If控制语句

pipeline {
    agent any    
    stages {
        stage(flow control) {
            steps {
                script {
                    if ( 10 == 10) {
                        println "pass"
                    }else {
                        println "failed"
                    }
                }
            }
        }
    }
}

构建结果

技术分享图片

控制台输出

Started by user darren ning
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/jenkins_pipeline_demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (flow control)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
pass
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

2 多个stage嵌套的语法

   pipeline {
    agent any    
    stages {
        stage(Non-Parallel Stage) {
            steps {
                echo This stage will be executed first.
            }
        }
        stage(Parallel Stage) {
            failFast true
            parallel {
                stage(并行一) {
                    steps {
                        echo "并行一"
                    }
                }
                stage(并行二) {
                    steps {
                        echo "并行二"
                    }
                }
                stage(并行三) {
                    stages {
                        stage(Nested 1) {
                            steps {
                                echo "In stage Nested 1 within Branch C"
                            }
                        }
                        stage(Nested 2) {
                            steps {
                                echo "In stage Nested 2 within Branch C"
                            }
                        }
                    }
                }
            }
        }
    }
}

执行

技术分享图片

控制台输出

Started by user darren ning
Obtained when.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
 > git --version # timeout=10
 > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 839c98003111f56e628fa806d80f0e8f2a97349b (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 839c98003111f56e628fa806d80f0e8f2a97349b
Commit message: "Update when.jenkinsfile"
 > git rev-list --no-walk 839c98003111f56e628fa806d80f0e8f2a97349b # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Non-Parallel Stage)
[Pipeline] echo
This stage will be executed first.
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Parallel Stage)
[Pipeline] parallel
[Pipeline] { (Branch: 并行一)
[Pipeline] { (Branch: 并行二)
[Pipeline] { (Branch: 并行三)
[Pipeline] stage
[Pipeline] { (并行一)
[Pipeline] stage
[Pipeline] { (并行二)
[Pipeline] stage
[Pipeline] { (并行三)
[Pipeline] stage
[Pipeline] { (Nested 1)
[Pipeline] echo
并行一
[Pipeline] }
[Pipeline] echo
并行二
[Pipeline] }
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] }
[Pipeline] }
[Pipeline] echo
In stage Nested 1 within Branch C
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Nested 2)
[Pipeline] echo
In stage Nested 2 within Branch C
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

3 case语句

pipeline脚本

pipeline {
    agent any    
    stages {
        stage(Case lab) {
            steps {
                echo This stage will be executed first.
                script{
                    switch("$contraller"){
                    case "Job1":
                        println "This is Job1"
                    break
                    case "Job2":
                        println "This is Job2"
                    break
                    case "Job3":
                        println "This is Job3"
                    break
                    default:
                        echo "############ wrong Job name ############"
                    break
                }
            }
        }
 
    }
}
}

在job配置几个参数

技术分享图片

选择Job1构建

技术分享图片

控制台输出

Started by user darren ning
Obtained when.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
 > git --version # timeout=10
 > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision ad52c819b4e74df5566cc767b8d580ccea363470 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f ad52c819b4e74df5566cc767b8d580ccea363470
Commit message: "Update when.jenkinsfile"
 > git rev-list --no-walk 839c98003111f56e628fa806d80f0e8f2a97349b # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Case lab)
[Pipeline] echo
This stage will be executed first.
[Pipeline] script
[Pipeline] {
[Pipeline] echo
This is Job1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

选择Job3

技术分享图片

控制台输出

Started by user darren ning
Obtained when.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
 > git --version # timeout=10
 > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision ad52c819b4e74df5566cc767b8d580ccea363470 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f ad52c819b4e74df5566cc767b8d580ccea363470
Commit message: "Update when.jenkinsfile"
 > git rev-list --no-walk ad52c819b4e74df5566cc767b8d580ccea363470 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Case lab)
[Pipeline] echo
This stage will be executed first.
[Pipeline] script
[Pipeline] {
[Pipeline] echo
This is Job3
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

选择Job4

在pipeline的脚本里,并没有Job4的选项,看一下构建的结果

Started by user darren ning
Obtained when.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
 > git --version # timeout=10
 > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision ad52c819b4e74df5566cc767b8d580ccea363470 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f ad52c819b4e74df5566cc767b8d580ccea363470
Commit message: "Update when.jenkinsfile"
 > git rev-list --no-walk ad52c819b4e74df5566cc767b8d580ccea363470 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Case lab)
[Pipeline] echo
This stage will be executed first.
[Pipeline] script
[Pipeline] {
[Pipeline] echo
############ wrong Job name ############      #使用的dedault的输出
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

上面就是嵌套语句以及使用if和case语句进行流程控制的几个实验

DEVOPS技术实践_21:Pipeline的嵌套以及流程控制的if和case语句

原文:https://www.cnblogs.com/zyxnhr/p/11892531.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!