Wednesday, April 06, 2011 10:43 PM
Sean Feldman
Jenkins – Getting Revision for Project with Multiple Modules
I ran into this problem just recently on a project that has more than a single repository for a project. Using Jenkins (Hudson) was awesome for the past 2+ years and I was surprised that it couldn’t handle revision environment variable assignment when more than a single module had a place in a build job. According to some threads, this is done by design. One workaround in particular that I liked was to look into the poll log file and get information out of it.
This is a quick implementation of a get-revision function for nant to grab revision based on the project name (stringToSearch argument) and its looking at the polling log file per project.
.
1: <script language="C#" prefix="utils"> 1:
2: <imports>
3: <import namespace="System.Globalization"/>
4: <import namespace="System.IO"/>
5: <import namespace="System.Text.RegularExpressions"/>
6: </imports>
7: <code>
8: <![CDATA[
9: [Function("get-revision")] 10: public static int GetRevision(string filePath, string stringToSearch)
11: { 12: if (!System.IO.File.Exists(filePath))
13: return 0;
14: System.IO.FileInfo file = new System.IO.FileInfo(filePath);
15: using (System.IO.StreamReader stream = file.OpenText())
16: { 17: System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(stream.ReadToEnd(), stringToSearch + @".*\srevision\s(?<revision>.*?)\r #string_to_search <ANYTHING>revision(group_revision)\r",
18: System.Text.RegularExpressions.RegexOptions.Multiline |
19: System.Text.RegularExpressions.RegexOptions.IgnoreCase |
20: System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
21: return match.Success ? int.Parse(match.Groups["revision"].Value, System.Globalization.NumberStyles.Any) : 0;
22: }
23: }
24: ]]>
25: </code>
26:
</script>
And this is how the typical usage would look like:
1: <property name="svn.revision" value="0" readonly="false"/>
2: <if test="${environment::variable-exists('SVN_REVISION')}">
3: <property name="svn.revision" value="${environment::get-variable('SVN_REVISION')}" />
4: </if>
5: <if test="${not environment::variable-exists('SVN_REVISION')}">
6: <property name="svn.revision" value="${utils::get-revision('.\..\scm-polling.log', project::get-name())}" />
7: </if>
As an afterthought, I could actually set SVN_REVISION to the value from the log, and then work with SVN_REVISION as usual.
Filed under: CI