当前位置: 首页 > 科技观察

Jenkins业务版顺利上线

时间:2023-03-16 22:13:48 科技观察

背景介绍:原版本要等到晚上10点访问量最少,期间服务中断产生大量告警。为了不影响业务,现在需要对jenkins进行修改,使其顺利上线。具体操作步骤如下:第一步:拉取git的指定分支。Jenkins默认只能构建一个git分支对应的项目。通过git参数插件,可以从项目中读取GITSCM配置来选择分支或标签来构建项目。pipeline{parameters{gitParameter(name:'BRANCH_TAG',type:'PT_BRANCH_TAG',defaultValue:'master')}stage('gitpull'){steps{checkout([$class:'GitSCM',branches:[[name:"${params.BRANCH_TAG}"]],doGenerateSubmoduleConfigurations:false,extensions:[],gitTool:'Default',submoduleCfg:[],userRemoteConfigs:[[url:'http://xxx.git',credentialsId:'xxx',]]])}}}图1-1拉取指定分支步骤2:通过mvn编译java代码,将编译好的jar包推送到各个服务器,其中servers_ips为各个服务器的ip地址(如:servers_ips="172.1.1.100172.1.1.101").script{sh"mvncleanpackage-DskipTests"#Compilejavaprojectcodefor(server_ipinservers_ips.tokenize('')){defremote=[:]remote.name=rootremote.host=server_ipremote.allowAnyHosts=truewithCredentials([sshUserPrivateKey(credentialsId:xxx,keyFileVariable:'identity',passphraseVariable:'',usernameVariable:'userName')]){remote.user=userNameremote.identityFile=identitysshPutremote:remote,from:"./target/xxx.jar",into:"/data/"#pushjar包到服务器}}}第三步:下线指定服务器批量修改每台服务器上的nginx配置,注解指定服务器上游,下线不提供服务,为以后新代码上线做准备,在不影响业务的情况下顺利上线。脚本{for(server_ip2inservers_ips.tokenize('')){defremote=[:]remote.name=rootremote.host=server_ip2remote.allowAnyHosts=truewithCredentials([sshUserPrivateKey(credentialsId:xxx,keyFileVariable:'identity',passphraseVariable:'',usernameVariable:'userName')]){remote.user=userNameremote.identityFile=identitysshCommandremote:remote,command:"""sed-i's/.*'${指定服务器ip}'/#&/'/etc/nginx/conf.d/xxx.confnginx-sreload"""}}}第四步:重启服务运行新代码在刚刚下线的服务器上重启java项目使其运行新代码。立即重启会导致原来的连接返回5xx,所以重启前休眠几秒。sleep30defremote=[:]remote.name=rootremote.host=指定服务器ipremote.allowAnyHosts=truewithCredentials([sshUserPrivateKey(credentialsId:xxx,keyFileVariable:'identity',passphraseVariable:'',usernameVariable:'userName')]){remote.user=userNameremote.identityFile=identitysshCommandremote:remote,command:"./prodservice.shrestart"}第五步:健康检查访问服务器后台提供的接口,判断是否可以提供服务。如果状态码为200,则表示健康,可以上线提供服务。否则会在访问3次后抛出异常中断Jenkins。查看在服务启动过程中,jenkins会抛出异常并中断,这里需要添加异常处理才能继续运行jenkins。health_times=0health_status=""while(health_times<3){try{health_status=sh(script:"curl-I-m10-o/dev/null-s-w%{http_code}-d''http://${server_ip}:8080/xxx",returnStdout:true).trim()}catch(Exceptione1){echo"servicenotup"}if(health_status=='200'){echo"health"break}elseif(health_times<2){sleep30echo"Unhealthy,trytodetectagain..."}else{error"unhealthy"}health_times=health_times+1}第六步:指定服务器通过健康检查后,可以批量修改每个服务器上的nginx配置,使服务器上线提供服务。for(server_ip2inservers_ips.tokenize('')){remote.name=rootremote.host=server_ip2remote.allowAnyHosts=truewithCredentials([sshUserPrivateKey(credentialsId:ssh_credentialsId,keyFileVariable:'identity',passphraseVariable:'',usernameVariable:'userName')]){remote.user=userNameremote.identityFile=identitysshCommandremote:remote,command:"""sed-i's/^#*\\(.*'${server_ip}'\\)/\\1/'/etc/nginx/conf.d/xxx.confnginx-sreload"""}}第七步:检查curl服务在构建过程中是否循环,检查是否会出现服务中断。下图7-1是构建过程中的日志。图7-1平滑启动流程至此,Jenkins的平滑启动就完成了。