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 -->
<PropertyGroup>
<WebSiteDestDir>
C:\Inetpub\wwwroot_mywebsite
</WebSiteDestDir>
</PropertyGroup>

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

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

<!-- Create application pool -->
<Target Name="CreateAppPool" >
<Message Text="Creating my AppPool" />
<Web.AppPool.Create AppPoolName="myAppPool"
WorkerProcesses="1"
IdentityType="NetworkService"
ContinueOnError="false"/>
</Target>

<!-- Create website -->
<Target Name="CreateWebSite" DependsOnTargets="CreateAppPool">
<Message Text="Creating WebSite" />
<Web.WebSite.Create Description="myWebapp"
Path="$(WebSiteDestDir)"
HostName="myWebapp"
AppPoolId="myAppPool"
ContinueOnError="false" />
</Target>

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

3 comments:

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 Reddy 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 Gray 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.