Jenkins Pipeline 根据文件的数量动态生产多Stage - 并行运算测试案例
2021/11/18 6:10:06
本文主要是介绍Jenkins Pipeline 根据文件的数量动态生产多Stage - 并行运算测试案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
碰到一个奇怪的问题, 我的Jenkins pipeline, 假如是一下这份的话就不会出现问题:
def testfiles def workspace = env.WORKSPACE pipeline { agent any stages { stage('Checkout Code') { steps { dir('sprintboot-demo') { //git url: 'git@github.com:mamtajha-ts/sprintboot-demo.git' git branch: 'feat1', url: 'git@github.com:mamtajha-ts/sprintboot-demo.git' } } } stage('Findout Cases to Run') { steps { script { testfiles = findFiles(glob: '**/*Tests.java') } } post { cleanup { echo 'completed the anylisis of test cases' } } } stage('Running Cases') { agent { docker { image 'maven:3.8.1-adoptopenjdk-11' args '-v $workspace:/.m2' reuseNode true } } steps { script { def tests = [:] def dir_offset_to_trum = 'sprintboot-demo/' def testcase_run_dir def full_dir for(int i=0; i < testfiles.size(); i++) { echo "this is the case ${i}" full_dir = "${testfiles[i].path}" echo "*******" echo "${full_dir}" echo "*********number${i}" testcase_run_dir = full_dir.replaceAll(/^${dir_offset_to_trum}/, "") echo "${testcase_run_dir}" name_file = "${testfiles[i].name}" tests["${i}"] = { stage("${name_file}"){ echo "Test case full directory ${full_dir}" echo "Test case relative directory to run: ${testcase_run_dir}" sh "ls /.m2" sh "mvn -v -Dspring-boot.run.arguments=\'${testcase_run_dir}\'" } } } parallel tests } } post { cleanup { echo 'done' } } } } }
要是以下操作的话就会有问题:
def testfiles def workspace = env.WORKSPACE pipeline { agent any stages { stage('Checkout Code') { steps { dir('sprintboot-demo') { //git url: 'git@github.com:mamtajha-ts/sprintboot-demo.git' git branch: 'feat1', url: 'git@github.com:mamtajha-ts/sprintboot-demo.git' } } } stage('Findout Cases to Run') { steps { script { testfiles = findFiles(glob: '**/*Tests.java') } } post { cleanup { echo 'completed the anylisis of test cases' } } } stage('Running Cases') { agent { docker { image 'maven:3.8.1-adoptopenjdk-11' args '-v $workspace:/.m2' reuseNode true } } steps { script { def tests = [:] def dir_offset_to_trum = 'sprintboot-demo/' def testcase_run_dir def full_dir for(int i=0; i < testfiles.size(); i++) { echo "this is the case ${i}" full_dir = "${testfiles[i].path}" echo "*******" echo "${full_dir}" echo "*********number${i}" testcase_run_dir = full_dir.replaceAll(/^${dir_offset_to_trum}/, "") echo "${testcase_run_dir}" tests["${i}"] = { stage("${testfiles[i].name}"){ echo "Test case full directory ${full_dir}" echo "Test case relative directory to run: ${testcase_run_dir}" sh "ls /.m2" sh "mvn -v -Dspring-boot.run.arguments=\'${testcase_run_dir}\'" } } } parallel tests } } post { cleanup { echo 'done' } } } } }
唯一的差别就是我会在stage的名字上使用预先设定的变量 (工作正常), 而另外一个没有这么做:
name_file = "${testfiles[i].name}"
stage("${testfiles[i].name}"){ echo "Test case full directory ${full_dir}" echo "Test case relative directory to run: ${testcase_run_dir}" sh "ls /.m2" sh "mvn -v -Dspring-boot.run.arguments=\'${testcase_run_dir}\'" }
而使用for 循环生产的stage的话会诡异的出现越界错误:
Also: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 Also: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 Also: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 Also: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 Also: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 Also: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 Also: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 Also: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 Also: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at org.codehaus.groovy.runtime.dgmimpl.arrays.ObjectArrayGetAtMetaMethod.invoke(ObjectArrayGetAtMetaMethod.java:41) 现在还不得其解。看起来像是Jenkins的一个bug,在使用paralle 跟for循环的时候还会再+1达到越界。
这篇关于Jenkins Pipeline 根据文件的数量动态生产多Stage - 并行运算测试案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23linux 系统宝塔查看网站访问的命令是什么?-icode9专业技术文章分享
- 2024-11-12如何创建可引导的 ESXi USB 安装介质 (macOS, Linux, Windows)
- 2024-11-08linux的 vi编辑器中搜索关键字有哪些常用的命令和技巧?-icode9专业技术文章分享
- 2024-11-08在 Linux 的 vi 或 vim 编辑器中什么命令可以直接跳到文件的结尾?-icode9专业技术文章分享
- 2024-10-22原生鸿蒙操作系统HarmonyOS NEXT(HarmonyOS 5)正式发布
- 2024-10-18操作系统入门教程:新手必看的基本操作指南
- 2024-10-18初学者必看:操作系统入门全攻略
- 2024-10-17操作系统入门教程:轻松掌握操作系统基础知识
- 2024-09-11Linux部署Scrapy学习:入门级指南
- 2024-09-11Linux部署Scrapy:入门级指南