DEVOPS技术实践_22:根据参数传入条件控制执行不同stage

╄→гoц情女王★ 提交于 2019-12-05 05:04:29

前面学习了参数的传递和调用,下面研究一下根据参数作为条件执行不同的stage

使用叫when 和expression控制某一个stage的运行,

运行场景例如写了多个stage,这个pipeline脚本执行执行冒烟测试,和集成测试。有时候,希望快速执行冒烟测试,想根据结果看看,不一定一上来就执行集成测试。为了达到这种控制效果,我们就需要使用逻辑控制。在pipeline中就使用when 和expression两个命令。例如,如果json文件中冒烟测试变量为true,我就只执行冒烟测试的stage,其他和冒烟测试无关的stage我就不去执行。如果冒烟测试变量值为false,也就是默认要跑集成测试(不跑冒烟测试)。下面分两个类型测试就好

1 修改json文件

{
"NAME":"Lucy",
"AGE":"18",
"PHONE_NUMBER":"13912345678",
"ADDRESS":"Haidian Beijing",
"EMAIL":"lucy@demo.com",
"GENDER":"male",
"SMOKE":"false",
"IS_MARRY":"false"
}

test.json文件基础上加了一个变量 SMOKE, 默认值是false。

2 使用的jenkinsfile文件

 import hudson.model.*;
    pipeline{
    agent any
    environment {
    INPUT_JSON = "/tmp/test.json"
    }
    stages{
        stage("Hello Pipeline") {
            steps {
                script {
                    println "Hello Pipeline!"
                    println env.JOB_NAME
                    println env.BUILD_NUMBER
                }
            }
        }
 
    stage("Init paramters in json") {
        steps {
            script {
 
                println "read josn input file"
                    json_file = INPUT_JSON? INPUT_JSON.trim() : ""
                    prop = readJSON file : json_file
                    name = prop.NAME? prop.NAME.trim() : ""
                    println "Name:" + name
                    age = prop.AGE? prop.AGE.trim() : ""
                    println "Age:" + age
                    phone = prop.PHONE_NUMBER? prop.PHONE_NUMBER.trim() : ""
                    println "Phone:" + phone
                    address = prop.ADDRESS? prop.ADDRESS.trim() : ""
                    println "Address:" + address
                    email = prop.EMAIL? prop.EMAIL.trim() : ""
                    println "Email:" + email
                    gender = prop.GENDER? prop.GENDER.trim() : ""
                    println "Gender:" + gender
                    is_marry = prop.IS_MARRY? prop.IS_MARRY.trim() : false
                    println "is_marry:" + is_marry
                    is_smoke = prop.SMOKE? prop.SMOKE : false
                    println "is_smoke:" + is_smoke
                }
            }
        }
    stage("call a method") {
            steps {
                script {
                    println "send the parameter as map type"
                    model_call = load env.JENKINS_HOME + "/moodlegroovy/model.groovy"
                    model_call.getUserInfo(name:name, age:age, phone:phone, address:address, email:email, gender:gender, is_marry:is_marry)
                }
            }
        }
        stage("check serive up") {
            when {
                expression { return  ("$is_smoke" == "true")}
            }
            steps {
                script {
                    println "SMOKE TEST: check service startup"
                }
            }
        }

        stage("check UI login") {
            when {
                expression {
                    return ("$is_smoke" == "true")
                }
            }
            steps {
                script {
                    println "SMOKE TEST: check UI login success"
                }
            }
        }
        
        stage("Integrate-ModelA") {
            when {
                expression {
                    return ("$is_smoke" == "false")
                }
            }
            steps {
                script {
                    println "Integrate-ModelA"
                }
            }
        }
        
        stage("Integrate-ModelB") {
            when {
                expression {
                    return ("$is_smoke" == "false")
                }
            }
            steps {
                script {
                    println "Integrate-ModelB"
                }
            }
        }
     }
}

3 执行打印输出

控制台输出

Started by user darren ning
Obtained pipeline.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 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0
Commit message: "Update pipeline.jenkinsfile"
 > git rev-list --no-walk ad52c819b4e74df5566cc767b8d580ccea363470 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Pipeline)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Pipeline!
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo
91
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Init paramters in json)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
read josn input file
[Pipeline] readJSON
[Pipeline] echo
Name:Lucy
[Pipeline] echo
Age:18
[Pipeline] echo
Phone:13912345678
[Pipeline] echo
Address:Haidian Beijing
[Pipeline] echo
Email:lucy@demo.com
[Pipeline] echo
Gender:male
[Pipeline] echo
is_marry:false
[Pipeline] echo
is_smoke:false
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (call a method)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
send the parameter as map type
[Pipeline] load
[Pipeline] { (/root/.jenkins/moodlegroovy/model.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] echo

    Lucy come from Haidian Beijing, he is 18 old. he's phone number is
    13912345678, or you can contact he via lucy@demo.com, he is not marry yet.
    
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (check serive up)
Stage "check serive up" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (check UI login)
Stage "check UI login" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integrate-ModelA)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Integrate-ModelA
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integrate-ModelB)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Integrate-ModelB
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

4 修改json文件,把smoke的值改为true

{
"NAME":"Lucy",
"AGE":"18",
"PHONE_NUMBER":"13912345678",
"ADDRESS":"Haidian Beijing",
"EMAIL":"lucy@demo.com",
"GENDER":"male",
"SMOKE":"false",
"IS_MARRY":"false"
} 

5 点击构建测试

在最后的的两个false没有输出,true输出

控制台输出

Started by user darren ning
Obtained pipeline.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 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0
Commit message: "Update pipeline.jenkinsfile"
 > git rev-list --no-walk 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Pipeline)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Pipeline!
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo
92
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Init paramters in json)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
read josn input file
[Pipeline] readJSON
[Pipeline] echo
Name:Lucy
[Pipeline] echo
Age:18
[Pipeline] echo
Phone:13912345678
[Pipeline] echo
Address:Haidian Beijing
[Pipeline] echo
Email:lucy@demo.com
[Pipeline] echo
Gender:male
[Pipeline] echo
is_marry:false
[Pipeline] echo
is_smoke:true
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (call a method)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
send the parameter as map type
[Pipeline] load
[Pipeline] { (/root/.jenkins/moodlegroovy/model.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] echo

    Lucy come from Haidian Beijing, he is 18 old. he's phone number is
    13912345678, or you can contact he via lucy@demo.com, he is not marry yet.
    
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (check serive up)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
SMOKE TEST: check service startup
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (check UI login)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
SMOKE TEST: check UI login success
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integrate-ModelA)
Stage "Integrate-ModelA" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integrate-ModelB)
Stage "Integrate-ModelB" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

实验完成


参考文献:https://blog.csdn.net/u011541946/article/details/89322510 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!