Thursday, 19 February 2009

Automatic webapp to IIS deployment using MsBuild

Sometimes, when buidling/deploying your projects standard MsBuild tasks are not enough. I've found a very handy SDC tasks library which allows you to do much more from your build script.

The following example demonstrates creation of application pool and website using this AppPool on IIS. Normally this would need to be done manually. Thanks to SDC we can automate the whole process to eliminate possible errors that can occure during manual deployment.

<!-- Use SDC tasks -->
<UsingTask AssemblyFile="Microsoft.Sdc.Tasks.dll" />

<!-- Define website folder -->

<!-- Main task -->
<Target Name="Deploy_Webapp">
<CallTarget Targets="CleanIIS" />
<CallTarget Targets="CreateWebSite" />
<CallTarget Targets="CopyContent" />

<!-- Clean IIS: remove existing AppPool and Website -->
<Target Name="CleanIIS">
<Web.WebSite.Delete Description="myWebapp" />
<Web.AppPool.Delete AppPoolName="myAppPool" />

<!-- Create application pool -->
<Target Name="CreateAppPool" >
<Message Text="Creating my AppPool" />
<Web.AppPool.Create AppPoolName="myAppPool"

<!-- Create website -->
<Target Name="CreateWebSite" DependsOnTargets="CreateAppPool">
<Message Text="Creating WebSite" />
<Web.WebSite.Create Description="myWebapp"
ContinueOnError="false" />

<Target Name="CopyContent" DependsOnTargets="CreateWebSite">
<!-- Copy the app content to WebSiteDestDir here -->
Using this library you can also create ActiveDirectory user, virtual directories and a whole lot more!


Anonymous said...

Web.WebSite.Delete should be Web.WebSite.DeleteWebSite

Instead of the Using clause, you should have

<Import Project="$(Tools)\Microsoft.Sdc.Common.tasks"/>

Praveen Kumar said...

can we create IIS wibsite in aremote machine, if so what are all we have to consider. like user credentials....please provide code sample..

Andrew said...

SDC Web.AppPool.Delete and Web.AppPool.AppPoolDelete do not work.

I had to go download MSBuild Extension Pack and use:

which does work.