CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, May 26, 2008

Microsoft .NET Web Application using CruiseControl.NET and NAnt

1 Abstract

The implementation of a reliable build and deployment process is the most important part of the Software Development Life Cycle (SDLC). It is difficult and inconvenient to build and deploy .NET application having multiple projects. Such application complicates the process during manual build and deployment. The CruiseControl.NET and NAnt tools are used to automate the complex build and deployment process.

The CruiseControl.NET is an automated continuous integration server implemented using the Microsoft .NET Framework. It can be used to customize the build process by modifying configuration file (ccnet.config) and also help integration with other external tools like NAnt and Visual Studio. NAnt is an advanced cross-platform XML build system for .NET software. The integration of NAnt script with CruiseControl.NET provides automation to build and deployment process.

This article is about providing ideas, practices, and a platform for standardizing approaches to build and deploy processes. In this article, we’ll review the configuration of CruiseControl.NET with NAnt scripts and executing build and deployment. We’ll see how to create NAnt script and also explore CruiseControl.NET configuration file.

System Architects and .NET developers are the targeted audience for this article who has knowledge of Visual Studio 2005 and XML.

2 cruisecontrol.net and NANT

  2.1 CruiseControl.NET

  2.2 NAnt

  2.3 Why and When to use?

3 build and deployment process

  3.1 Create a sample .NET Web Application

  3.2 Create NAnt scripts for the build and deployment

  3.3 Configure NAnt scripts with CruiseControl.Net

  3.4 Execute build and deploy

4 Best Practices

5 Conclusion

6 abbrEvations, Acronyms and Nomenclature

7 Disclaimer

2 cruisecontrol.net and NANT

This section describes the significance of CruiseControl.NET and NAnt tools.

2.1 CruiseControl.NET

CruiseControl.NET is an Automated Continuous Integration [ACI] server implemented using the Microsoft .NET Framework. ACI is a process which monitors the version control system for changes/modifications and automatically runs the build process. CruiseControl.NET can be configured to run build and deployment automatically by setting its config file to run at particular time.

The CruiseControl.NET has a user interface called “Web Dashboard” using which one can execute and schedual build projects. It also provides consolidated log report for each execution and maintains the history.

Advantages of CruiseControl.NET

  • Integration with Visual Source Safe control (VSS)
  • Integration with other external tools, such as NAnt and Visual Studio
  • Can build multiple projects on one server
  • Can execute the build manually or schedule it at a suitable daytime
  • The scheduling of the build is as easy as modifying a XML based configuration file
  • The build report and logs are available at one place which makes it easy to track the history of all the builds.

2.2 NAnt

NAnt is an advanced cross-platform XML build system for .NET software available free as an open source. NAnt parses XML build files and executes the instructions gained from the file. The XML build file consists of a set of instructions for the build of a .NET solution. The build process includes tasks like compiling source code, running unit tests, configuring build-specific settings, etc. NAnt, as a tool, helps automate the build process by providing necessary power and flexibility to highly customized build actions of specific applications.

A normal build process mentioned below can be automated using NAnt.

  • Extract the latest source from Visual Source Safe (VSS)
  • Configure the build (Output of the build into a directory, the build number, scheduling, log, etc)
  • Compile the code
  • In order to deploy, package the output

NAnt enables one to automate the aforementioned steps instead of manual process of running commands and copying files. The NAnt script doesn’t require any special editor, can be created using Notepad (or XML editor) and saved as “.build” extension.

Advantages of NAnt:

  • Used to build, compile and run the .NET projects
  • It can easily handle the build although different modules of the project written in different languages like C#, VB, etc.
  • NAnt can easily integrate with NUnit testing tool - NUnit 2.0
  • NAnt can also perform other tasks like creating, deleting, copying files or directories, sending emails, zipping files, etc.
  • It allows to integrate .NET assemblies in the script which makes it easy to call any customized Method or Function.

2.3 Why and When to use?

The CruiseControl.NET and NAnt have some advantages over the new build and deployment tools like Visual Studio Team System (VSTS) and Team Foundation Server (TFS). Due to cost effective, reusability, open source and minimum infrastructure requirements makes CruiseControl.NET and NAnt more effective for the low budget and less resources project.

The comparison between tools, CruiseControl.NET/NAnt and VSTS/TFS used for build and deployment process is mentioned below:

Attributes

CruiseControl.NET, NAnt (using VSS)

VSTS, TFS

Cost

Free and Open Source

Requires License

Operation System

Windows 2000, XP or higher

Windows 2003

Installation & Configuration

Simple

Time consuming and complex

Source Control

Visual Source Safe (VSS)

Inbuilt, Team Foundation Version Source Control (TFVC)

Security

VSS based security on Source files

Windows/ Active Directory based security

Storage

File System ( VSS)

SQL Server 2005 (TFVC)

Features :

 

Multiple Check Out

Not Supported by VSS

Supported

File Merge

Not Supported by VSS

Supported,

Manual and Automated Merge

File Lock

Exclusive Lock.

Once file check-out, no one can check out or in

Lock Types:

None: shared check-out

Check-in: Others can check-out, but not check-in

Check-out: exclusive lock, no one can check out or in

Shelving

(Process of saving partial\ incomplete code in source control without Check-In)

Not Supported by VSS

Supported by Team Foundation Version Control

NAnt script knowledge

Required

Not required

Table 2.3.1: Comparison between the tools CruiseControl.NET/NAnt and VSTS/TFS.

If you have small team size and require cost effective build and deployment option, then CruiseControl.NET and NAnt can be effective. The correct methodology of implementation of build and deployment process using NAnt scripts makes it reusable for the other projects with less effort.

The next section will guide you to create build and deployment process for the sample Web Application.

3 build and deployment process

This section provides information on the steps to be followed in order to create build and deployment process for a sample Web Application using CruiseControl.NET and NAnt.

clip_image002

3.1 Create a sample .NET Web Application

  1. In Visual Studio 2005, create a new web application “DemoBuildandDeployment” and provide the path c:\demo\demoBuildandDeployment
  2. Add the “DemoBuildDeployment” project to Visual Source Safe (VSS)

3.2 Create NAnt scripts for the build and deployment

NAnt script file has the XML command tags, which make it very easy and convenient to write. NAnt script file starts with the <project> tag where the project name can be defined as an attribute. This is a root node of a build script and each script has one <project> tag.

<project name="AutoBuild">

</project>

<project> can have multiple <target> tags. A target is a discrete piece of work that can consist of zero or more tasks to be executed. Targets can be dependent on each other. Targets behave like a “Method” in .NET code which means they can be called within the build file or from another build file.

<project name="AutoBuild">
<target name="NewBuild">
</target>
</project>
<nant> tag is used to call particular task of build file whereas <property> tag is used to provide an input parameter to the build file. As shown below example, target <NewBuild> of Execute.build file is called using <nant> tag and the “branch” and “source.path” are passed as input parameter to Excecute.build file using <property> tag.
<project name="AutoBuild">
<target name="NewBuild">
<echo>Create New Build and Deployment</echo>
<nant buildfile="Execute.build" target="NewBuild">
<properties>
<property name="branch" value="$/demo/"/>
<property name="source.path" value="C:\demo\"/>
</properties>
</nant>
</target>
</project>

As mentioned below, <vssget> tag is used to get the latest source code from Visual Source Safe (VSS).

<vssget
username=”<User ID of VSS>”
password="<Password of VSS>"
localpath="<Working root directory on local system>"
recursive="true"
replace="true"
writable="false"
removedeleted="true"
dbpath="<Full srcsafe.ini path>"
path="<VSS path>"/>

The Visual Studio 2005 has an executable ‘MSBuild.exe’ used to compile and build application. The <msbuild> tag is used to build the project.

<msbuild project = "<Project Path\solution name.sln">
<arg value="/property: Configuration=debug; outdir=bin\Debug\" />
<arg value="/t: Rebuild" />
</msbuild>

In order to simplify the build and deployment process of the sample Web Application, create four build scripts as mentioned below.

1. Main.build

This script would contain all the information of input parameters such as source code path, Visual Source Safe path, and deployment folder.

2. ApplicationBuild.build

This file would contain the commands to build the web application project in debug and release mode.

3. Deployment.build

This file would contain information to deploy the Web Application to virtual directory path of Web Application. (c:\Apps\Demo\DemoBuildandDeployment)

4. Execute.build

This file would be used to get the latest version of source code from VSS. It would call ApplicationBuild.build to build the latest source code and Deploy.build to deploy the project.

The logical flow of script execution is shown below.

clip_image002[6]

Figure 3.2.1: Logical flow of build scripts

Detail implementation of each script is explained as below:

1. Main.build:

Main.build script is responsible for initiating the build by setting the input parameters.

Input parameters:

branch: VSS root branch path

source.path: Working directory path for Web Application where source code is located.

destination.path: Path of folder where virtual directory is mapped for website.

clip_image002[8]

Figure 3.2.2: Main.build file

Execution of Main.build file will call “NewBuild” target of Execute.build file and

provides the input parameters.

NOTE:

<echo> tag is used to provide comments in log file or on command prompt

2. Execute.build

Execute.build script is responsible to call another script step by step. This script calls ApplicationBuild.build to build the web application then call deploy.build to deploy web application at destination location.

clip_image002[10]

Figure 3.2.3: Exceute.build file

NOTE:

The property {db} is set to the VSS path where the srcsafe.ini file is located.

3. ApplicationBuild.build

ApplicationBuild.build script is responsible for building the Web Application, in the real sense, using <msbuild> command in debug and release mode.

clip_image002[12]

Figure 3.2.4: ApplicationBuild.build file

<delete> tag is used to delete all the existing DLLs and other files residing in Debug and Release folder. In <msbuid> tag, argument “/property: Configuration” is used to define the mode of build. It can be set as either debug or release.

4. Deploy.build

Deploy.build script is responsible for deploying the DLLs and configuration files to destination folder.

clip_image002[14]

Figure 3.2.5: Deploy.build file

Once all scripts are ready, they would require configuring with CruiseControl.NET to execute. The next section 3.3 describes the configuration of scripts.

3.3 Configure NAnt scripts with CruiseControl.Net

CruiseControl.NET tool has a configuration file (ccnet.config) which can be configured to run the NAnt scripts. Multiple projects can be configured to execute using this file.

The ccnet.config file is located at C:\Program Files\CruiseControl.NET\server\ccnet.config.

Some of the important XML tages used in ccnet.config file are as mentioned below:

XML Tags

Description

<project>

Define the project. ccnet.config file can be configured for multiple project execution using this tag

<webURL>

Web URL of CruiseControl.NET

<BuildFile>

NAnt script files which require to call

<targetList>

Target to call from NAnt script

<baseDirectory>

Location of build scripts

<publishers>

Logging the executable error

<triggers>

Call the Nant script at defined schedule

Table 3.3.1: XML tages used in ccnet.config file

Below is the config file configured to run our build and deployment process.

<cruisecontrol>
<project name="Demo Build and Deployment project">
<workingDirectory>C:\Demo\</workingDirectory>
<webURL>http://localhost/ccnet/</webURL>
<tasks>
<nant>
<executable>C:\Program Files\nant\bin\nant.exe</executable>
<baseDirectory>C:\demo\Operations</baseDirectory>
<buildArgs>-v</buildArgs>
<nologo>false</nologo>
<buildFile>main.build</buildFile>
<targetList>
<target>NewBuild</target>
</targetList>
<buildTimeoutSeconds>12000</buildTimeoutSeconds>
</nant>
</tasks>
<publishers>
<xmllogger logDir="C:\Demo\Log\" />
</publishers>
<triggers>
<scheduleTrigger time="15:00" buildCondition="ForceBuild">
<scheduleTrigger time="20:00" buildCondition="ForceBuild">
        <weekDays>
<weekDay>Wednesday</weekDay>
        </weekDays>
      </scheduleTrigger>
</triggers>
</project>
</cruisecontrol>

3.4 Execute build and deploy

To execute the Build and Deployment project access, the CruiseControl.NET Web Dashboard through the web address http://localhost/ccnet/ViewFarmReport.aspx.

clip_image002[16]

Figure 4.4.1: Snapshot of CruiseControl.NET Web Dashboard before execution of the project

Click the “Force” button to start the build and deployment process and the “Activity” column status will change from “Sleeping” to “Building” which will revert back to “Sleeping” once the job is finished.

As shown in Figure 4.4.2 below, the status for “Last Build Status” column will change to “Success” once build is complete without any error. In order to view the log file of specific project, click on Project Name link which would provide the details of execution logs and in case of errors helps debugging.

clip_image002[18]

Figure 4.4.2: CruiseControl.NET Web Dashboard after executing the project

NOTE:

The automated build and deployment can also be executed using manual command prompt as below:

"C:\Program Files\NAnt\bin\NAnt.exe" -buildfile:Main.build -v NewBuild

4 Best Practices

This section provides the details of some of the best practices that should be followed for automated build and deployment using CruiseControl.NET and NAnt.

· Based on the deployment environment, the configurable file like “Web.config” should be created at run time. In order to achieve this, a console .NET application can be created to generate the Web.config file which can be called from NAnt script. This will avoid manual modification of Web.config file for the different environments (Like development, Integration, System Acceptance Test, Test for Production, Production, etc.).

· Create separate NAnt script for each build and deployment process so that Build folder (Containing DLLs, aspx, etc.) is created only once and that can be used for deployment of different environments using deployment scripts. This will avoid building project multiple times for the different environments.

· VSS files can be marked with a Label to mention the build version which will help track the file used in a particular version of build.

• In order to receive a notification for the successful completion of the build and

deployment process, one can add <email> section in ccnet.config for the project.

5 Conclusion

In this article we learned to create NAnt script, configure NAnt scripts to CruiseControl.NET and explore ccnet.config file. The automated build and deployment process of .NET application using a CruiseControl.NET and NAnt tools is cost effective, reusable, easily configurable and requires minimal or no additional infrastructure setup. To make the build and deployment reusable for different projects, NAnt scripts should be divided based on logical task.

The next step would be to advance the build and deployment implementation based on your project requirements and using suggested best practices in this article.

6 abbrEvations, Acronyms and Nomenclature

Terminology  and Description

ACI Automated Continuous Integration
IIS Internet Information Services
TFS Team Foundation Server
TFVC Team Foundation Version Control
VSS Visual Source Safe
VSTS Visual Studio Team System
XML Extensible Markup Language

NAnt script/ build script/ build file
An XML file consist of a set of instructions for the build of a .NET solution

7 Disclaimer

This paper should not be interpreted as an endorsement of any software products.

Post a Comment

Ivan B said...

Thank you so much for this very informative article. It really gave me a headstart on using NAnt scripts! :D

Mani said...

Thanks for the great artilce. really useful to me.

indiroma said...

Thanks for the sharing this website. it is very useful professional knowledge. Great idea you know about company background.
web application development