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語句進行流程控制的幾個實驗


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM