ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Jenkins Pipeline 根据文件的数量动态生产多Stage - 并行运算测试案例

2021-11-18 03:31:07  阅读:213  来源: 互联网

标签:10 Pipeline full run java echo Jenkins dir 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达到越界。


标签:10,Pipeline,full,run,java,echo,Jenkins,dir,Stage
来源: https://www.cnblogs.com/move-on/p/15570401.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有