Context:
- a build job is a declarative pipeline, almost always part of a multibranch pipeline job
- a Subversion checkout will have been performed either implicitly, or, if there are multiple parallel builds involved, via
checkout scm: scm(withpollandchangelogset totruefor one of them, andfalsefor the rest)
I now need to obtain the Subversion revision number corresponding to that checkout (for the main source, not any externals) from a shared pipeline function (currently just a groovy file in vars).
My existing approach is to run svnversion.
Drawback there is that this requires running a sh or bat step (depending on OS) with returnStdout: true; that feels like unnecessary extra overhead, and also causes these steps to be registered and visible in the pipeline info, and I would prefer to keep the internals of my function, well, internal.
So, first question: is there an exposed API that enables running a process on the current node and file path and capturing its output without having to go through a sh or bat step?
Alternatively, I could also try to get the revision directly from the SCM object.
Looking at SubversionSCM, there are 2 promising APIs:
-
getRevisionFile()gives me aFileobject (pointing to a path like/var/lib/jenkins/jobs/…/branches/trunk/builds/17/revision.txt)However, this has two significant drawbacks:
- I need to pass a
RuntogetRevisionFile(), and the script approval is not happy about granting access tocurrentBuild.getRawBuild() - while I can get its contents that way, I’d need to assume that the first line is the most relevant one, and would then also need to parse out the revision number (does not seem that hard, but I don’t know how official and document those file contents are).
- I need to pass a
-
getLocations()gets me a list of locations, and it looks likescm.getLocations()[0].getRevision(null).getNumber()gets me what I want (obviously the real code would have size and null checks), but I’m not sure how guaranteed that information is, and whether it’s always the first one that has the info I need.
Which of these is the safest / most supported approach? Are there others?
Note that the Subversion plugin claims to set environment variables:
The Subversion SCM plugin exports the svn revisions and URLs of the build’s subversion modules as environment variables. These are $SVN_REVISION_n and $SVN_URL_n, where n is the 1-based index of the module in the configuration.
For backwards compatibility if there’s only a single module, its values are also exported as $SVN_REVISION and $SVN_URL.
which would be a lot more convenient to use. However, when I try
script {
println env.SVN_REVISION
println env.SVN_REVISION_1
println env.SVN_REVISION_0
}
after the checkout step, they all print null (and without the env. they error out entirely).