Hudson – pass the %build_number%

In our hudson setup we have several jobs per project. For example we’ll have:

  1. build – compiles and runs tests.
  2. integration – runs integration tests. triggered by build’s success
  3. devstage – ftp’s our project to the development test environment. triggered by the build’s success.
  4. release to live boxes. This is a manual trigger.

Each of these jobs has a hudson build number, however only the “build” job’s build matters, because that references the code built, and includes the source control changeset details, including the commit messages. We also use it to generate part of the version number of our product. When we release to the dev environment and the live environment, we use the build number as the version number. When there’s 7 different build numbers, and if you decide to change the name of one of your jobs, this can become very confusing, very quickly.

So we decided we’d like all the child jobs (or downstream) which clone the build workspace, should also have the build’s number. Having a look around for hudson plugins, couldn’t find much to help. There’s the Parameterized Trigger Plugin which works brilliantly, aside from the release jobs which are manually triggered. Then we found the Envfile Plugin. This has absolutely no documentation, so I thought I’d share what we did:

  1. install the Envfile Plugin into Hudson
  2. create a powershell script in your project to create a file setting the required environment variable when the build job is running. Format of the environment variable is VARIABLE_NAME=VALUE. Note the usual SET VARIABLE_NAME=VALUE is resolved to “SET=VARIABLE_NAME=VALUE” by the EnvFile Plugin:
    # Input arguments
    "SOURCE_BUILD_NUMBER=$buildNumber" | out-file -encoding ASCII environmentVariables.txt
  3. get your first hudson job in the stream to run the powershell script:
    powershell.exe %WORKSPACE%\dev\source\ReleaseScripts\CreateEnvfile.ps1 %BUILD_NUMBER%
  4. get your downstream, or child, projects to set the build environment using a file:
  5. Ensure the build scripts in that job reference your new variable


    rather than


And hey presto! Your release folders are the same number as the build project. No more confusion!

4 thoughts on “Hudson – pass the %build_number%

  1. Thanks for this. As you said, there is no documentation for this plugin at all so you have really helped me today.

    Thanks again


  2. you can you a build script like batch file, nant, rake… anything really. You can also use the tasks in Jenkins itself. Step Five is purely to show you how to get that build number for use.

What did you think?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s