jenkins实现参数化构建


安装jenkins插件

       安装插件“Extended Choice Parameter plugin”,它可以扩展参数化构建过程,直接在管理界面增加

       

        创建任务时候使用参数构建  pipeline也可以添加参数化构建  

      

        构建任务选择传递参数

           1.先点击Build with Parameters    2.然后点击立即构建

         

         2.没有构建参数的任务就可以直接点击立即构建

          

 参数化构建实例

    

#!groovy

@Library("myjenkinslib@master") _ 

def mytools = new org.tools()

pipeline {
   agent any
   
   environment {
     srcPath="/var/lib/jenkins/workspace/AppServiceProject-Dev-Choice"
     servicename="${servicename}"
   }


   tools {
      // Install the Maven version configured as "M3" and add it to the path.
      maven "M2"
   }

   stages {
   
      stage('GetCode'){
         steps {
         
            // Get some code from a GitHub repository
            //git credentialsId: 'yxhgitlab', url: 'https://192.168.30.111:8090/tf-as/appservice.git'
            git branch: 'dev', credentialsId: 'yxhgitlab', url: 'https://192.10008090/tf-as/appservice.git'

            // To run Maven on a Windows agent, use
            // bat "mvn -Dmaven.test.failure.ignore=true clean package"
         }

         post {
            // If Maven was able to run the tests, even if some of the test
            // failed, record the test results and archive the jar file.
            success {
              print("getCode success")
            }
         }
      }
      
      stage("Build"){
           steps {
              script {
                 javahome = tool "Java1"
                 mvnhome = tool "M2"
                 sh "${mvnhome}/bin/mvn -Pdev clean -DskipTests=true install"
                 try {
                 
                    sh "${workspace}/mvn-sonar.sh"
                    print("代码覆盖率检查成功")
                
                 }
                 catch (Exception err){
                   print("代码覆盖率检查失败")
                 }
                 
              }
            }
            
           post {
              success {
                print("Build success......")
              }
           }
      }
      
      
      stage("Deploy Service"){
           steps {
             script {


        // if ("${servicename}" == "all")
   //{}
   //else
   //{
     //print("${servicename}")
  // }


             switch(env.servicename)
             {
             case "all":
              parallel(
              a:{
                script {
                  sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/register-center/target/register-center-dev.jar dest=/home/admin/app/  mode=777'"
                }
                },
              b:{
               script {
                sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/config-center/target/config-center-dev.jar dest=/home/admin/app/  mode=777'"
                }
              },
            c:{
               script {
                 sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/gateway-zuul/target/gateway-zuul-dev.jar dest=/home/admin/app/  mode=777'"
               }
              },
               d:{
               script {
                 sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/admin/target/admin-dev.jar dest=/home/admin/app/  mode=777'"
               }
              },
               e:{
               script {
                 sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/bizapp/sdc-collect-config/target/sdc-collect-config-dev.jar dest=/home/admin/app/  mode=777'"
               }
              },
               f:{
               script {
                 sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/bizapp/sdc-rule-config/target/sdc-rule-config-dev.jar dest=/home/admin/app/  mode=777'"
               }
              },
               g:{
               script {
                 sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/bizapp/sdc-es-service/target/sdc-es-service-dev.jar dest=/home/admin/app/ mode=777'"
               }
              }
            )
            break
            case "register-center":
            script {
                 sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/register-center/target/register-center-dev.jar dest=/home/admin/app/  mode=777'"
              }
              break
            case "gateway-zuul":
            script {
                 sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/gateway-zuul/target/gateway-zuul-dev.jar dest=/home/admin/app/  mode=777'"
              }
              break
            case "admin":
            script {
                  sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/admin/target/admin-dev.jar dest=/home/admin/app/  mode=777'"
              }
              break
              
            case "sdccollectconfig":
            script {
                 sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/bizapp/sdc-collect-config/target/sdc-collect-config-dev.jar dest=/home/admin/app/  mode=777'"
              }
              break
              
            case "sdcruleconfig":
            script {
                  sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/bizapp/sdc-rule-config/target/sdc-rule-config-dev.jar dest=/home/admin/app/  mode=777'"
              }
              break
              
            case "sdces":
            script {
                  sh "ansible webservers  --user=admin -m copy -a 'src=${srcPath}/bizapp/sdc-es-service/target/sdc-es-service-dev.jar dest=/home/admin/app/ mode=777'"
              }
              break
            }
           }
          }
           post {
              success {
                print("Deploy Service success......")
              }
           }
      }
      

      
      stage("Start"){
       steps {
         script {
           switch(env.servicename)
            {
               case "all":
               parallel(
                a:{
                  script {
                   res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart register'", returnStatus: true)
                   if(res != 0){
                      error("register服务启动失败,本次发布流程终止")
                  }
                }
              },
            b:{
              script { 
                 res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart gateway'", returnStatus: true)
                 if(res != 0){
                     error("gateway服务启动失败,本次发布流程终止")
                 }
                }
              }, 
                
             c:{
              script {     
                 res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart admin'", returnStatus: true)
                 if(res != 0){
                     error("admin服务启动失败,本次发布流程终止")
                 }
                 }
                },
            d:{
                script {  
                  res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart sdccollectconfig'", returnStatus: true)
                  if(res != 0){
                     error("sdccollectconfig服务启动失败,本次发布流程终止")
                 }
                }
             }, 
                
            e:{
                 script {  
                  res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart sdcruleconfig'", returnStatus: true)
                  if(res != 0){
                     error("sdc-rule-config服务启动失败,本次发布流程终止")
                   }
                 }
                },
            f:{
                script {
                 res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart sdces'", returnStatus: true)
                 if(res != 0){
                     error("sdces服务启动失败,本次发布流程终止")
                 }
               }
              }
             )
             break
              case "gateway-zuul":
               script { 
                 res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart gateway'", returnStatus: true)
                 if(res != 0){
                     error("gateway服务启动失败,本次发布流程终止")
                 }
                }
              break
              case "register-center":
              script {
                res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart register'", returnStatus: true)
                if(res != 0){
                   error("register服务启动失败,本次发布流程终止")
                }
              }
              break
              case "admin":
                script {     
                 res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart admin'", returnStatus: true)
                 if(res != 0){
                     error("admin服务启动失败,本次发布流程终止")
                 }
                 }
              break
              case "sdccollectconfig":
              script {  
                  res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart sdccollectconfig'", returnStatus: true)
                  if(res != 0){
                     error("sdccollectconfig服务启动失败,本次发布流程终止")
                 }
                }
              break
              case "sdcruleconfig":
              script {  
                 res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart sdcruleconfig'", returnStatus: true)
                 if(res != 0){
                     error("sdc-rule-config服务启动失败,本次发布流程终止")
                 }
                 }
              break
              case "sdces":
               script {
                 res = sh(script: "ansible webservers --user=admin -m shell -a 'supervisorctl restart sdces'", returnStatus: true)
                 if(res != 0){
                     error("sdces服务启动失败,本次发布流程终止")
                  }
                 }
               break
              }
             }
           }
           post {
              success {
                print("所有应用启动成功,本次流水线执行成功")
              }
              failure {
                print("部署失败")
            }
          }
      }
 }   
}

 可以使用groovy编程语言实现任何业务功能  在流水线中使用脚本编程的时候需要用script{}把脚本包括起来

    

 jenkins流水线自动拉取分支列表

pipeline {
   agent any

   tools {
      // Install the Maven version configured as "M3" and add it to the path.
      maven "M2"
   }

   stages {
      stage('GetCode'){
         steps {
              script {
                git branch: "${env.branch}".split("/")[1], credentialsId: 'yxhgitlab', url: 'https://192.168.30.1:8000/tf-as/appservice.git'
                 
            }
         }

         post {
            // If Maven was able to run the tests, even if some of the test
            // failed, record the test results and archive the jar file.
            success {
              print("getCode success")
            }
         }
      }
      
      stage("Build"){
           steps {
              script {
                 javahome = tool "Java1"
                 
                 //sh "echo '***************************************************'"
                 //sh "${javahome}/bin/java -version"
                 //sh "export JAVA_HOME=${javahome}"
                 
                 mvnhome = tool "M2"
                 
                 sh "${mvnhome}/bin/mvn -Pprod clean -DskipTests=true install"
                 
                  try {
                 
                   //sh "${workspace}/mvn-sonar.sh"
                    print("代码覆盖率检查成功")
                
                 }
                 catch (Exception err){
                   print("代码覆盖率检查失败")
                 }
                
              }
            }
            
           post {
              success {
                print("Build success......")
              }
           }
      }
      
      stage("Deploy"){
           steps {
            script {
                 sh "ansible appserver129 --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/129-app/bizapp/sdc-tele-web/target/sdc-tele-web-prod.jar dest=/data1/taishi/app/sdc-tele-web/'"
                 sh "ansible appserver129 --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/129-app/main/target/main-prod.jar dest=/data1/taishi/app/main/'"
                 sh "ansible appserver129 --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/129-app/bizapp/sdc-tele-mq/target/sdc-tele-mq-prod.jar dest=/data1/taishi/app/sdc-tele-mq/'"
                 sh "ansible appserver129 --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/129-app/bizapp/sdc-tele-receiver/target/sdc-tele-receiver.jar dest=/data1/taishi/app/sdc-tele-receiver/'"
             }
           }
           
           post {
              success {
                print("Deploy success......")
              }
           }
      }
      
      
       stage("Start"){
           steps {
              script {

                    sh "ansible appserver129 --user=admin -m shell -a 'supervisorctl -uadmin -pTransfar@2022 restart main'"
                    sh "ansible appserver129 --user=admin -m shell -a 'supervisorctl -uadmin -pTransfar@2022 restart sdc-tele-mq'"
                    sh "ansible appserver129 --user=admin -m shell -a 'supervisorctl -uadmin -pTransfar@2022 restart sdc-tele-receiver'"
                    sh "ansible appserver129 --user=admin -m shell -a 'supervisorctl -uadmin -pTransfar@2022 restart sdc-tele-web'"

                }
            }
           
           post {
              success {
                print("Start success......")
              }
           }
      }
      
   }
}

            

            

             

              

              

 基于git的tag打包编译