Parameterized Builds in Jenkins – choosing subversion folders
Dieser Artikel wurde von Michael Döcke geschrieben.
For software projects with a large amount of code it is essential to emphasize maintainability. Continuous Integration Systems, such as Jenkins CI, are good instruments to simplify and monitor your builds in an automatic way. An advantage of Jenkins CI – the possibility to integrate some, possibly self-written plug-ins – is the topic of a small project that could be helpful for some Jenkins/Hudson-using developers.
SVNFolderParameterPlugin is a plug-in that provides forms for parameterized builds where you can browse in a Subversion repository for a subdirectory in a specified directory. Together with a custom check-out script, this allows building Jenkins jobs which run against arbitrary source code versions in your Subversion (e.g., in order to rebuild a tagged version). When starting the build, all subfolders will be shown in a dropdown list and the URL of the selected folder will be forwarded as a property to the build script.
Fig. 1: Added parameter in job configuration.
Fig. 2: Combobox filled with folders of the configured Subversion path while starting the build.
The project structure consists of 3 parts which can be found in the ‚src/main‘ folder. While ‚java‘ is clearly the folder for java source files, ‚resources‘ (with jelly files) and ‚webapp‘ (with html files) will be used for displaying web forms by filling their controls with the member-values of the corresponding java objects. The assignment between the jelly and the java files will be achieved by putting the *.jelly in ‚resources‘, in the same subfolder as the java source code and a further folder which is named exactly like the class you refer to. Since Hudson/Jenkins uses Stapler as its web framework, *.stapler files with a constructor definition have to be created next to the java files (again, with the same name), to be used by Stapler.
Fig. 3: Project structure.
As seen in the figure above, only two java source files were necessary in this plug-in. SVNFolderParameterDefinition.java provides functions for resolving the folders for a given Subversion path, for validating user input for the corresponding form and accessing member variables by Stapler. SVNFolderParameterValue.java is a simple Bean-like class which holds the value of the user selection during the build step after submitting the parameters, which is essential for passing the value to the build script.
The form controls shown in Figure 1 will be created by the config.jelly and in Figure 2 by the index.jelly in ‚src/main/resources/com/ubigrate/plugins/hudsonplugins/svn_folder_parameter/SVNFolderParameterDefinition‘. Adding the files in this directory defines which object should be used while filling the form elements by accessing the it- or instance-object fields and getting their values. Accessing these values is done via the “getter”-method of the denoted field.
With this you will be able to access the value parameter from SVNFolderParameterValue.java in value.jelly in the following way (considering a correct folder structure):
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form"> <f:entry title="${it.name}" description="${it.description}"> <f:textbox name="value" value="${it.value}" readonly="true"/> </f:entry> </j:jelly>
The index.jelly from SVNFolderParameterValue is only kept for checking the parameter value after the build.
You can download the project including the sources at:
http://svn.ubigrate.com/tools/trunk/SVNFolderParameterPlugin
Use anonymous:anonymous for login.
The source can be extended easily with additional parameters such as a login field if you don’t want to provide an anonymous read-only subversion-account or some other important arguments which characterize or constrain the access to the repository. You just need to edit the config.jelly and index.jelly for the forms and SVNFolderParameterDefinition.java to handle the input.
Since Jenkins/Hudson allows only *.hpi files as plug-ins, an appropriate build script is needed. The build.xml in the root folder uses the ant war task to create the necessary *.hpi file. You still have to copy the libraries from $HUDSON_HOME/war/WEB-INF/lib into the projects lib folder. Additionally two more libraries are required:
- svnkit.jar (from org.tmatesoft.svn_1.3.5.standalone)
- servlet-api-2.5-20081211.jar
For building this plug-in it’s sufficient to use the default target. You will have noticed that while most of the other Jenkins plug-ins were built with Maven, this one uses ANT. The simple reason for this is to comply with our other builds.
The entire plug-in (SVNFolderParameterPlugin.hpi) is available here.
Just use the advanced options tab in the plug-in manager in your Jenkins/Hudson environment and upload the plug-in. Another possibility is to copy the hpi file into the plugin folder of your Jenkins/Hudson installation. Consider that a restart of Jenkins/Hudson is necessary in both cases.
For developing your own plug-in take a look at the Jenkins plugin tutorial.
Matt Doar sagt
am 4. August 2011 @ 19:52
Nice plugin, but the parameter does not appear to be exported to the REST API using @Exported.
~Matt