Created
July 11, 2013 01:47
-
-
Save macg33zr/5971860 to your computer and use it in GitHub Desktop.
Jenkins System Groovy script to correlate upstream jobs on named parameter with the same value. This is to correlate jobs that are triggered externally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// System groovy script to correlate upstream jobs on a parameter value matching a parameter value on this job | |
// | |
// - Have 1 - N upstream jobs which are called externally with a parameter "FOO" | |
// - The upstream jobs each call this job with the same parameter "FOO" | |
// - In this job, I want to continue some process when "FOO" has the same value for any builds of the upstream jobs | |
// - Hence correlating on the FOO parameter value | |
// - I don't want to look back too many builds on the upstream jobs | |
// How many builds to look back on. Set to zero for all builds. | |
def maxBuilds = 0 | |
// Parameter to correlate on. This is a param on this project that is going to match the upstream projects same param and value | |
def correlateParamName = 'FOO' | |
def correlateParamValue = build.buildVariableResolver.resolve(correlateParamName) | |
println "--------------------------------------------------------------------------------------------------------" | |
println "Correlate on this Job param '$correlateParamName' = '$correlateParamValue'" | |
println "--------------------------------------------------------------------------------------------------------" | |
// Getting the upstream projects | |
def upStreamProjects = build.getParent().getUpstreamProjects() | |
println "Found ${upStreamProjects.size()} upstream projects" | |
// Enumerate upstream projects and correlate params | |
def correlated = 0 | |
upStreamProjects.each { proj -> | |
println "--------------------------------------------------------------------------------------------------------" | |
println "upstream project: ${proj.getName()}, enumerating builds..." | |
for (build in proj.builds) { | |
// Terminate if maxBuilds set and exceeded | |
if( (maxBuilds > 0) && ( (proj.builds.first().number - build.number) >= maxBuilds) ) { | |
println "Build ${build.number} of ${proj.builds.first().number} exceeds max of ${maxBuilds}, terminating search" | |
break | |
} | |
def buildActions = build.getActions(hudson.model.ParametersAction) | |
if (buildActions.size() > 0) { | |
def paramAction = buildActions.get(0) | |
def paramValue = paramAction.createVariableResolver(build).resolve(correlateParamName) | |
println "Build: ${build.toString()} (${build.number} of ${proj.builds.first().number}) has param '${correlateParamName}' = '${paramValue}'" | |
if(paramValue == correlateParamValue) { | |
println "Matched --> param is correlated for upstream job" | |
++correlated | |
break; | |
} | |
} | |
else { | |
println "Build: ${build.toString()} (${build.number} of ${proj.builds.first().number}) has no param '${correlateParamName}' to correlate" | |
} | |
} | |
} | |
println "Correlated $correlated of ${upStreamProjects.size()} upstream jobs" | |
println "--------------------------------------------------------------------------------------------------------" | |
assert correlated == upStreamProjects.size(), 'Correlation failed' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment