zoukankan      html  css  js  c++  java
  • DEVOPS技术实践_21:Pipeline的嵌套以及流程控制的if和case语句

    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语句进行流程控制的几个实验

  • 相关阅读:
    JAVA URI URL 区别
    超轻量级DI容器框架Google Guice与Spring框架的区别教程详解及其demo代码片段分享
    开源框架:Apache的DBUtils框架
    java url openConnection get post 请求
    java 使用jxl poi 操作excel
    如何用Curl 来post xml 数据
    Linux Shell脚本编程--Linux特殊符号大全
    linux查看各服务状态以及开启和关闭
    eclipse中点不出来提示
    cocos2dx切换场景
  • 原文地址:https://www.cnblogs.com/zyxnhr/p/11892531.html
Copyright © 2011-2022 走看看