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达到越界。


上一篇:HTML+CSS大作业HTML5期末大作业 旅游酒店网站设计——旅游酒店服务预订(1页) web网页设计—— 出游


下一篇:【CVX】Bounding consumer preference