首先配置从节点
image.png
image.png
创建流水线项目,进入配置
image.png
配置流水线
image.png
远端部署文件,仅供参考,不同场景使用方式不同
#!/bin/bash
#export BUILD_ID=dontKillMe #不使用pipline加
#TOMCAT_PID=`ps aux | grep 'tomcat' | grep -v grep | awk '{print $2}'`
TOMCATTHREAD=`ps aux | grep 'tomcat' | grep -v grep`
if test -z "$TOMCATTHREAD"
then
echo "TOMCAT NOT START"
else
THREADLIST=(${TOMCATTHREAD// / })
PID=${THREADLIST[1]}
kill -9 $PID
sleep 6s
echo "The tomcat process has been killed"
fi
#将项目拷贝到tomcat目录下
cp -r /root/workspace/ContinuousIntegration/Shopping /home/wgz/usr/apache-tomcat-7/webapps
#登陆数据库删除库导入库, 针对我当前环境,实际情况不要这么做
mysql="mysql --defaults-extra-file=/root/.my.cnf"
sq1="drop database if exists db_shopping"
sq2="source /root/workspace/ContinuousIntegration/db_shopping.sql"
$mysql -e "$sq1"
$mysql -e "$sq2"
#禁止jenkins杀死tomcat进程
JENKINS_NODE_COOKIE=dontKillMe nohup sh /home/wgz/usr/apache-tomcat-7/bin/startup.sh &
sleep 3
echo "Project deployment completed"
远端部署,本地执行自动化测试及邮件发送,更新代码至git后,会自动进行构建
#!/usr/bin/env groovy
pipeline{
// agent{label 'Centos'}
agent{label 'master'}
environment {
sendmail = 'yes'
gitpullerr = 'noerr'
}
#轮询检测是否有代码更新到git
triggers { pollSCM('* * * * *') }
stages {
stage('构建') {
agent{label 'Centos'}
steps {
checkout scm
//checkout([$class: 'GitSCM', branches: [[name: ${git_branch}]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: ${git_key}, url: ${git_url}]]])
}
}
stage('部署'){
agent{label 'Centos'}
steps {
sh 'cd /root/workspace/ContinuousIntegration/ && sh ./deploy.sh'
}
}
stage('测试'){
steps{
sh 'rm -rf /Users/wgz/.jenkins/workspace/ContinuousIntegration/web'
sh 'git clone https://github.com/weiguozhen/web.git'
sh 'cd web && sh ./run.sh'
}
}
}
post{
success {
script {
if (sendmail == 'yes') {
sh 'pwd'
emailext attachmentsPattern: 'web/*.html', body: '''<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>
<td><br />
<b><font color="#0B610B">构建信息</font></b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<tr>
<td>
<ul>
<div style="font-size:18px">
<li>构建名称:${PROJECT_NAME}</li>
<li>构建结果: <span style="color:green"> Successful </span></li>
<li>构建编号:${BUILD_NUMBER}</li>
<li>触发原因:${CAUSE}</li>
<li>GIT 地址:xxxx</li>
<li>GIT 分支:master</li>
<li>变更记录: ${CHANGES,showPaths=true,showDependencies=true,format="<pre><ul><li>提交ID: %r</li><li>提交人:%a</li><li>提交时间:%d</li><li>提交信息:%m</li><li>提交文件:<br />%p</li></ul></pre>",pathFormat=" %p <br />"}
<li><font color="#0B610B">测试结果报告见附件📎</font></li>
</div>
</ul>
</td>
</tr>
</table>
</body>
</html>
''', subject: '${PROJECT_NAME}', to: '1272235678@qq.com,1527183103@qq.com'
}
}
}
failure {
script {
if (gitpullerr == 'noerr') {
emailext attachmentsPattern: 'web/*.html', body: '''<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</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>
<td><br />
<b><font color="#0B610B">构建信息</font></b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<tr>
<td>
<ul>
<li>构建名称:${JOB_NAME}</li>
<li>构建结果: <span style="color:red"> ${BUILD_STATUS}</span></li>
<li>构建编号:${BUILD_NUMBER} </li>
<li>GIT 地址:xxxx</li>
<li>GIT 分支:master</li>
<li>变更记录: ${CHANGES,showPaths=true,showDependencies=true,format="<pre><ul><li>提交ID: %r</li><li>提交人:%a</li><li>提交时间:%d</li><li>提交信息:%m</li><li>提交文件:%p</li></ul></pre>",pathFormat="%p <br />"}
</ul>
</td>
</tr>
<tr>
<td><b><font color="#0B610B">构建日志 :</font></b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<tr>
<td><textarea cols="150" rows="30" readonly="readonly"
style="font-family: Courier New">${BUILD_LOG}</textarea>
</td>
</tr>
</table>
</body>
</html>
''', subject: '${PROJECT_NAME}', to: '1272235678@qq.com'
}else {
echo 'scm pull error ignore send mail'
}
}
}
}
}
这里踩了一些坑,记录一下,在本地jenkins启动远端linux tomcat时,控制台显示启动成功,但是tomcat并未启动成功,本以为是jenkins启动tomcat机制杀死了tomcat,但是观察日志发现,是启动tomcat的关键路径有问题,需要在远端tomcat catalina.sh中作如下配置,根据你自己的存放路径来写。
JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:MaxNewSize=1024m"
export TOMCAT_HOME=/home/wgz/usr/apache-tomcat-7
export CATALINA_HOME=/home/wgz/usr/apache-tomcat-7
export JAVA_HOME=/opt/jdk1.8.0_171
如果过程中提示有命令找不到,比如本地robot命令在本地终端可执行,但在jenkins中无法执行,在sh中生效本地环境变量,可解决找不到运行环境的问题。
未完待续...
网友评论