碰到一個奇怪的問題, 我的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達到越界。