当前位置 : 主页 > 手机开发 > harmonyos >

jenkins实现参数化构建

来源:互联网 收集:自由互联 发布时间:2023-08-26
安装jenkins插件 安装插件“Extended Choice Parameter plugin”,它可以扩展参数化构建过程,直接在管理界面增加 创建任务时候使用参数构建 pipeline也可以添加参数化构建 构建任务选择传递参数

安装jenkins插件

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

       

jenkins实现参数化构建_jar

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

      

jenkins实现参数化构建_启动失败_02

        构建任务选择传递参数

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

         

jenkins实现参数化构建_jar_03

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

          

jenkins实现参数化构建_启动失败_04

 参数化构建实例

    

jenkins实现参数化构建_jar_05

#!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("部署失败")
            }
          }
      }
 }   
}

View Code

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

    

jenkins实现参数化构建_jar_06

 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......")
              }
           }
      }
      
   }
}

View Code            

jenkins实现参数化构建_git_07

            

jenkins实现参数化构建_jar_08

             

jenkins实现参数化构建_git_09

              

jenkins实现参数化构建_jar_10

              

jenkins实现参数化构建_jar_11

       设置拉取git分支下拉列表的仓库地址和用户认证

         

jenkins实现参数化构建_jar_12

         

jenkins实现参数化构建_git_13

          

jenkins实现参数化构建_jar_14

         

jenkins实现参数化构建_git_15

 基于git的tag打包编译

    

jenkins实现参数化构建_jar_16

直接使用pipeline语法

       

jenkins实现参数化构建_启动失败_17

       

jenkins实现参数化构建_jar_18

       

jenkins实现参数化构建_启动失败_19

    pipeline必须配合jenkinsfile才能实现动态选择分支,如果只用pipeline就没有地方配置查询git项目分支列表的用户

创建自由风格流水线

         

jenkins实现参数化构建_jar_20

        1.添加git参数

         

jenkins实现参数化构建_git_21

     2.源码管理

       

jenkins实现参数化构建_启动失败_22

     3.构建shell脚本

          

jenkins实现参数化构建_jar_23

    4.构建操作 查看输出日志

           

jenkins实现参数化构建_启动失败_24

        

jenkins实现参数化构建_git_25

 

【文章原创作者:阿里云代理 http://www.558idc.com/aliyun.html 复制请保留原URL】
上一篇:GitLab缓存问题
下一篇:没有了
网友评论