Creating tags or branches for Subversion based on Hudson builds

HudsonJenkinsSubversion

Hudson has a builtin mechanism for tagging. Using this default mechanism helps a lot. However if you have multi-module jobs, you could still be stuck making the same changes in all those paths. Especially when you want to create branches instead of tags. Of course these manual changes are very errorprone. Especially when you are in a hurry. So it can be improved and that’s what this Ant script is about.

What does it do?

Basically it reads the revision.txt of a specific build of a job in Hudson.  All the urls in this file are used to tag this build by Hudson automatically. The script itself is meant to run from a Hudson job too.

The Ant script

<project name="Creating tags and branches" basedir=".">

  <!-- path to the svnant libraries -->
  <path id="svnant.classpath">
    <fileset dir="${basedir}/lib/svn">
      <include name="**/*.jar"/>
    </fileset>
  </path>

  <!-- load the svn task -->
  <typedef
    resource="org/tigris/subversion/svnant/svnantlib.xml"
    classpathref="svnant.classpath" />

  <!-- antcontrib -->
  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="${basedir}/lib/ant-contrib-1.0b3.jar"/>
    </classpath>
  </taskdef>

  <target name="create tag or branch">
    <loadfile property="revisions.file"
      srcFile="../../../${projectname}/builds/${builddatetime}/revision.txt" />
    <for param="source.url"
      list="${revisions.file}"
      delimiter="${line.separator}">
      <sequential>
        <propertyregex property="source.url.without.source"
          input="@{source.url}" regexp="${source}"
          replace="${tagorbranchpath}/${tagorbranchname}"
          casesensitive="false" override="true" />
        <propertyregex property="source.url.without.revision"
          input="@{source.url}" regexp="/[^/]*$" replace=""
          casesensitive="false" override="true" />
        <propertyregex property="destination.url"
          input="${source.url.without.source}"
          regexp="/[^/]*$" replace="" casesensitive="false"
          override="true" />
        <propertyregex property="revision.with.slash"
          input="${source.url.without.source}"
          regexp="${destination.url}" replace=""
          casesensitive="false" override="true" />
        <propertyregex property="revision"
          input="${revision.with.slash}" regexp="/"
          replace="" casesensitive="false" override="true" />
        <echo>svn copy srcUrl="${source.url.without.revision}" destUrl="${destination.url}" revision="${revision}" message="${message}"</echo>
        <svn>
          <copy srcUrl="${source.url.without.revision}"
            destUrl="${destination.url}"
            revision="${revision}" message="${message}" />
        </svn>
      </sequential>
    </for>
  </target>
</project>

Creating a project for the script

Configuring the Hudson job

Type Name Description
String source Set the default value to trunk. This will be the start of your branch. For feature branches use trunk. For release branches you would use something like tags/1.0. You probably would not use a branch from here. Although nothing is stopping you from doing it.
Choice projectname At Choices put the job names you want to create branches or tags on.
String builddatetime 2010-12-09_20-18-52 is an example of the input needed. It is the name of the directory of the build used by Hudson. The tag or branch will be created on the revisions of this build.
Choice tagorbranchpath Enter the choices tags and branches.
String tagorbranchname This will be the name for the new branch or tag. For example MyProduct 1.0.
String message Set the default value to Created by using the Hudson job ‘Create tag or branch’. This is the message to be stored in Subversion for this action.

Example of usage

After the job to create a branch or tag is created, you can start using it. For example to create a release branch for release 1.0 of the job called MyProduct (trunk) do the following:

Now your all set to create a stable release from your new release branch!

Possible future changes

If the script would change the build.xml of the Hudson job too, the tag-image would be shown in Hudson.

Have fun with it!

Of course there’s nothing stopping you from changing this script to your needs.

Comments

Leave a Reply