环境准备
centos7
k8s集群
jenkins + gitlab
下载jenkins相关插件
创建流水线
流水线配置
脚本内容
声明式,构建slave节点的流水线。
// 为了解决jmeter生成的HTML报告无法显示的问题。参考https://blog.csdn.net/qq_29427355/article/details/82424467 System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "") pipeline agent kubernetes //yamlFile 'KubernetesPod.yaml' cloud 'kubernetes' yaml '''---apiVersion v1 kind Pod metadata namespace devops spec nodeSelector kubernetes.io/os linux restartPolicy Never serviceAccountName jenkins containersname"docker" image"docker:latest" imagePullPolicy"IfNotPresent" resources limits requests command cat ttytrue volumeMountsmountPath /var/run/docker.sock name docker-sock readOnlyfalsename"python3" image"python:3.8.6" imagePullPolicy"IfNotPresent" resources limits requests command cat ttytrue volumeMountsmountPath /var/run/docker.sock name docker-sock readOnlyfalsename"maven36" image"yfy/maven:3.6-jdk-11" imagePullPolicy"IfNotPresent" resources limits requests command cat ttytrue volumeMountsmountPath /root/.m2/repository name maven-repo readOnlyfalsename"jmeter55" image"justb4/jmeter:latest" imagePullPolicy"IfNotPresent" resources limits requests command cat ttytrue volumeMountsmountPath /plugin name jmeter-plugins readOnlyfalse volumeshostPath path /var/run/docker.sock name docker-sock hostPath path /data/devops/jenkins/.m/repository name maven-repo hostPath path /data/devops/jenkins/tools/jmeter/plugins name jmeter-plugins ''' options //设置管道运行的超时时间,在此之后,Jenkins将中止管道 timeout(time20 unit'MINUTES') // 失败重试次数 retry(2) //输出时间戳 timestamps() environment //构建分支 master、test // Brand='test' //定义Maven编译环境 dev/test/prod ACTIVE = "test" //jar包名称 JAR_NAME = 'yss-datamiddle-multiplecheck.jar' //JOB所在根目录,JOB_NAME WORKSPACE_PATH = "/opt/dataMiddle-service/workspace/${JOB_NAME}/" stages stage('checkout') steps checkout([$class'GitSCM' branches name'*/main' extensions userRemoteConfigs credentialsId'gitlab-author' url'http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git' ) stage('jmeter55') steps container('jmeter55') // jmeter -n -t test.jmx -l test.jtl -e -o /path // # -n: 以非GUI形式运行Jmeter // # -t:source.jmx 脚本路径 // # -l result.jtl 运行结果保存路径(.jtl),此文件必须不存在 // # -e:在脚本运行结束后生成html报告 // # -o:用于存放html报告的目录 sh "jmeter --version" sh "jmeter -n -Jthreadcount=5 -t jmeter-test.jmx -l reports/jmeter-test-report.jtl -e -o reportshtml/" stage("性能测试报告") steps // echo '---Performance Reports---' // perfReport filterRegex'' showTrendGraphs true sourceDataFiles'reports/*.jtl' echo '--- HTML Report ---' //展示测试报告 publishHTML([allowMissing false alwaysLinkToLastBuild false keepAll false reportDir'reportshtml' reportFiles'index.html' reportName'jmeterHTMLReport' reportTitles'' useWrapperFileDirectly true ) // stage('maven3.6') // steps // container('maven36') // sh "mvn -v" // sh "mvn clean package -DskipTests=true" // // // // stage('build-allure-results') // steps // container('python3') // sh """ // python --version // pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple // python main.py $ allure_results_path // """ // // // // stage('build-allure-export') // steps // // 通过jenkins的全局工具配置,下载allure工具 // allure includeProperties false jdk'' results path'${allure_results_path}' // // // stage('docker build') // steps // container('docker') // sh """ // docker version // """ // // container('python3') // print('user mutil container...') // // //
使用kubernets作为slave节点配置
kubernetes: slave节点
yaml: 声明式定义pod template
pipeline agent kubernetes //yamlFile 'KubernetesPod.yaml' cloud 'kubernetes' yaml '''
在pod-template中使用jmeter容器
name"jmeter55" image"justb4/jmeter:latest" imagePullPolicy"IfNotPresent" resources limits requests command cat ttytrue volumeMountsmountPath /plugin name jmeter-plugins readOnlyfalse
- mountPath: /plugin : 容器内持久化挂载的jmeter插件
volumeshostPath path /var/run/docker.sock name docker-sock hostPath path /data/devops/jenkins/.m/repository name maven-repo hostPath path /data/devops/jenkins/tools/jmeter/plugins name jmeter-plugins
- hostPath:
path: /data/devops/jenkins/tools/jmeter/plugins
name: jmeter-plugins
jmeter插件在宿主机上的位置
注意:提前把jmeter使用的相关插件拷贝到 此目录下:
/data/devops/jenkins/tools/jmeter/plugins
如图所示:
checkout仓库
stage('checkout') steps checkout([$class'GitSCM' branches name'*/main' extensions userRemoteConfigs credentialsId'gitlab-author' url'http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git' )
使用jmeter容器生成HTML报表
stage('jmeter55') steps container('jmeter55') // jmeter -n -t test.jmx -l test.jtl -e -o /path // # -n: 以非GUI形式运行Jmeter // # -t:source.jmx 脚本路径 // # -l result.jtl 运行结果保存路径(.jtl),此文件必须不存在 // # -e:在脚本运行结束后生成html报告 // # -o:用于存放html报告的目录 sh "jmeter --version" sh "jmeter -n -Jthreadcount=5 -t jmeter-test.jmx -l reports/jmeter-test-report.jtl -e -o reportshtml/"
stage("性能测试报告") steps // echo '---Performance Reports---' // perfReport filterRegex'' showTrendGraphs true sourceDataFiles'reports/*.jtl' echo '--- HTML Report ---' //展示测试报告 publishHTML([allowMissing false alwaysLinkToLastBuild false keepAll false reportDir'reportshtml' reportFiles'index.html' reportName'jmeterHTMLReport' reportTitles'' useWrapperFileDirectly true )
执行流水线
Started by user jenkins Pipeline Start of Pipeline Pipeline podTemplate Pipeline Pipeline node Created Pod kubernetes devops/jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0 Still waiting to schedule task ‘jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0’ is offline Agent jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0 is provisioned from template jmeter-pytest-pipeline_16-nzxgn-n639w ---apiVersion"v1"kind"Pod"metadata annotations buildUrl"http://192.168.10.201:30080/job/jmeter-pytest-pipeline/16/" runUrl"job/jmeter-pytest-pipeline/16/" labels jenkins"slave" jenkins/label-digest"ae6ed240d3a142da64181870bc68b00255b964cc" jenkins/label"jmeter-pytest-pipeline_16-nzxgn" name"jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0" namespace"devops"spec containerscommand"cat" image"docker:latest" imagePullPolicy"IfNotPresent" name"docker" resources limits requests ttytrue volumeMountsmountPath"/var/run/docker.sock" name"docker-sock" readOnlyfalsemountPath"/home/jenkins/agent" name"workspace-volume" readOnlyfalsecommand"cat" image"python:3.8.6" imagePullPolicy"IfNotPresent" name"python3" resources limits requests ttytrue volumeMountsmountPath"/var/run/docker.sock" name"docker-sock" readOnlyfalsemountPath"/home/jenkins/agent" name"workspace-volume" readOnlyfalsecommand"cat" image"yfy/maven:3.6-jdk-11" imagePullPolicy"IfNotPresent" name"maven36" resources limits requests ttytrue volumeMountsmountPath"/root/.m2/repository" name"maven-repo" readOnlyfalsemountPath"/home/jenkins/agent" name"workspace-volume" readOnlyfalsecommand"cat" image"justb4/jmeter:latest" imagePullPolicy"IfNotPresent" name"jmeter55" resources limits requests ttytrue volumeMountsmountPath"/plugin" name"jmeter-plugins" readOnlyfalsemountPath"/home/jenkins/agent" name"workspace-volume" readOnlyfalseenvname"JENKINS_SECRET" value"********"name"JENKINS_AGENT_NAME" value"jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0"name"JENKINS_NAME" value"jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0"name"JENKINS_AGENT_WORKDIR" value"/home/jenkins/agent"name"JENKINS_URL" value"http://192.168.10.201:30080/" image"jenkins/inbound-agent:4.11-1-jdk11" name"jnlp" resources limits requests memory"256Mi" cpu"100m" volumeMountsmountPath"/home/jenkins/agent" name"workspace-volume" readOnlyfalse nodeSelector kubernetes.io/os"linux" restartPolicy"Never" serviceAccountName"jenkins" volumeshostPath path"/var/run/docker.sock" name"docker-sock"hostPath path"/data/devops/jenkins/.m/repository" name"maven-repo"hostPath path"/data/devops/jenkins/tools/jmeter/plugins" name"jmeter-plugins"emptyDir medium"" name"workspace-volume"Running on jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0 in /home/jenkins/agent/workspace/jmeter-pytest-pipeline Pipeline Pipeline withEnv Pipeline Pipeline timeout Timeout set to expire in 20 min Pipeline Pipeline retry Pipeline Pipeline timestamps Pipeline Pipeline stage Pipeline (checkout) Pipeline checkout 08:32:00 The recommended git tool is NONE 08:32:06 using credential gitlab-author 08:32:06 Cloning the remote Git repository 08:32:07 Cloning repository http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git 08:32:07 > git init /home/jenkins/agent/workspace/jmeter-pytest-pipeline # timeout=1008:32:08 Fetching upstream changes from http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git 08:32:08 > git --version # timeout=1008:32:08 > git --version # 'git version 2.30.2'08:32:08 using GIT_ASKPASS to set credentials gitlab-author 08:32:08 > git fetch --tags --force --progress -- http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git +refs/heads/*:refs/remotes/origin/* # timeout=1008:32:09 > git config remote.origin.url http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git # timeout=1008:32:09 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=1008:32:10 Avoid second fetch 08:32:10 Checking out Revision 6deb2f3a38f079cd55fbd1d9189872c75d1ea676 (refs/remotes/origin/main) 08:32:10 > git rev-parse refs/remotes/origin/main^# timeout=10 commit08:32:10 > git config core.sparsecheckout # timeout=1008:32:10 > git checkout -f 6deb2f3a38f079cd55fbd1d9189872c75d1ea676 # timeout=1008:32:13 Commit message"Update Jenkinsfile"08:32:13 > git rev-list --no-walk 40205a28a67e6a6b48cee7b8f2c98e6bea88ff8c # timeout=10 Pipeline Pipeline // stage Pipeline stage Pipeline (jmeter55) Pipeline container Pipeline Pipeline sh 08:32:14 + jmeter --version 08:32:19 Dec 18 2022 1:32:18 AM java.util.prefs.FileSystemPreferences$1 run 08:32:19 INFO Created user preferences directory. 08:32:19 _ ____ _ ____ _ _ _____ _ __ __ _____ _____ _____ ____ 08:32:19 / \ | _ \ / \ / ___| | | | ____| | | \/ | ____|_ _| ____| _ \ 08:32:19 / _ \ | |_) / _ \| | | |_| | _| _ | | |\/| | _| | | | _| | |_) | 08:32:19 / ___ \| __/ ___ \ |___| _ | |___ | |_| | | | | |___ | | | |___| _ < 08:32:19 /_/ \_\_| /_/ \_\____|_| |_|_____| \___/|_| |_|_____| |_| |_____|_| \_\ 5.5 08:32:19 08:32:19 Copyright (c) 1999-2022 The Apache Software Foundation 08:32:19 Pipeline sh 08:32:20 + jmeter -n '-Jthreadcount=5' -t jmeter-test.jmx -l reports/jmeter-test-report.jtl -e -o reportshtml/ 08:32:23 Creating summariser <summary> 08:32:23 Created the tree successfully using jmeter-test.jmx 08:32:23 Starting standalone test @ December 18 2022 1:32:22 AM CET (1671323542996) 08:32:23 Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445 08:32:25 summary = 2 in 00:00:01 = 1.4/s Avg 560 Min 81 Max 1039 Err 0 (0.00%) 08:32:25 Tidying up ... @ December 18 2022 1:32:24 AM CET (1671323544991) 08:32:26 ... end of run Pipeline Pipeline // container Pipeline Pipeline // stage Pipeline stage Pipeline (性能测试报告) Pipeline echo 08:32:27 --- HTML Report --- Pipeline publishHTML 08:32:27 htmlpublisher Archiving HTML reports... 08:32:27 htmlpublisher Archiving at PROJECT level /home/jenkins/agent/workspace/jmeter-pytest-pipeline/reportshtml to /var/lib/jenkins/jobs/jmeter-pytest-pipeline/htmlreports/jmeterHTMLReport Pipeline Pipeline // stage Pipeline Pipeline // timestamps Pipeline Pipeline // retry Pipeline Pipeline // timeout Pipeline Pipeline // withEnv Pipeline Pipeline // node Pipeline Pipeline // podTemplate Pipeline End of Pipeline Finished SUCCESS
查看HTML测试报告
reportng html报告在jenkins中显示有问题,或者 css样式不显示
如下不显示内容
解决jmeter测试报告HTML页面无法正常显示
在pipeline代码中添加以下代码:
// 为了解决jmeter生成的HTML报告无法显示的问题。参考https://blog.csdn.net/qq_29427355/article/details/82424467 System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
再次执行流水线,流水线会失败,并输出一下内容:
点击提示中的超链接,授权并批准administrators权限
或者
在系统管理 》进行授权
授权成功以后,重新构建流水线,再次查看HTML报表
参考相关代码:
https://gitee.com/yyyfly/devops-k8s-pytest-jmeter