启动并设置端口
nohup java -jar jenkins.war --httpPort=9000 > jenkins.log 2>&1 &
此处略过创建流水线项目的流程……
def checkHealth(){
i = 0
while(true) {
Thread.sleep(1000)
def response = httpRequest contentType: 'APPLICATION_JSON',
httpMode: "GET",
validResponseCodes: '100:599',
customHeaders: [
[name: "TOKEN", value: "B456skjasdjkf="]
],
url: 'https://localhost:8080'
if(response.status == 200){
return
}else if(i >= 15){
currentBuild.result = 'FAILURE'
}
println("Status: "+response.status)
++i
}
}
pipeline {
agent any
tools {
maven "maven3.6.3"
}
stages {
stage('Build') {
steps {
git credentialsId: 'xxxxx', url: 'gitleye@xxxx.git'
sh "mvn -Dmaven.test.failure.ignore=true clean package"
}
post {
success {
echo '打包成功!move jar...'
sh 'mv /home/lylh/.jenkins/workspace/demo/target/demo-0.0.1-SNAPSHOT.jar /data/jar/demo.jar'
}
}
}
stage('Reboot') {
steps {
echo '部署成功!'
withEnv(['JENKINS_NODE_COOKIE=background_job']) {
sh """
sh /data/jar/leyoupin/start.sh
"""
}
}
}
stage('Health') {
steps {
script{
checkHealth()
echo '启动成功! (?ω?)'
}
}
}
}
}
流水线配置(图1)
启动Shell脚本代码
#!/bin/sh
APP_NAME=demo.jar
APP_PATH=/data/jar/demo
is_exist(){
pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}'`
if [ "$pid" != "" ];then
echo "exist"
return 1;
else
return 0;
fi
}
start(){
is_exist
flag=$?
if [[ $flag == 1 ]];then
echo "${APP_NAME} is already running. execute kill……"
num=0
kill $pid
while [[ $flag == 1 && $num -lt 10 ]]
do
echo "${APP_NAME} killing... ${num}"
is_exist
flag=$?
let "num++"
sleep 1
done
is_exist
if [ $? == 1 ];then
echo 'force kill!'
kill -9 $pid
fi
echo "${APP_NAME} is stop"
else
echo "${APP_NAME} is not running"
fi
cd ${APP_PATH}
file=`find . -name ${APP_NAME%.jar}_temp.jar`
if [ "$file" != "" ];then
rm $APP_NAME
mv leyoupin_temp.jar $APP_NAME
fi
nohup java -jar $APP_NAME --spring.profiles.active=test2 --logging.config=classpath:logback-console.xml > nohup.log 2>&1 &
echo "$APP_PATH/$APP_NAME starting sucess"
}
start
流水线部署成功
|