启动并设置端口
nohup java -jar jenkins.war --httpPort=9000 > jenkins.log 2>&1 &
此处略过创建流水线项目的流程……
//使用java和Groovy 编写脚本语言
def checkHealth(){
i = 0
while(true) {
// 使用沙盒模式要写类的全路径名称
Thread.sleep(1000)
def response = httpRequest contentType: 'APPLICATION_JSON',
httpMode: "GET",
// 设置允许响应的状态,不设置500和404的状态会终止部署
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脚本编写
pipeline {
agent any
// 加载插件
tools {
// Install the Maven version configured as "M3" and add it to the path.
maven "maven3.6.3"
}
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
// 在流水线使用git配置,见图1
git credentialsId: 'xxxxx', url: 'gitleye@xxxx.git'
// Run Maven on a Unix agent.
sh "mvn -Dmaven.test.failure.ignore=true clean package"
// To run Maven on a Windows agent, use
// bat "mvn -Dmaven.test.failure.ignore=true clean package"
}
post {
// If Maven was able to run the tests, even if some of the test
// failed, record the test results and archive the jar file.
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 '部署成功!'
// 使用这段代码在jenkins部署成功后,不会kill掉这里面启动的进程
withEnv(['JENKINS_NODE_cookie=background_job']) {
sh """
sh /data/jar/leyoupin/start.sh
"""
}
}
}
stage('Health') {
steps {
script{
// 调用探活方法
checkHealth()
echo '启动成功! (✪ω✪)'
}
}
}
}
}
流水线配置(图1)
启动Shell脚本代码
#!/bin/sh
#print hello world in the console window
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
#echo "$flag"
kill $pid
while [[ $flag == 1 && $num -lt 10 ]]
do
#echo "$pid"
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`
# echo "$file";
if [ "$file" != "" ];then
rm $APP_NAME
mv leyoupin_temp.jar $APP_NAME
fi
# nohup java -jar $APP_NAME --spring.profiles.active=test2 > nohup.log 2>&1 &
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
流水线部署成功