I have a project that builds based on an SVN commit. When that project succeeds it triggers a project to build on a slave.
How do I get the SVN revision of the original/trigger project in the slave?
You can get the upstream's revision information (and more) via Groovy. The following script in a "Execute system Groovy script" build step will do what you want:
import jenkins.model.*
// find latest upstream build that triggerd this run (there may be several iff queued)
def latestCause = build.causes.findAll {
it.class.toString() == 'class hudson.model.Cause$UpstreamCause'
}.max( { it.getUpstreamBuild() } )
// determine upstream project name and build number
def upstreamProjectName = latestCause.upstreamProject
def upstreamBuildNum = latestCause.upstreamBuild
println "upstreamProject = ${upstreamProjectName}"
println "upstreamBuildNum = ${upstreamBuildNum}"
// find corresponding upstream job and build objects
def upstreamJob = Jenkins.instance.getItemByFullName( upstreamProjectName )
def upstreamBuild = upstreamJob.getBuildByNumber( upstreamBuildNum )
// print subversion revision info
def state = upstreamBuild.getAction( hudson.scm.SCMRevisionState.class )
state.revisions.each { location, revision ->
println "upstream url/rev: ${location} / ${revision}"
}
Sample output:
upstreamProject = svn-upstream-project
upstreamBuildNum = 16
upstream url/rev: https://svn.example.com/foo/bar / 12345
You will not need to pass any "upstream" information explicitly to the downstream project. This is easier to maintain, and also the downstream queueing behavior won't change (see my comment on the other answer). This is essential if your downstream execution takes more time than what's available between to upstream executions -- you'll end up with an ever growing build queue otherwise.
You can craft particularly nice solutions if you use a script like the above and combine it with the Script SCM plugin. For example, you can synchronize downstream changelogs (and committer notifications) to upstream projects without connecting the downstream to SCM at all. This is a typical setup when you connect a "build job" and one (or more) "test jobs".
When triggering the downstream job, use the Parameterized Trigger Plugin (Post-build Action
called Trigger parameterized build on other projects
) and supply the svn revision as a parameter. You can do this in two ways:
Subversion revision
parameter, it will make sure that if you check out the same source in you down stream job, it will check out the same version as in the upstream (i.e. it copies the hidden SVN Revision Action
which is stored with the upstream build).Predefined parameters
parameter and bind the value of SVN_REVISION
to a variable. That variable will then be available in the downstream job.Here is a related discussion which touches upon the problems with using the Subversion revision
parameter, it is not 100% obvious to the user what SVN revision that has been propagated.