Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop IIS 7 Application Pool from build script

How can I stop and then restart an IIS 7 application pool from an MSBuild script running inside TeamCity. I want to deploy our nightly builds to an IIS server for out testers to view.

I have tried using appcmd like so:

appcmd stop apppool /apppool.name:MYAPP-POOL

... but I have run into elevation issues in Windows 2008 that so far have stopped me from being able to run that command from my TeamCity build process because Windows 2008 requires elevation in order to run appcmd.

If I do not stop the application pool before I copy my files to the web server my MSBuild script is unable to copy the files to the server.

Has anybody else seen and solved this issue when deploying web sites to IIS from TeamCity?

like image 588
Andrew Hanson Avatar asked Sep 08 '09 18:09

Andrew Hanson


2 Answers

This article describes using an htm file named App_offline.htm to take a site offline. Once the IIS detectes this file in the root of a web application directory,

ASP.NET 2.0 will shut-down the application, unload the application domain from the server, and stop processing any new incoming requests for that application.

In App_offline-htm, you can put a user-friendly message indicating that the site is currently under maintainance.

Jason Lee shows the MSDeploy calls you need to use (plus much more about integrating these steps in your build scripts!).

MSDeploy 
-verb:sync 
-source:contentPath="[absolute_path]App_offline-Template.htm" 
-dest:contentPath="name_of_site/App_offline.htm",computerName="copmuter_name",
username=user_with_administrative priviliges,password=passwort

After deployment you can remove the App_offline.htm file using the following call:

MSDeploy 
-verb:delete 
-dest:contentPath="name_of_site/App_offline.htm",computerName="computer_name",
username=user_with_administrative_priviliges,password=passwort
like image 160
Nasser Avatar answered Oct 18 '22 18:10

Nasser


The msbuild community tasks includes an AppPoolController that appears to do what you want (though as noted it is dated and at present only supports IIS6.) An example:

<AppPoolController ApplicationPoolName="MyAppPool" Action="Restart" />

Note that you can also provide a username and password if necessary.

Edit: Just noticed that the MSBuild Extension Pack has an Iis7AppPool task that is probably more appropriate.

like image 7
Pedro Avatar answered Oct 18 '22 18:10

Pedro