综合使用参数化,shell命令,发送邮件,发布report报告,执行ant命令,java jar
pipeline{
agent {
label 'master'
}
parameters {
string defaultValue: '', description: 'case路径', name: 'casePath', trim: false
string defaultValue: '', description: '', name: 'functionPath', trim: false
string defaultValue: '', description: '', name: 'PictureUploadPath', trim: false
string defaultValue: '/', description: '', name: 'requestTempPath', trim: false
string defaultValue: '', description: '', name: 'productPath', trim: false
string defaultValue: '', description: '', name: 'resultPath', trim: false
string defaultValue: '', description: '', name: 'imagesPath', trim: false
string defaultValue: '', description: '', name: 'requestDesPath', trim: false
string defaultValue: '', description: '', name: 'policyInfoXmlPath', trim: false
string defaultValue: '', description: '', name: 'loginUserName', trim: false
string defaultValue: '', description: '', name: 'loginUserPwd', trim: false
string defaultValue: 'just by remark', : 'remark', name: 'remark', trim: false
}
environment {
testEnvirontment = "环境"
}
tools {
jdk 'JAVA_HOME'
}
triggers {
cron 'H H(9-14)/4 * * 1-5'
}
stages {
stage('pull source') {
steps {
git credentialsId: '凭证', url: 'git路径'
}
}
stage('execut shell') {
steps {
sh ''' cp -r $WORKSPACE/apache-jmeter-5.1.1/bin/jmeter/apache-jmeter-5.3/bin/
echo "$casePath,$resultPath,$requestTempPath,$requestDesPath,$imagesPath,$functionPath,$productPath,$loginUserName,$loginUserPwd,$remark" > /usr/local/appach/jmeter/apache-jmeter-5.3/bin/Config/NB_doubleCheck.txt
echo $testEnvirontment >/usr/local/appach/jmeter/apache-jmeter-5.3/bin/Config/environtment.txt
chown -R jkusr.jkusr $WORKSPACE/apache-jmeter-5.1.1/bin/
echo $Dateparameter
'''
}
}
stage('execut ant'){
steps{
withAnt(installation: 'ANT_HOME', jdk: 'JAVA_HOME') {
sh 'ant -f /usr/local/appach/ant/apache-ant-1.9.15/bin/mysql_uat/Counter/NB/restult/build.xml -Dtime=$Dateparameter'
}
}
}
stage('execut after'){
steps{
sh '''
cp /home/jkusr/mkdir.sh $WORKSPACE/
cd $WORKSPACE/
./mkdir.sh html html1
cp /home/report/html/TestReport$Dateparameter.html $WORKSPACE/html/
cp /home/report/html1/TestReport$Dateparameter.html $WORKSPACE/html1/
chown -R usr.usr /home/report/
'''
}
}
stage('result'){
steps{
sh '''
cd /home/usr/report/html1
grep -o \'<td align="center">[0-9]*</td><td align="center" style="color:red;font-weight:bolder;">[0-9]*</td><td align="center" style="color:green;font-weight:bolder;">[0-9]*\' TestReport$Dateparameter.html | grep -c 100
if [ $?=0 ]
then
exit 0
else
exit 1
fi
'''
}
}
}
post{
success {
emailext attachmentsPattern: 'html/TestReport$Dateparameter.html', body: '$DEFAULT_CONTENT', recipientProviders: [developers()], replyTo: '$DEFAULT_REPLYTO', subject: 'MySqlUat-Counter-测试报告', to: '$DEFAULT_REPLYTO'
}
failure {
emailext attachmentsPattern: 'html/TestReport$Dateparameter.html', body: '$DEFAULT_CONTENT', recipientProviders: [developers()], replyTo: '$DEFAULT_REPLYTO', subject: 'MySqlUat-Counter-测试报告', to: '$DEFAULT_REPLYTO'
}
always{
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: './html1/', reportFiles: 'TestReport$Dateparameter.html', reportName: 'HTML Report', reportTitles: '汇总测试报告'])
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: './html/', reportFiles: 'TestReport$Dateparameter.html', reportName: 'DetailL Report', reportTitles: '详细测试报告'])
sh '''
cd $WORKSPACE/apache-jmeter-5.1.1/bin/Function/
java -jar WechatSendMessage.jar group "msAutomationTest" content::"$testEnvirontment环境-测试结果如下": "/home/report/html1/TestReport$Dateparameter.html"::"/home/report/picture/TestReport$Dateparameter.png"::file file::"/home/report/html/TestReport$Dateparameter.html"
'''
}
}
}
或者使用:
pipeline{
agent {
label 'master'
}
parameters {
string defaultValue: '', description: 'testEnvirontment', name: 'testEnvirontment', trim: false
}
environment {
loginUserName = ""
loginUserPwd = ""
testType = ""
policyInfoXmlPath = ""
requestDesPath = ""
imagesPath = ""
resultPath = "/"
productPath = "$WORKSPACE/apache-jmeter-5.1.1/bin//Config/"
requestTempPath = ""
PictureUploadPath = ""
functionPath = ""
casePath = ""
jmeterhome = ""
anthome = ""
jobName = "$JOB_NAME"
}
tools {
jdk 'JAVA_HOME'
}
triggers {
cron '0 8,13,18 * * *'
}
stages {
stage('pull source') {
steps {
git credentialsId: '', url: 'git地址'
sh ' chown -R jkusr.jkusr $WORKSPACE/ '
}
}
stage('execut shell') {
steps {
sh '''
echo "$casePath,$functionPath,$PictureUploadPath,$requestTempPath,$productPath,$resultPath,$imagesPath,$requestDesPath,$policyInfoXmlPath,$loginUserName,$loginUserPwd,$testType" > $jmeterhome/MslifeAutomation/Config/appUwTestInfo.txt
echo "$testEnvirontment" > $jmeterhome/Config/environtment.txt
'''
}
}
stage('execut ant'){
steps{
withAnt(installation: 'ANT_HOME', jdk: 'JAVA_HOME') {
sh '''
ant -f $anthome/mysql/APP/build.xml -Dtime=$Dateparameter -DjobName=$jobName
'''
}
}
}
stage('pull html'){
steps{
sh '''
cp /home/mkdir.sh $WORKSPACE/
cd $WORKSPACE/
./mkdir.sh html html1
cp /$WORKSPACE/apache-jmeter-5.1.1/bin/Result/html/${jobName}_明细报告_$Dateparameter.html $WORKSPACE/html/
cp /$WORKSPACE/apache-jmeter-5.1.1/bin/Result/html1/${jobName}_汇总报告_$Dateparameter.html $WORKSPACE/html1/
chown -R jkusr.jkusr /home/report/
chown -R jkusr.jkusr $WORKSPACE/
'''
}
}
stage('publish report'){
steps{
script{
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: './html1/',
reportFiles: '${JOB_NAME}_汇总报告_$Dateparameter.html',
reportName: "汇总测试报告"
])
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: './html/',
reportFiles: '${JOB_NAME}_明细报告_$Dateparameter.html',
reportName: "明细测试报告"
])
}
}
}
stage('result'){
steps{
sh '''
cd $WORKSPACE/html1
grep -o '<td align="center">[0-9]*</td><td align="center" style="color:red;font-weight:bolder;">[0-9]*</td><td align="center" style="color:green;font-weight:bolder;">[0-9]*' ${jobName}_汇总报告_$Dateparameter.html | grep -c 100
if [ $?=0 ]
then
exit 0
else
exit 1
fi
'''
}
}
}
post{
success {
emailext(
body: '$DEFAULT_CONTENT',
subject: '$JOB_NAME',
attachmentsPattern: 'html/${JOB_NAME}_明细报告_$Dateparameter.html',
from: '',
to: '邮件地址'
)
}
failure {
emailext(
body: '$DEFAULT_CONTENT',
subject: '$JOB_NAME',
attachmentsPattern: 'html/${JOB_NAME}_明细报告_$Dateparameter.html',
from: '',
to: '$DEFAULT_RECIPIENTS'
)
sh '''
cd $jmeterhome/Function/
java -jar heello.jar
'''
}
always{
sh '''
cd $jmeterhome/Function/
java -jar heello.jar
'''
}
}
}
ant命令里面 将参数Dateparameter 传递到build.xml,build.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-jmeter-test" default="run" basedir="." >
<property name="time" value="${time}"/>
<property name="jmeter.home" value="/usr/local/appach/jmeter/apache-jmeter-5.3"/>
<!-- jmeter生成jtl格式的结果报告路径 -->
<property name="jmeter.result.jtl.dir" value="/home/report/jtl" />
<!-- jmeter生成html格式的结果报告路径 -->
<property name="jmeter.result.html.dir" value="/home/report/html" />
<!-- jmeter生成html格式的结果报告路径 -->
<property name="jmeter.result.html.dir2" value="/home/report/html1" />
<!-- jmeter生成报告前缀 -->
<property name="ReportName" value="TestReport" />
<property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}${time}.jtl" />
<property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}${time}.html" />
<property name="jmeter.result.htmlName2" value="${jmeter.result.html.dir2}/${ReportName}${time}.html" />
<target name="run">
<antcall target="test"/>
<antcall target="report"/>
</target>
<!--测试脚本-->
<target name="test">
<taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
<jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
<!-- 声明要运行的脚本“*.jmx”指包含此目录下的所有jmeter脚本-->
<testplans dir='/home/jenkins/jobs/test/workspace/apache-jmeter-5.1.1/bin/MslifeAutomation/Script/Count/NB' includes="*.jmx" />
<property name="jmeter.save.saveservice.output_format" value="xml" />
</jmeter>
</target>
<target name="report">
<xslt
in="${jmeter.result.jtlName}"
out="${jmeter.result.htmlName2}"
style="${jmeter.home}/extras/summary_20201110.xsl">
</xslt>
<xslt
in="${jmeter.result.jtlName}"
out="${jmeter.result.htmlName}"
style="${jmeter.home}/extras/detail_20201110.xsl">
</xslt>
<!-- 拷贝报告所需的图片资源至目标目录 -->
<copy todir="${jmeter.result.html.dir}">
<fileset dir="${jmeter.home}/extras">
<include name="collapse.png" />
<include name="expand.png" />
</fileset>
</copy>
<!-- 拷贝报告所需的图片资源至目标目录 -->
<copy todir="${jmeter.result.html.dir2}">
<fileset dir="${jmeter.home}/extras">
<include name="collapse.png" />
<include name="expand.png" />
</fileset>
</copy>
</target>
</project>