jenkins 在pipeline中运行docker镜像执行测试代码
jenkins教程:https://www.jenkins.io/zh/doc/book/pipeline/getting-started/
1.建立一个流水线任务
2.在配置中--流水线--定义pipeline流水线脚本,如下:
pipeline { agent any stages { stage("获取git master") { agent { label "master" } steps { git url: 'https://053ef307***@github.com/vt.git', branch: 'master' } } stage("执行用例") { agent { docker { label "master" image 'python3_pytest:lastest' args '' } } steps { sh 'pytest ./testcase/ --env dev1 --html ./report/api_dev.html --junit-xml ./report/api_dev.xml' } post { always { // txt测试报告 junit 'report/api_dev.xml' // html测试报告 publishHTML (target: [ allowMissing: false, alwaysLinkToLastBuild: false, keepAll: true, reportDir: './report', reportFiles: 'api_dev.html', reportName: "API DEV Report" ]) } } } } post { always { echo currentBuild.result } success { echo "success" // emailext body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: '$DEFAULT_RECIPIENTS' } failure { echo "failure" emailext body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: '$DEFAULT_RECIPIENTS' } } }
这是一个3个步骤的流水线,第一拉取GitHub代码,第二启动镜像(Jenkins节点上docker的镜像)执行shell,运行用例,第三发送报告
Jenkins教程:https://blog.csdn.net/kefengwang/article/details/54233584
邮件配置:
<!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> <td>(本邮件是Jenkins程序自动下发的,请勿回复!)</td> </tr> <tr> <td><h2><font color="#0000FF">构建结果 - ${BUILD_STATUS}</font></h2></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>构建编号:第${BUILD_NUMBER}次构建</li> <li>触发原因:${CAUSE}</li> <li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li> <li>构建地址:<a href="${BUILD_URL}">${BUILD_URL}</a></li> <li>工作目录:<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li> <li>项目地址:<a href="${PROJECT_URL}">${PROJECT_URL}</a></li> <li>测试报告:<a href="${BUILD_URL}testReport/">${BUILD_URL}testReport/</a></li> </ul> </td> </tr> <tr> <td><b><font color="#0B610B">测试结果:</font></b> <hr size="2" width="100%" align="center" /></td> </tr> <tr> <td> <ul> <li>Total:${TEST_COUNTS,var="total"}</li> <li>Pass:${TEST_COUNTS,var="pass"}</li> <li>Failed:${TEST_COUNTS,var="fail"}</li> <li>Skiped:${TEST_COUNTS,var="skip"}</li> </ul> <br /> </td> </tr> <tr> <td><b><font color="#0B610B">构建日志 (最后 100行):</font></b> <hr size="2" width="100%" align="center" /></td> </tr> <tr> <td><textarea cols="100" rows="30" readonly="readonly" style="font-family: Courier New;width: 888px">${BUILD_LOG, maxLines=100}</textarea> </td> </tr> </table> </body> </html>
鸡肉是我的最爱!