Jenkins parameterized matrix job

纵饮孤独 提交于 2020-01-15 06:07:23

问题


I am in the process of setting up a Jenkins job to run a bunch of tests on some c++ code. The code is generated during one Jenkins job. There are a number of sub-projects, with their code in their own folders.

My thought is to have a matrix job where each configuration runs the test on one folder of code files. There are two things that I am not sure the best way to do though...

  1. I would like to set up the matrix job to automatically pick up if more sub-folders are added. Something like passing a list of folders to the job as a parameter, and have that parameter used as the axis for the job.

  2. I would like the test to not be run on a specific folder unless some of the code in that folder was changed by the parent job.

Right now how to set up this test is completely open- I am trolling for ideas. If you have ever set up something like this- how did you do it?


回答1:


I had similar task - running a matrix job with variable number of folders as one axis. The folders were in version control but could easily be artifact. What I've done, is create two jobs, one main and normal, the other slave and matrix. Here is the code that needs to be run as elevated groovy in the main job:

import hudson.model.*
def currentBuild = Thread.currentThread().executable;
def jobName = 'SlaveMatrixJob' // Name of the matrix job to configure
def axisFolders = []
def strings =""

// Get the matrix job
def job = hudson.model.Hudson.instance.getItem(jobName)
assert job != null, "The job $jobName could not be found"

// Check it is a matrix job
assert job.getClass() == hudson.matrix.MatrixProject.class, "The job $jobName is of class '${job.getClass().name}', but expecting 'hudson.matrix.MatrixProject'"

// Get the folders
new File("C:\\Path\\Path").eachDirMatch ~/_test.*/, {it ->
   println "Got folder: ${it.name}"
   axisFolders << it.name
}

// Check if the array is empty
assert !axisFolders.isEmpty(), "No folders found to set in the matrix, aborting"

//Sort them
axisFolders.sort()

// Now set new axis list for test folders
def newAxisList = new hudson.matrix.AxisList()
newAxisList.add(new hudson.matrix.TextAxis('TEST_FOLDERS', axisFolders))
job.setAxes(newAxisList)
println "Matrix Job $jobName new axis list: ${job.getAxes().toString()}"

What this does basically is get all the folde in c:\path\path starting with _test and then inserting them in the SlaveMatrixJob parameter named TEST_FOLDERS.

I had to go with two jobs, since I was not able to make this dynamic update work without installing additional plugins, which was not possible at the time.

For the second point, you could add logic to the script to check if the folders have been updated since the last build and skip the ones that weren't. Or you could search for some plugins, but my advice is go with the script for simpler tasks.



来源:https://stackoverflow.com/questions/20409091/jenkins-parameterized-matrix-job

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!