jenkins实现java项目自动化部署

1 篇文章 0 订阅
1 篇文章 0 订阅

Jenkins流水线脚本案例

pipeline {
    agent any

    //全局变量
    environment {
         git_auth = 'b1c66dc8-8920-44f4-8bb7-2d7cb5cc85671'
         common_url = 'http://172.52.217.174/idt-ha/itd-common.git'
         as_url = 'http://172.52.217.174/idt-ha/itd-as.git'
         gateway_url = 'http://172.52.217.174/idt-ha/itd-gateway.git'
         win_url = 'http://172.52.217.174/idt-ha/itd-win.git'
         alg_url = 'http://172.52.217.174/idt-ha/itd-algo.git'
    }

    parameters {
        //git分支参数
        gitParameter name: 'BRANCH',
                    type: 'Branch',
                    branchFilter: 'origin/(.*)',
                    defaultValue: 'dev',
                    selectedValue: 'DEFAULT',
                    sortMode: 'DESCENDING_SMART',
                    description: '请选择git对应分支'
        //软件多选
        extendedChoice name: 'project_names',
                       type: 'PT_CHECKBOX',
                       description: '请勾选所要部署的项目软件',
                       quoteValue: false,
                       saveJSONParameterToFile: false,
                       value: 'itd-as,itd-gateway,itd-algo',
                       descriptionPropertyValue: 'itd-as,itd-gateway,itd-algo',
                       visibleItemCount: 3,
                       multiSelectDelimiter: ',',
                       defaultValue: 'itd-as,itd-gateway,itd-algo'
    }

    stages {
        stage("1、拉取代码"){
            steps {
                script {
                    echo '获取itd-common相关代码,并移动到指定目录'
                    checkout([$class: 'GitSCM', branches: [[name: "${BRANCH}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${common_url}"]]])
                    sh "rm -rf /var/jenkins_home/workspace/itd-common-yl/*"
                    sh "mv -f /var/jenkins_home/workspace/itd-as-yl/* /var/jenkins_home/workspace/itd-common-yl"
                    echo "编译itd-common"
                    sh "mvn clean install -f /var/jenkins_home/workspace/itd-common-yl/pom.xml -Dmaven.test.skip=true"
                    echo "编译完成itd-common"
                    for (project_name in project_names.tokenize(',')){
                          echo "item项目:${project_name}"
                          if(project_name.equals("itd-as")) {
                              echo '获取itd-as相关代码,并移动到指定目录'
                              checkout([$class: 'GitSCM', branches: [[name: "${BRANCH}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${as_url}"]]])
                              echo "编译itd-as"
                              sh "mvn clean install -f /var/jenkins_home/workspace/itd-as-yl/pom.xml -Dmaven.test.skip=true"
                              echo "编译完成itd-as"
                              echo "移动itd-as.jar到指定目录"
                              sh "sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@172.52.217.161 'rm -rf /iids/docker-compose/itd-as/itd-as.jar' "
                              sh "sshpass -p 123456 scp -r /var/jenkins_home/workspace/itd-as-yl/target/itd-as.jar root@172.52.217.161:/iids/docker-compose/itd-as/"
                          }
                          if(project_name == "itd-gateway") {
                             echo '获取itd-gateway相关代码,并移动到指定目录'
                             checkout([$class: 'GitSCM', branches: [[name: "${BRANCH}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${gateway_url}"]]])
                             sh "rm -rf /var/jenkins_home/workspace/itd-gateway-yl/*"
                             sh "mv -f /var/jenkins_home/workspace/itd-as-yl/* /var/jenkins_home/workspace/itd-gateway-yl"
                             echo "编译itd-gateway"
                             sh "mvn clean install -f /var/jenkins_home/workspace/itd-gateway-yl/pom.xml -Dmaven.test.skip=true"
                             echo "编译完成itd-gateway"
                             echo "移动itd-gateway.jar到指定目录"
                             sh "sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@172.52.217.161 'rm -rf /iids/docker-compose/itd-as/itd-gateway.jar' "
                             sh "sshpass -p 123456 scp -r /var/jenkins_home/workspace/itd-gateway-yl/target/itd-gateway.jar root@172.52.217.161:/iids/docker-compose/itd-gateway/"
                          }
                          if(project_name == "itd-algo") {
                             echo '获取itd-algo相关代码,并移动到指定目录'
                             checkout([$class: 'GitSCM', branches: [[name: "${BRANCH}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${alg_url}"]]])
                             sh "rm -rf /var/jenkins_home/workspace/itd-algo-yl/*"
                             sh "mv -f /var/jenkins_home/workspace/itd-as-yl/* /var/jenkins_home/workspace/itd-algo-yl"
                             echo "移动publish到指定目录"
                             sh "sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@172.52.217.161 'rm -rf /iids/docker-compose/itd-alg/publish' "
                             sh "sshpass -p 123456 scp -r /var/jenkins_home/workspace/itd-algo-yl/IIDS.Algorithm.WebAPI/bin/publish root@172.52.217.161:/iids/docker-compose/itd-alg/publish"
                         }
                    }
                }
            }
        }

        stage('2.执行一键部署脚本') {
          steps {
                 script {
                            sh "sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@172.52.217.161 'cd /iids/docker-compose/ && sh docker-compose-run.sh'"
                             echo "部署成功"
                        }
                 }
        }
    }
}

这个是流水线脚本。可以选择构建分支和模块,下面是页面效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值