Skip to content

Instantly share code, notes, and snippets.

@macg33zr
Created August 3, 2013 23:48
Show Gist options
  • Save macg33zr/6148433 to your computer and use it in GitHub Desktop.
Save macg33zr/6148433 to your computer and use it in GitHub Desktop.
Dynamically set configuration matrix on a Jenkins multi-configuration job to individual nodes with all online slaves. This would be a system groovy script in a separate job to configure the multi-configuration job. #JenkinsCI
// Name of the matrix job to configure
def jobName = 'MATRIX'
// 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 axis list
def axisList = job.getAxes()
println "------------------------------------------------------------------------"
println "Matrix Job $jobName current axis list: ${axisList.toString()}"
// Get the slaves
def axisNodes = []
hudson.model.Hudson.instance.slaves.each { slave ->
println "Got slave: ${slave.name}, online: ${slave.getComputer().isOnline()}"
if(slave.getComputer().isOnline()) {
axisNodes << slave.name
}
}
// Check got some online slaves
assert !axisNodes.isEmpty(), "No slaves online to set in the matrix, aborting"
// Now set new axis list for online slave labels
def newAxisList = new hudson.matrix.AxisList()
newAxisList.add(new hudson.matrix.LabelAxis('ONLINE_NODES', axisNodes))
job.setAxes(newAxisList)
println "Matrix Job $jobName new axis list: ${job.getAxes().toString()}"
println "------------------------------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment