jenkins学习笔记第十一篇pipeline发送邮件

综合使用参数化,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>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员路同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值