Showing posts with label TortoiseSVN. Show all posts
Showing posts with label TortoiseSVN. Show all posts

SVN, Hudson & MSBuild - Building code on post commit

SVN, Hudson and MSBuild - Revision control repository
SVN, Hudson and MSBuild - Continuous Integration

This is the third and last installment in the series I’m writing about SVN, Hudson and MSBuild.

Today I’m going to show you the last piece that actually makes the whole thing work. We could call this the plumbing. The piece lies within a specific SVN folder related to your project. It’s called hooks. The path to the hooks folder is this:

Project’s hooks folder before the set upFigure 1 - Project’s hooks folder before the setup

As you can see there are some template files ( .tmpl ). The one we’re going to use to inform Hudson that it’s time to build the code just committed to the repository is the file post.commit.tmpl. Make a copy of this file and change its extension to .bat since it’ll be used by SVN to execute some commands. The file should be named post-commit.bat.

Open the .bat file and add this code at the end:

SET REPOS=%1
SET REV=%2
SET CSCRIPT=C:\WINDOWS\system32\cscript.exe
SET VBSCRIPT=C:\svn\post-commit-hook-hudson.vbs
SET SVNLOOK=C:\Program Files\VisualSVN Server\bin\svnlook.exe
SET HUDSON=http://leniel-pc:8080/
"%CSCRIPT%" "%VBSCRIPT%" "%REPOS%" %REV% "%SVNLOOK%" %HUDSON%

Note above that we’re setting some vars and pointing to some specific files:

- CSCRIPT points to cscript.exe file that should be present in your Windows system32 folder.

- VBSCRIPT points to to the post-commit-hook-hudson.vbs file and its code is as follows:

repos   = WScript.Arguments.Item(0)
rev     = WScript.Arguments.Item(1)
svnlook = WScript.Arguments.Item(2)
hudson  = WScript.Arguments.Item(3)

Set shell = WScript.CreateObject("WScript.Shell")

Set uuidExec = shell.Exec(svnlook & " uuid " & repos)
Do Until uuidExec.StdOut.AtEndOfStream
  uuid = uuidExec.StdOut.ReadLine()
Loop
Wscript.Echo "uuid=" & uuid

Set changedExec = shell.Exec(svnlook & " changed --revision " & rev & " " & repos)
Do Until changedExec.StdOut.AtEndOfStream
  changed = changed + changedExec.StdOut.ReadLine() + Chr(10)
Loop
Wscript.Echo "changed=" & changed

url = hudson + "subversion/" + uuid + "/notifyCommit?rev=" + rev
Wscript.Echo url

Set http = CreateObject("Microsoft.XMLHTTP")
http.open "POST", url, False
http.setRequestHeader "Content-Type", "text/plain;charset=UTF-8"
http.send changed

- SVNLOOK points to svnlook.exe file that comes with VisualSVN Server (see part 1 of this series for more details about it).

- HUDSON points to your Hudson server address. Change it accordingly.

With it all configured we should be ready to get an automatic build when code is committed to the repository.

To test your environment, change any file already versioned and commit it. Open Hudson in your browser and watch a new build start automatically.

If you look in Hudson’s build Console Output you’ll see that the build was initiated by an SCM change.

That’s all!

This is how your SVN project hooks folder should look like now:

Project’s hooks folder after the set upFigure 2 - Project’s hooks folder after the setup

Can you spot another .bat file in the folder? It’s the pre-revprop-change.bat. I’ve been using it so that I can modify the commit’s log message/comment when I forget to mention something or to correct spelling. More info about this file can be seen in this StackOverflow question: What is a pre-revprop-change hook in SVN and how do I create it?

SVN, Hudson and MSBuild - Revision control repository

SVN, Hudson and MSBuild - Continuous Integration
SVN, Hudson and MSBuild - Building code on post commit

This three part series of posts I’m starting today serves as a guide on how to setup a continuous integration/CI server.

Tools I’m going to use (all FREE) and a brief description of them:

Continuous integration

Hudson Hudson

Hudson monitors executions of repeated jobs, such as building a software project or jobs. Hudson helps building/testing software projects continuously. In a nutshell, Hudson provides an easy-to-use so-called continuous integration system, making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. The automated, continuous build increases the productivity.


Build automation

MSBuild (Visual Studio 2010 native build system)

The Microsoft Build Engine (MSBuild) is the build platform for Microsoft and Visual Studio. MSBuild is completely transparent with regards to how it processes and builds software, enabling developers to orchestrate and build products in build lab environments where Visual Studio is not installed.


Revision control

TortoiseSVN

TortoiseSVN is an easy-to-use SCM / source control software for Microsoft Windows and possibly the best standalone Apache™ Subversion® client there is. It is implemented as a Windows shell extension, which makes it integrate seamlessly into the Windows explorer. Since it's not an integration for a specific IDE you can use it with whatever development tools you like.

VisualSVN Server

VisualSVN Server allows you to easily install and manage a fully-functional Subversion server on the Windows platform. Thanks to its robustness, unbeatable usability and unique enterprise-grade features, VisualSVN Server is affordable both for small business and corporate users. It greatly simplifies installing and managing Subversion and is available for free.

The series will focus on continuous integration of .NET family of projects and I’ll write things in the order I installed them.

This 1st part will deal with the Revision Control repository configuration.

The only difference here is that I’m running all these software engineering tools on my Windows 7 virtual machine that is used exclusively for software development. A more ubiquitous situation is when/where you have each one of these tools working on their specific servers, that is, you’ll frequently see a dedicated continuous integration server, a dedicated build server and a dedicated repository server.

Having dedicated servers is good for a software development team/company. This definitely isn’t my case. I’m doing solo work and as such I decided to have a stable environment which can give me all the benefits of a continuous integration process running in a single machine (cost effective solution). This allows me to apply a better quality control over my code and at the same time I can automate common tasks.

These tools are working great on my machine right now where I’m developing and integrating an ASP.NET MVC 3 application. Bellow I’ll describe what you need in order to get a revision control repository running in your development machine.

I try to simplify every step going to the point and avoiding the intrinsic aspects such as why things are named or done this or that way.

When you finish this step by step guide, the following question should be answered:

Question: How/Why should I put my code under revision control?

Valid answer:

1 - Download and install TortoiseSVN. TortoiseSVN is a Subversion client.

2 - Create the SVN folder for your project. For example in C:\svn\MyProject. Right-click this folder and select TortoiseSVN → Create repository here. You should get a confirmation message like this one:

TortoiseSVN - Repository Successfully CreatedFigure 1 - Repository Successfully Created

3 - Create this temporary folder structure so that you have a standard SVN folder layout:

C:\Temp\MyProject\trunk
C:\Temp\MyProject\branches
C:\Temp\MyProject\tags

Do not mind the Temp folder for now. The main idea here is that you’ll put you current project’s folders/files inside the trunk folder. For more on this, read revision control.

4 - Move all MyProjects’s code to the trunk folder.

5 - Now right click the folder C:\Temp\MyProject and select TortoiseSVN → Import… Now you must provide the SVN address where you want to import your project. Do this:

TortoiseSVN - ImportFigure 2 - Import

Note the slashes ( / ) instead of back slashes ( \ ).

Before you import the project into the repository take note of:

- Just import the content needed to build the project. Remove unnecessary or temporary files/folders generated by the compiler e.g. Files = *.suo/*.user and folders = Bin/obj that contain compiled binaries.
- Organize your project in folders and subfolders. Using TortoiseSVN you can later rename/move files but it’s highly recommended that you set your project’s structure before importing!

After clicking OK, you should see this:

TortoiseSVN - MyProject Import FinishedFigure 3 - MyProject Import Finished

Great. Now we have just imported MyProject’s files to a revision control repository. See that we are at revision 1.

Now we’re going to check out this code in a different folder so that we can have the benefits of revision control.

6 - Create the folder C:\MyProject.

7 - Right click C:\MyProject and select SVN Checkout…

TortoiseSVN - CheckoutFigure 4 - Checkout

After clicking OK, you should get this:

TortoiseSVN Checkout FinishedFigure 5 - Checkout Finished

You’ll see that Windows Explorer now shows an icon in each file/folder informing that that specific file/folder is up to date or not with the one we have in the SVN repository:

Windows Explorer with Project Under Version ControlFigure 6 - Windows Explorer with Project Under Version Control

As you can see, I have changed the content of the file Main line Project Files and Folders go here.txt and as a result of this action I’m informed that this file was changed since the last check out (red exclamation mark). When I perform a commit to the repository this file that is marked as modified will be part of the head revision of the repository.

TortoiseSVN context menu options
SVN is a really powerful tool that has a lot of functions as the revision graph selected bellow:

TortoiseSVN - Context Menu OptionsFigure 7 - TortoiseSVN Context Menu Options

You’ll learn about and use each one of these functions for sure as the time passes by. Do not panic and take it easy! Surprised smile Be careful though when performing a commit to the repository and avoid breaking things. This can cause headaches.

Summary
In this simple step by step guide you saw how to put a project under revision control. This gives you a cool way of keeping track of what has been added, deleted, changed in your project since its start through what we call revisions. In addition you also get the chance of rolling back your changes in case anything goes wrong. This I think is the greatest feature of a revision control repository, that is, it acts in such a way that if I later decide to rollback to a previous version of a given file I’m allowed to do so because it keeps the old files. This works just like the database rollback function.

Next in this series
In the 2nd part of this series I’ll go into the details related to the installation and configuration of Hudson that is the continuous integration server.