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



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM