Tuesday, March 9, 2010

How to make automatic deployment for .NET web application

How to make automatic deployment for .NET web application

Here I am going to define the way to give automatic deployment for any .NET application. The .NET application should use the source control (EX: SVN, VSS, TFS etc). In my given example I am defining about SVN as source control if you require any other details. Please feel free to drop me a message. The below mentioned details will handle for code automatic deployment, I am working on database auto deployment, will be posting this as soon as possible……………………….

What this post will tell you.

  1. Taking latest code from SVN source control dynamically.
  2. Keeping the entire file into one directory. (User will define through config file)
  3. Building the .NET application using MSBUild.exe
  4. If error/success then send the email.
  5. If error then stop the process.
  6. If success then enjoy (If any issue feel free to buzz me...)
  7. Bit more steps :
    1. Run NANT tasks- Stop IIS
    2. Copy the reuired file for deployment from previous define folder to deployment folder (Step2 folder to deployment folder)
    3. Change the webconfig file connection string or other as per your requirement can define into nant config file.
    4. Start the IIS

Prerequisite:

  1. .NET web application
  2. SVN as source control

Require software:

  1. CCNET – Download from: http://sourceforge.net/projects/ccnet/
  2. CollabNET – Download from: http://www.open.collab.net/downloads/subversion/
  3. NANT – Download from: http://sourceforge.net/projects/nant/files/nant/0.85/

Steps to follow:

  1. Download the CCNET from above mentioned location.
  2. Go to path: http://localhost/ccnet it will open the locally installed ccnet application. Which will look like:
  3. Go to path of CCNET: your drive path (ex: D:\Program Files\CruiseControl.NET\server)
  4. Open the file named as “ccnet.config” and configure the config file as below mentioned steps: Copy the below mentioned project in config file between the

<cruisecontrol xmlns:cb="urn:ccnet.config.builder">

Copy the below mentioned project tab here and follow the steps which are written as comment inside the file.

</cruisecontrol>

<project name="Your Project Name" queue="Q1" queuePriority="1">

<labeller type="defaultlabeller">

<prefix>Your Project Name</prefix>

<incrementOnFailure>false</incrementOnFailure>

<labelFormat>00000</labelFormat>

</labeller>

<!-- in below path the CCNET will keep the log files.-->

<artifactDirectory>c:\Artifacts</artifactDirectory>

<webURL>http://localhost/ccnet/server/local/project/Concep/ViewProjectReport.aspx</webURL>

<modificationDelaySeconds>10</modificationDelaySeconds>

<!-- If you want to schedule the deployment rather then force build -->

<triggers>

<scheduleTrigger time="12:15" buildCondition="ForceBuild" name="Scheduled">

</scheduleTrigger>

</triggers>

<!-- If you want to send the email on build failure or success build -->

<publishers>

<statistics />

<xmllogger />

<email mailport="25" includeDetails="TRUE" mailhostUsername="mail host user name" mailhostPassword="mail host user password" useSSL="FALSE">

<from>build@build.com</from>

<mailhost>mail host address/mailhost>

<users>

<user name="JoeDeveloper" group="developers" address="x1@x1.com" />

</users>

<groups>

<group name="developers">

<notifications>

<notificationType>Failed</notificationType>

<notificationType>Fixed</notificationType>

</notifications>

</group>

</email>

</publishers>

<!-- Configure the SVN details for the force build force build -->

<sourcecontrol type="svn">

<!-- Define the path of collabnet sv.exe file for takeing latest from SVN -->

<executable>D:\Program Files\CollabNet\Subversion Server\svn.exe</executable>

<!-- Define the SVN URL -->

<trunkUrl>Give SVN URL</trunkUrl>

<!-- Create the working directory and gibve path, where it will take the code form SVN and keep it. -->

<workingDirectory>C:\Projects</workingDirectory>

<!-- SVN USERNAME AND PASSWORD-->

<username>XXX</username>

<password>XXX</password>

</sourcecontrol>

<!-- Create defferent task which need to perform after taking the latest from SVN -->

<tasks>

<!-- Changed from MSbuild task to devenv task since vsts test cases did not run properly. -->

<msbuild>

<!-- Path of MSBuild.exe to build the project-->

<executable>C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe</executable>

<!-- Path of that project which you want to build -->

<workingDirectory>C:\Projects</workingDirectory>

<!-- Name of the solution project file which need to be build -->

<projectFile>project.sln</projectFile>

<timeout>600</timeout>

<!-- Will be default after installing ccnet -->

<logger>d:\Program Files\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll</logger>

</msbuild>

</tasks>

</project>

Configure the file as per the requirement and follow the below mentioned steps:

  1. Run the CCNET.exe file
  2. Then open the URL: http://localhost/ccnet
  3. You will see the name of project which you defined will be listed there.
  4. There will be one button called force build – click that button
  5. Once build completed the ccnet site will show you the build is success or not. And will show the exception and warnings.
  6. As defined in config file – you can go to path – c:\Artifacts to view the details error log.

Now once it succeed and if you want to maintain the deployment folder then please paste the below mentioned tag inside the <task></task> tag of above mentioned config file.

<nant>

<!—Download NANT from above mentioned URL and copied in your system then define the location of NANT.exe below -->

<executable>D:\Program Files\nant-0.85\bin\NAnt.exe</executable>

<!—Define the working directory path where you took latest from svn and built-->

<baseDirectory>C:/Projects</baseDirectory>

<!—copy the below mentioned file into base directory of your project -->

<buildFile>default.build</buildFile>

<targetList>

<!-- set the target in default.build file and give the name here -->

<target>extractWebSite</target>

</targetList>

</nant>

Default.build file:

<?xml version="1.0"?>

<project name="your project name" default="install">

<target name

<target name="extractWebSite">

<property name="InstallFolder" value="Path of deployment folder"/>

<property name="Name" value="Varun"/>

<!-- Stop the IIS -->

<exec verbose="true" program="iisreset">

<arg line="/stop"/>

</exec>

<!-- Delete the file from deployment folder -->

<delete >

<fileset basedir="${InstallFolder}">

<exclude name="Module_Bain/GenerateReport/*.*"/>

<exclude name="Module_Bain/RegisterReport/*.*"/>

<include name="**/*.*"/>

</fileset>

</delete>

<!-- Copy the files from build folder to deplyment folder -->

<copy todir="${InstallFolder}">

<fileset>

<include name="FolderName/*.aspx" />

</fileset>

</copy>

<!-- Show message -->

<echo message="Extracting web site done"/>

<!-- rewrite the some netry in web.config file -->

<xmlpoke

file="${InstallFolder}\Application\Web.config"

xpath="/configuration/appSettings/add[@key = 'LogoImageFolderPath']/@value"

value="${Name}" />

<echo message="Updating web.config... done"/>

<!-- Start the IIS -->

<exec verbose="true" program="iisreset">

<arg line="/start"/>

</exec>

</target>

</project>

It will create the deployment folder. Using this NANT script we can also make sure the web.config entries which are required to be same of web.config. for ex: connection string or any folder path.

Please update your comments and feel free to contact for any clarifications.

No comments:

Post a Comment