Jenkins 流水线发布JAR包
1、流水线内容:文件以Jenkinsfile
命名
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'git@152.136.184.149:gitlab-instance-0d7d20f4/cicd.git']]])
}
}
stage('打JAR包') {
steps {
sh 'mvn clean package'
}
}
stage('部署前操作') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '120.48.34.146', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''source /etc/profile
bash /home/qinzt/jar/helloword/run.sh stop helloword-*.jar
mv /home/qinzt/jar/helloword/helloword-*.jar{,.$(date +%F-%S)}''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
stage('远程部署') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '120.48.34.146', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''source /etc/profile
cd /home/qinzt/jar/helloword && ./run.sh start helloword-*.jar''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'jar/helloword', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: '**/helloword-*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
post {
unsuccessful{
//如果构建未成功,则发送邮件通知
emailext attachLog: true, body: '${FILE,path="email.html"}', subject: '【构建通知】:${PROJECT_NAME}', to: '1790168505@qq.com'
}
}
}
2、邮箱通知模板内容:文件以email.html
命名
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
</head>
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
offset="0">
<table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
<tr>
本邮件由系统自动发出,无需回复!<br/>
各位同事,大家好,以下为${PROJECT_NAME }项目构建信息</br>
<td><font color="#CC0000">构建结果 - ${BUILD_STATUS}</font></td>
</tr>
<tr>
<td><br />
<b><font color="#0B610B">构建信息</font></b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<tr>
<td>
<ul>
<li>项目名称 : ${PROJECT_NAME}</li>
<li>项目描述 : ${JOB_DESCRIPTION}</li>
<li>构建编号 : 第${BUILD_NUMBER}次构建</li>
<li>触发原因: ${CAUSE}</li>
<li>构建状态: ${BUILD_STATUS}</li>
<li>构建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
<li>构建 Url : <a href="${BUILD_URL}">${BUILD_URL}</a></li>
<li>工作目录 : <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
<li>项目 Url : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
</ul>
</td>
</tr>
<tr>
<td><b><font color="#0B610B">变更集</font></b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<td>${JELLY_SCRIPT,template="html"}<br/>
<hr size="2" width="100%" align="center" /></td>
</tr>
</table>
</body>
</html>
3、将Jenkinsfile
和email.html
推送到git库根目录中
git add .
git commit -m "add Jenkinsfile email.html"
git push -u origin main
4、run.sh 脚本内容:文件以run.sh
命名
#!/bin/bash
cd `dirname $0`
APP_NAME=$2
#使用说明,用来提示输入参数
usage() {
echo "Usage: ./run.sh [start|stop|restart|status]"
exit 1
}
#检查程序是否在运行
is_exist(){
pid=`jps -l|grep $APP_NAME|grep -v grep|awk '{print $1}'`
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} is already running. pid=${pid}"
else
nohup java -Xmx100m -Xms100m -jar $APP_NAME >/dev/null 2>&1 &
echo "START..."
sleep 5
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} is running success. pid=${pid}"
fi
fi
}
#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -15 $pid
echo "..."
sleep 2
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} still in the running. pid=${pid}"
else
echo "${APP_NAME} has stopped running."
fi
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启
restart(){
stop
#sleep 5
start
}
#程序升级
upgrade(){
./run.sh stop
cd ..
mv $APP_NAME $backup
cp $rjxf ./
./bin/run.sh start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
"upgrade")
upgrade
;;
*)
usage
;;
esac
5、配置流水线