日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Jenkins 業(yè)務(wù)發(fā)版平滑上線實(shí)戰(zhàn)

背景簡(jiǎn)介:

第一步:拉取git指定分支

Jenkins默認(rèn)一個(gè)項(xiàng)目只能對(duì)應(yīng)一個(gè)git分支來(lái)構(gòu)建,通過(guò)git Parameter插件可以從項(xiàng)目中讀取GIT SCM 配置實(shí)現(xiàn)選擇分支或tag來(lái)構(gòu)建項(xiàng)目。

竹溪ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

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 拉取指定分支

第二步:編譯

通過(guò)mvn編譯java代碼,將編譯后得到的jar包推送至各個(gè)服務(wù)器,其中servers_ips為各個(gè)服務(wù)器ip地址(如: servers_ips = ” 172.1.1.100 172.1.1.101”)。

script {
sh " mvn clean package -DskipTests " #編譯java項(xiàng)目代碼
for (server_ip in servers_ips.tokenize(' ')){
def remote = [:]
remote.name = root
remote.host = server_ip
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId:xxx,keyFileVariable:'identity',passphr aseVariable:'',usernameVariable:'userName')])
{
remote.user = userName
remote.identityFile = identity
sshPut remote: remote, from: "./target/xxx.jar", into: "/data/" #
將jar包推送至服務(wù)器
}}}

第三步:下線指定服務(wù)器

批量修改各個(gè)服務(wù)器上nginx配置,注釋upstream中指定的服務(wù)器,使其下線不提供服務(wù),為之后新代碼上線做準(zhǔn)備,在不影響業(yè)務(wù)的情況下平滑上線。

script {
for (server_ip2 in servers_ips.tokenize(' ')){
def remote = [:]
remote.name = root
remote.host = server_ip2
remote.allowAnyHosts = true

withCredentials([sshUserPrivateKey(credentialsId:xxx,keyFileVariable:'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command:"""
sed -i 's/.*'${指定服務(wù)器ip}'/#&/' /etc/nginx/conf.d/xxx.conf
nginx -s reload
"""
}}}

第四步:重啟服務(wù)運(yùn)行新代碼

在剛下線的服務(wù)器上重啟java項(xiàng)目使其運(yùn)行新的代碼。立即重啟會(huì)導(dǎo)致原有的連接返回5xx,所以在重啟之前sleep幾秒。

sleep 30
def remote = [:]
remote.name = root
remote.host = 指定服務(wù)器ip
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: xxx, keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command:"./prodservice.sh restart"
}

第五步:健康檢查

訪問(wèn)該服務(wù)器后端提供的接口判斷是否可以提供服務(wù)。狀態(tài)碼為200即為健康即可上線提供服務(wù),否則訪問(wèn)3次之后拋出異常中斷Jenkins。服務(wù)啟動(dòng)過(guò)程中進(jìn)行檢查,jenkins會(huì)拋異常并中斷,這里需要加上異常處理繼續(xù)運(yùn)行jenkins。

health_times=0
health_status = ""
while(health_times < 3){
try {
health_status = sh(script: "curl -I -m 10 -o /dev/null -s -w %{http_code} -d '' http://${server_ip}:8080/xxx", returnStdout: true).trim()
} catch(Exception e1) {
echo "服務(wù)沒(méi)起來(lái)" }
if ( health_status == '200' ){
echo "健康"
break
}else if ( health_times < 2) {
sleep 30
echo "不健康,再次嘗試檢測(cè)..."
}else{
error "不健康" }
health_times = health_times +1 }

第六步:上線指定服務(wù)器

通過(guò)健康檢查后,即可批量修改各個(gè)服務(wù)器上的nginx配置,使得該服務(wù)器上線提供服務(wù)。

for (server_ip2 in servers_ips.tokenize(' ')){
remote.name = root
remote.host = server_ip2
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: ssh_credentialsId, keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command:"""
sed -i 's/^#*\\(.*'${server_ip}'\\)/\\1/' /etc/nginx/conf.d/xxx.conf
nginx -s reload
"""
}}

第七步:檢查

在構(gòu)建過(guò)程中循環(huán)curl服務(wù),檢查是否會(huì)出現(xiàn)服務(wù)中斷的情況。下圖7-1為構(gòu)建過(guò)程中的日志。

圖7-1 平滑上線過(guò)程

至此,Jenkins平滑上線完成。


分享標(biāo)題:Jenkins 業(yè)務(wù)發(fā)版平滑上線實(shí)戰(zhàn)
網(wǎng)站路徑:http://www.dlmjj.cn/article/cdihdsg.html