1 Star 0 Fork 4

zhouwei/jenkins_maven

forked from 盛宇/jenkins_maven 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Jenkinsfile 8.86 KB
一键复制 编辑 原始数据 按行查看 历史
zhouwei 提交于 2024-12-16 05:03 . update Jenkinsfile.
pipeline {
agent any
environment {
mvnwithjdk8 = '/usr/local/maven/bin/mvn'
killport = '/home/zw/script/kill.sh'
serverport = '1111'
jenkinstemptransfer = '/home/zw/jenkins-temp-transfer'
jarpath = '/home/zw/deploy'
jarname = 'example-0.0.1-SNAPSHOT.jar'
project_name = 'example-SNAPSHOT'
sleeptime = '30'
}
stages {
stage('构建') {
steps {
script{
sh '''
echo "type: $type"
$mvnwithjdk8 clean
$mvnwithjdk8 package
'''
}
}
}
stage('备份') {
steps {
archiveArtifacts artifacts: 'target/'
}
}
stage('发布') {
steps {
script{
echo "环境为:$environment 分支为:$branch"
if ("$server" == "all") {
sshPublisher(publishers: [sshPublisherDesc(configName: "$server1", transfers: [sshTransfer(execCommand: "$killport $serverport", execTimeout: 120000)], verbose: true)])
sshPublisher(publishers: [sshPublisherDesc(configName: "$server1", transfers: [sshTransfer(execCommand: "mkdir -p $jenkinstemptransfer; mkdir -p $jarpath; mv $jenkinstemptransfer/$jarname $jarpath; source /etc/profile; nohup java -Dproject.name=$server1 -jar $jarpath/$jarname --server.port=1111 >> $jarpath/nohup.out &", execTimeout: 120000, patternSeparator: '[, ]+', removePrefix: 'target/', sourceFiles: 'target/*.*jar')], verbose: true)])
sshPublisher(publishers: [sshPublisherDesc(configName: "$server1", transfers: [sshTransfer(execCommand: "tail -n 50 $jarpath/nohup.out", execTimeout: 120000)], verbose: true)])
sleep 5
for (i=0; i<10; i++) {
prodport = sh (script: "ssh zw@$server1 netstat -tnlp | grep $serverport | awk -F : '{print \$4}' ",returnStdout:true).trim()
prodport2 = sh (script: "ssh zw@$server1 netstat -tnlp | grep $serverport | awk -F ':' '{print \$2}' | awk -F ' ' '{print \$1}' ",returnStdout:true).trim()
echo "端口为 $prodport $prodport2"
echo "$prodport"
echo "$prodport2"
if ("${prodport}" == "$serverport" || "${prodport2}" == "$serverport") {
echo "检测到服务端口启动,服务运行成功,下面进行健康状态检查"
for (i=0; i<10; i++) {
status = sh (script: "curl -s --connect-timeout 2 -m 2 http://${server1}:1111/",returnStdout:true,returnStatus:true)
// status = sh (script: "curl -s --connect-timeout 2 -m 2 https://back.gpyh.com/healthCheck.html | grep 'success'",returnStdout:true,returnStatus:true)
echo "***************"
echo "result $status"
if ("${status}" == "0") {
echo "服务健康状态检查正常,可以继续部署!正在部署第二台"
sshPublisher(publishers: [sshPublisherDesc(configName: "$server2", transfers: [sshTransfer(execCommand: "$killport $serverport", execTimeout: 120000)], verbose: true)])
sshPublisher(publishers: [sshPublisherDesc(configName: "$server2", transfers: [sshTransfer(execCommand: "mkdir -p $jenkinstemptransfer; mkdir -p $jarpath; mv $jenkinstemptransfer/$jarname $jarpath; source /etc/profile; nohup java -Dproject.name=$server2 -jar $jarpath/$jarname --server.port=1111 >> $jarpath/nohup.out &", execTimeout: 120000, patternSeparator: '[, ]+', removePrefix: 'target/', sourceFiles: 'target/*.*jar')], verbose: true)])
sshPublisher(publishers: [sshPublisherDesc(configName: "$server2", transfers: [sshTransfer(execCommand: "tail -n 50 $jarpath/nohup.out", execTimeout: 120000)], verbose: true)])
break
}
else {
echo "服务健康状态检查不正常,正在重新获取健康状态。。。"
}
sleep 3
}
if ("${status}" != "0") {
echo "服务健康状态检查不正常,请等待正常后再继续部署!当前只部署了 $server1"
}
break
}
else {
echo "服务正在启动请等待。。。"
}
sleep 3
}
if ("${prodport}" != "$serverport" && "${prodport2}" != "$serverport" ) {
echo "服务启动失败,请联系对应服务负责人或运维排查问题"
exit 1
}
}
else {
sshPublisher(publishers: [sshPublisherDesc(configName: "$server", transfers: [sshTransfer(execCommand: "$killport $serverport", execTimeout: 120000)], verbose: true)])
sshPublisher(publishers: [sshPublisherDesc(configName: "$server", transfers: [sshTransfer(execCommand: "mkdir -p $jenkinstemptransfer; mkdir -p $jarpath; mv $jenkinstemptransfer/$jarname $jarpath; source /etc/profile; nohup java -Dproject.name=test -jar $jarpath/$jarname --server.port=1111 >> $jarpath/nohup.out 2>&1 &", execTimeout: 120000, patternSeparator: '[, ]+', removePrefix: 'target/', sourceFiles: 'target/*.*jar')], verbose: true)])
sshPublisher(publishers: [sshPublisherDesc(configName: "$server", transfers: [sshTransfer(execCommand: "tail -n 50 $jarpath/nohup.out", execTimeout: 120000)], verbose: true)])
sleep 5
for (i=0; i<10; i++) {
prodport = sh (script: "ssh zw@$server netstat -tnlp | grep $serverport | awk -F : '{print \$4}' ",returnStdout:true).trim()
prodport2 = sh (script: "ssh zw@$server netstat -tnlp | grep $serverport | awk -F ':' '{print \$2}' | awk -F ' ' '{print \$1}' ",returnStdout:true).trim()
echo "端口为"
echo "端口为 $prodport $prodport2"
echo "$prodport"
echo "$prodport2"
if ("${prodport}" == "$serverport" || "${prodport2}" == "$serverport") {
echo "检测到服务端口启动,服务运行成功,下面进行健康状态检查"
for (i=0; i<10; i++) {
status = sh (script: "curl -s --connect-timeout 2 -m 2 http://${server}:1111/",returnStdout:true,returnStatus:true)
// status = sh (script: "curl -s --connect-timeout 2 -m 2 https://back.gpyh.com/healthCheck.html | grep 'success'",returnStdout:true,returnStatus:true)
echo "***************"
echo "result $status"
if ("${status}" == "0") {
echo "服务健康状态检查正常,如果有第二台要部署可以继续部署!"
break
}
else {
echo "服务健康状态检查不正常,正在重新获取健康状态。。。"
}
sleep 3
}
if ("${status}" != "0") {
echo "服务健康状态检查不正常,如果有第二台要部署,请等待正常后再继续部署!"
}
break
}
else {
echo "服务正在启动请等待。。。"
}
sleep 3
}
if ("${prodport}" != "$serverport" && "${prodport2}" != "$serverport" ) {
echo "服务启动失败,请联系对应服务负责人或运维排查问题"
exit 1
}
}
}
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/justinzw/jenkins_maven.git
git@gitee.com:justinzw/jenkins_maven.git
justinzw
jenkins_maven
jenkins_maven
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385