Building a software project

Hudson can be used to perform the typical build server work, such as doing continuous/official/nightly builds, run tests, or perform some repetitive batch tasks. This is called "free-style software project" in Hudson.

Setting up the project

Go to Hudson top page, select "New Job", then choose "Build a free-style software project". This job type consists of the following elements:

  • optional SCM, such as CVS or Subversion where your source code resides.
  • optional triggers to control when Hudson will perform builds.
  • some sort of build script that performs the build (ant, maven, shell script, batch file, etc.) where the real work happens
  • optional steps to collect information out of the build, such as archiving the artifacts and/or recording javadoc and test results.
  • optional steps to notify other people/systems with the build result, such as sending e-mails, IMs, updating issue tracker, etc.

For more details, click the icons in the configuration page.

Hudson Set Environment Variable

Hudson sets some environment variables that are available to shell scripts, Windows batch files, and Ant files that are executed by Hudson^1^. A list of environment variables and how they are used are shown below.

Builds for Non-Source Control Projects

There is sometimes a need to build a project simply for demonstration purposes or access to a SVN/CVS repository is unavailable. By choosing to configure the project  as "None" under "Source Code Management" you will have to:

  1. Build the Project at least once, (it will fail), but Hudson will create the structure hudson/jobs/PROJECTNAME/workspace
  2. Copy the project files to hudson/jobs/PROJECTNAME/workspace
  3. Build again and configure appropriately

Hudson Set Environment Variables

When a Hudson job executes, it sets some environment variables that you may use in your shell script, batch command, or Ant script1. The following table contains a list of all of these environment variables.

Environment Variable Description
BUILD_NUMBER The current build number, such as "153"
BUILD_ID The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss)
JOB_NAME Name of the project of this build. This is the name you gave your job when you first set it up. It's the third column of the Hudson Dashboard main page.
BUILD_TAG String of hudson-${JOBNAME}-${BUILD_NUMBER}. Convenient to put into a resource file, a jar file, etc for easier identification.
EXECUTOR_NUMBER The unique number that identifies the current executor (among executors of the same machine) that's carrying out this build. This is the number you see in the "build executor status", except that the number starts from 0, not 1.
JAVA_HOME If your job is configured to use a specific JDK, this variable is set to the JAVA_HOME of the specified JDK. When this variable is set, PATH is also updated to have $JAVA_HOME/bin.
WORKSPACE The absolute path of the workspace.
SVN_REVISION For Subversion-based projects, this variable contains the revision number of the module. If you have more than one module specified, this won't be set.
CVS_BRANCH For CVS-based projects, this variable contains the branch of the module. If CVS is configured to check out the trunk, this environment variable will not be set.

Shell Scripts and Windows Batch Commands

If you're using a shell script to do your build, you can either put these environment variables directly into your shell scripts, or call them as parameters in your shell script. Below is an example how this can be done:

If you are executing a Windows Batch Command, the variables should be referenced using the %VARIABLE_NAME% pattern. For example:



Ant Scripts

If you're using an Ant script to do your build, you may include environment variables in property settings. Click on the Advanced... button just below where you put the Ant targets you want to build. This will display the Properties box. Below is an example how to use the Properties box to set Ant properties with Hudson Environment variables:

As an alternative, you can use the Environmental prefix to pull in all environmental variables as properties right inside your build.xml file. Below is an example how to set the property "label" to include the Project Name and the Build Number:

<property environment="env"/>
<property name="label" value="${env.JOB_NAME}-${env.BUILD_NUMBER}"/>

Configuring automatic builds

Builds in Hudson can be triggered periodically (on a schedule, specified in configuration), or when source changes in the project have been detected, or they can be automatically triggered by requesting the URL:

http://YOURHOST/hudson/job/PROJECTNAME/build

This allows you to hook Hudson builds into a variety of the set up. For more information (in particular doing this with security enabled), see Remote access API.

Alternatively, if you'd like to trigger builds from scripts when Hudson security is enabled, select "Trigger builds remotely" option in your project configuration, and provide an authorization token there. Now you can trigger your builds via the following URL:
http://YOURHOST/hudson/job/PROJECTNAME/build?token=YOUR_TOKEN_HERE

Builds by e-mail (sendmail)

If you have the root account of your system and you are using sendmail, I found it the easiest to tweak /etc/aliases and add the following entry:

hudson-foo: "|/bin/wget -o /dev/null http://YOURHOST/hudson/job/PROJECTNAME/build"

and then run "newaliases" command to let sendmail know of the change. Whenever someone sends an e-mail to "hudson-foo@yoursystem", this will trigger a new build. See this for more details about configuring sendmail.

Builds by e-mail (qmail)

With qmail, you can write /var/qmail/alias/.qmail-hudson as follows:

|/bin/wget -o /dev/null http://YOURHOST/hudson/job/PROJECTNAME/build"

Builds by changes in CVS

To perform builds whenever someone makes a change in the repository, either invoke the above program from /CVSROOT/loginfo, or hook up CVS notification e-mails as explained above.

Or, you could configure Hudson to poll your source control system periodically and start new builds when the changes have been detected.


1 Note: I believe Maven builds also have access to these variables. Can someone verify this?

Labels

  Edit Labels
(None)
  1. Feb 11, 2008

    Anonymous says:

    I am using the $ notation in a maven build successfully. mvn Dbuild.id=$ clean ...

    I am using the $

    Unknown macro: {BUILD_NUMBER}

    notation in a maven build successfully.

    
    

    mvn -Dbuild.id=$

    clean install

    
    
  2. Feb 27, 2008

    Anonymous says:

    You can reference Hudson variables as any other property when building with Mave...

    You can reference Hudson variables as any other property when building with Maven2.

    Hudson version 1.184

  3. Feb 27, 2008

    Anonymous says:

    I&nbsp;am using&nbsp;Hudson 1.44, and I have two versions of JDK installed: 1.4 ...

    I am using Hudson 1.44, and I have two versions of JDK installed: 1.4 and 1.6. Some projects I need to build in 1.4 and others in 1.6. I created two JDK instances in Hudson in the configuration file (config.xml):

    <jdk>
          <name>jdk 1.4</name>
          <javaHome>C:\j2sdk1.4.2_12</javaHome>
        </jdk>
        <jdk>
          <name>jdk 1.6</name>
          <javaHome>C:\jdk1.6.0_04</javaHome>
        </jdk>

    Inside of my project definition, I select jdk 1.6, and I set the Ant build command in the following way:  

    -DJAVA_HOME=C:\jdk1.6.0_04 -DPATH=C:\jdk1.6.0_04\bin

    Unfortunately, when I build I get the following error:

     [javac] C:\hudson\jobs\<project>\workspace\<class>.java:18: cannot access java.lang.Object
        [javac] bad class file: C:\jdk1.6.0_04\jre\lib\rt.jar(java/lang/Object.class)
        [javac] class file has wrong version 49.0, should be 48.0
        [javac] Please remove or make sure it appears in the correct subdirectory of the classpath.
        [javac] private String text;
        [javac]                 ^
        [javac] 1 error

    What am I doing wrong?

  4. Mar 28, 2008

    Bila Nagirner says:

    Hello, &nbsp;Is there a way to customize the BUILDNUMBER value?&nbsp; (ie. padde...

    Hello,

     Is there a way to customize the BUILD_NUMBER value?  (ie. padded 0's)

    Also, how can I reset this value to 1 after doing a few test builds?

     
    Thanks,

    Bila

    1. Jul 08, 2008

      Mark K says:

      I was able to do this by updating the number in nextBuildNumber text file inside...

      I was able to do this by updating the number in nextBuildNumber text file inside of the jobs directory (and all sub-projects), and then bouncing the hudson server.

  5. Jun 27, 2008

    Adam says:

    Hi,&nbsp; what is the value of BUILDID in case of triggering builds by scheduled...

    Hi, 

    what is the value of BUILD_ID in case of triggering builds by scheduled "Poll SCM" . Because it looks like this variable keeps time of starting "scanning" not the time of build (at least value of BUILD_ID is different than the name of folder with particaular build in build history). Could somebody help me?

    Thanks,

    Adam 

  6. Sep 08, 2008

    Amit says:

    Hi I am unable to customize my build process number ? I want to set my build num...

    Hi

    I am unable to customize my build process number ?

    I want to set my build number as TMT-100 so that when i trigger the build next time it becomes TMT-101,

    updating the number in nextBuildNumber is not effective for me.

    can anyone help me out of this issue.

    Thanks

    Anand

  7. Sep 16, 2008

    Rex Morrey says:

    We are using Hudson to manage a bunch of custom builds and like the quickness af...

    We are using Hudson to manage a bunch of custom builds and like the quickness afforted by Hudson in configuring a build. One of the issues we are trying to resolve is when there are two different branches involved in producing a build.  For example, many of our products use a common library, and occasionally a new branch of that common library is needed, along with the branch of a module (different modules).  It appears that Hudson's CVS setup only allows us to identify one branch, when what we need is a second CVS setup to identify an additonal branch. 

    Does anyone know how to handle such a situation?

  8. Apr 08

    Clement MAHE says:

    Hi&nbsp; Does Hudson accept SVN Readonly repository ? Because i have some proble...

    Hi 
    Does Hudson accept SVN Read-only repository ? Because i have some problems to configure it.

    In fact i can make update in command line but when i try to enter credential, it indicate that kind of message :

    "FAILED: svn: OPTIONS of '/svn/projectname/trunk': 403 Forbidden (http://servername:serverport)"

    With same repository url and same account of course.

    Thanks

  9. Apr 15

    will thai says:

    Hi &nbsp;I have a problem when using the 'use update' checkbox.&nbsp; I need to ...

    Hi

     I have a problem when using the 'use update' checkbox.  I need to have it unchecked to ensure a clean environment.

     The project I'm building generates files that is owned by 'root'.  So when hudson tries to deletes these files, the build fails.

     How do I fix this problem?

    thanks.

  10. Jun 12

    fabrice says:

    Hi I am trying to develop a postcommit svn hook script in order to run hudson bu...

    Hi

    I am trying to develop a post-commit svn hook script in order to run hudson build but it is a hard task !

    It would be a good idea to write on the wiki a such script in order to avoid to lose time writting it

    Here my incomplete scrip (I have to find a way to get the projectName because REPOS is only ther path of the repo -..., maybe see svnlook changed -r $REV)

    REPOS="$1"
    REV="$2"

    HUDSON_URL="http://tl-em-1:8080/hudson/#PROJECT#/build?token=BUILD_ALMERYS_JOB"

    oldIFS=$IFS
    IFS="/"
    Array=( $REPOS )
    IFS=$oldIFS

    LastElementIndex=$

    Unknown macro: {#Array[@]}
    -1

    ProjectName=$

    Unknown macro: {Array[$LastElementIndex]}

    HUDSON_URL=$( echo "$HUDSON_URL" | sed s/#PROJECT#/$ProjectName/g )

    echo $REPOS >> /tmp/commit.txt

    /usr/bin/wget -o /dev/null "$HUDSON_URL"