>Local Continuous Integration with Hudson and Git

>One of the practices from Continuous Integration is that builds should be run locally before committing to the main repository. Recently I’ve been working with Hudson and git to setup a local continuous integration server. With git I have a complete version of the repository and can commit locally. With Hudson, I can setup a job that monitors the local repository for any new changes. This way when I either commit code to the local repository, or pull in code from remote repository, hudson can trigger a local build.

Why do this? So that I make sure that my code is known to work at that particular point in time outside of my particular workspace environment. Builds should be IDE agnostic. Not everybody uses the same ide (I hear the eclipse community gasp).

Here are the steps:

  1. Download a copy of the Hudson war file.
  2. Start hudson with the following command: java -jar hudson.war –httpPort=8081 (Note: I choose to start it on a unique part due to possible local server conflicts that can occur)
  3. Access the local running hudson instance by going to http://localhost:8081/
  4. Go to Manage Hudson
  5. On the Available Plugins page select the git plugin, and install it.
  6. Restart Hudson

By installing the git plugin you will be able to have Hudson monitor the git repositories that you want to build against. You can setup multiple jobs for hudson. The next steps assume that you have already have a git repository on your local development machine.

  1. Go to http://localhost:8081/
  2. If one does not exist, create a new Job.
  3. On the configuration screen fill in the necessary information on how you want the job to be created.
  4. For the Source Code Management options, select git.
  5. Add a git repository.
  6. For the URL you can use a file based path like: file:///home/dcarver/Work/somegitproject
  7. Configure the branch you want to build against: origin/replacemewithbranch
  8. In the build section of the configuration page, select Poll SCM.
  9. Set up an appropriate polling schedule to check for changes in your local git repository.
  10. Fill in the rest of the build configuration steps.

If you setup your Polling for every 45 minutes. Your local running hudson will check your local git repository for changes every 45 minutes, if there are new commits or you have pulled recently from a remote repository it will start a build.

You can setup your local hudson to notify you be email, or however you want, if the local build fails. This way you can be sure that the code you are working on is working with out having to remember to jump out and run a build after making some changes. Some builds can take a while so you may or may not want to run an entire build but only critical pieces.

A DVCS system like git allows for this new type of workflow, and with the ease of setup of Hudson, one can easily setup a job that just runs in the background. If you happen to have a spare local machine laying around doing nothing, you could set that up as your local CI server and let it build the system for you as well.

Being able to catch issues while they are still close to the code that you worked on is critical in helping improve the code, and reduce the number of bugs we introduce into the upstream repository. This method may not catch all the bugs, but it should help catch a number of them.

Note: I may update this particular entry from time to time.

Advertisements
This entry was posted in agile, build, eclipse, testig, xml. Bookmark the permalink.

5 Responses to >Local Continuous Integration with Hudson and Git

  1. Rafał Rusin says:

    >Hudson is great. This post is cool, especially while you work with svn remote repository (like one at apache.org). If you use Hudson Git plugin, there's an option to build multiple branches (like master*). So you can push your local changes to verify if they work first, like this:git push origin HEAD:master-yournickand do 'build now' on server to verify if your patch works. If so, you can delete test branch by:git push origin :master-yournickand push changes to master.Hudson Git plugin also has auto-merge working branch option, which sounds cool, but I haven't tried that yet. Regards

  2. Miles Parker says:

    >This is exactly what I've been looking for. I actually found your post because I was looking for an answer to the conundrum "what do I use for the darn URL?" With this setup, you can burn your cake and not have to eat it to!But you forget the best use case, which is…debugging hudson build scripts.

  3. prabir says:

    >my git repository is in remote server and requires to use password. any ideas on how to do it?

  4. KP says:

    I have configured the job as suggested but I get the following error. Can anyone help, please?

    Started by user anonymous
    Building in workspace C:\Program Files (x86)\Jenkins\jobs\abc\workspace
    > git.exe rev-parse –is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
    > git.exe config remote.test_repo/test-release.url file:///gitrepo/test_repo/esb_services/EquipmentActivity_Service.application # timeout=10
    Fetching upstream changes from file:///gitrepo/test_repo/esb_services/EquipmentActivity_Service.application
    > git.exe –version # timeout=10
    > git.exe -c core.askpass=true fetch –tags –progress file:///gitrepo/test_repo/esb_services/EquipmentActivity_Service.application +refs/heads/*:refs/remotes/test_repo/test-release/*
    ERROR: Error fetching remote repo ‘test_repo/test-release’
    hudson.plugins.git.GitException: Failed to fetch from file:///gitrepo/test_repo/esb_services/EquipmentActivity_Service.application
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:763)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1012)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1043)
    at hudson.scm.SCM.checkout(SCM.java:485)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1276)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:607)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:86)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:529)
    at hudson.model.Run.execute(Run.java:1738)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:410)
    Caused by: hudson.plugins.git.GitException: Command “git.exe -c core.askpass=true fetch –tags –progress file:///gitrepo/test_repo/esb_services/EquipmentActivity_Service.application +refs/heads/*:refs/remotes/test_repo/test-release/*” returned status code 128:
    stdout:
    stderr: fatal: ‘C:/Program Files/Git/gitrepo/test_repo/esb_services/EquipmentActivity_Service.application’ does not appear to be a git repository
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1693)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1441)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:62)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:313)
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:761)
    … 11 more
    ERROR: null
    Finished: FAILURE

    • kingargyle says:

      It looks like you are giving it a filename to one of the files and not the Directory where your git repository is stored. An example, file:///Users/davidcarver/Work/NineWorlds/serenity-android

      Try just giving something like: file:///gitrepo/test_repo/esb_services

      The above url works for Linux/unix type operating systems for Windows you may have to get more specific, it’s been a while since I worked on a windows machine, but the concept should still work the same.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s