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 - 并行运算测试案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程