Deploying websites from Visual Studio to your Production or QA servers can be a hassle. You Publish, then FTP upload, then maybe Remote Desktop to the destination and copy the new files. Or you do what I really hate and rebuild your site, check-in to source control and get latest on your production server. I hate this. I have found the most elegant way to deploy a site is to use the Web Deployment Package method. Below are the details.
*Be sure to read all of this tutorial. The deployment package will delete all files on the destination server and, depending on how your site is configure, this could break your site.
Create Local Destination for Deployment Packages
You must decide where to create the deployment package. This is simply a folder on the local computer where the deployment package will be created.
ex: C:\My Projects\Builds\Example.com\
Create Deployment Package
You must now create the deployment package.
- In Visual Studio, right-click your website project name, and select "Publish...".
- Under the profile section, select the dropdown and select "<New Custom Profile...">. Name the new profile (ex: Web Deployment Package).
- Select "Web Deployment Package" from the publish method drop down. Fill out the deployment details specifying the deployment folder and .zip filename (ex: C:\My Projects\Builds\Example.com\Example.com.zip), and the name of the site on the destination IIS server (ex: www - example.com). To figure out the name of the site on the destination server, go to the destination server, open IIS and view the sites.
- Click Next and complete the deployment wizard.
Create a Deployment .bat File
This step will create 2 files that will 1) do a trial deployment (no files will be altered but you will see what files would be during a real install) and 2) make a real deployment of the the new site.
- Within your deployment folder (ex: C:\My Projects\Builds\Example.com\), create a file named TestDeploy.bat.
- Right-click the file and click Edit.
- Enter the following line in the file with your data. You will have to include your deployment package name, the destination server url, the destination server username, and the destination server password. <br/ >
Example.com.deploy.cmd /T /M:http://www.example.com/MSDeployAgentService /U:ServerUsername /P:ServerPassword
- Within your deployment folder, create a second file named ProdDeploy.bat.
- Edit the file and enter the following line with your data <br/ >
Example.com.deploy.cmd /Y /M:http://www.example.com/MSDeployAgentService /U:ServerUsername /P:ServerPassword
Create Command Line Shortcut
To simplify deployment, create a shortcut in your deployment folder (ex: C:\My Projects\Builds\Example.com\) to the command line. You will set the shortcut to always open the command line in the current folder.
- Right-click in the deployment package folder, select New and then select Shortcut.
- Enter the location of the command line exe
- In Windows Explorer, copy the path to the deployment package folder (ex: C:\My Projects\Builds\Example.com\).
- Right-click the new command line shortcut, select Properties and paste the deployment folder path into the "Start in:" field.
Install the Web Deployment Software on the Destination Server
You must install the Web Deployment software on the destination server. To do so, open/install Web Platform Installer and install Web Deploy 3.5 for Hosting Servers (or most recent version).
Optional: Ignore Files
It is important to know that when installing a web deployment package, the installer will delete all files in the server's destination directory unless you tell it otherwise. If you have a config file (web.config, appSettings.config, etc) that you usually do not deploy but manually edit, this file will be deleted. If you have an upload folder for uploaded images, this folder will be delete (this folder should be moved outside of the web site folder and setup as a virtual directory in IIS, in my opinion). Fortunately, you can tell the deployment package which files/folders you do not want to be deleted. In order to do so, you must create a wpp.targets file within your Visual Studio project. In Visual Studio, right-click your project and add a new config file in the root of your site (name it test, you will need to rename it). Rename the new file the same name as your web project with the extension wpp.targets (ex: Example.com.wpp.targets). You will use this file to tell the deployment package which files to ignore when deploying. Below is an example wpp.targets file. For more information, click here.
AddCustomSkipRules Delete filePath .*ConnectionStrings.config Delete filePath .*AppSettings.config
Run the Deployment Package
You should now be able to install the deployment package. Double click the command shortcut. The command line's current directory should be the deployment folder. Type TestDeploy.bat and hit Enter (or type T and hit Tab and hit Enter). If everything is working, you should see the results of the test deploy with no errors. If you see no errors, carefully review the files that are going to be deleted/modified. You do not want to mistakenly delete any files. If you receive an error, you may need to update a firewall setting or ensure that the deployment service on the server is running. You can check this by Remote Deskoping into your server, going into the Services tool on the server and ensuring that the "Web Deployment Agent Service" is running. Continue to check using the TestDeploy.bat until its working and you are sure all of your files are safe. Once you are ready to make a production deploy, within the command line, type ProdDeploy.bat (or type P and Tab) and hit Enter. If everything worked, your site should now be deployed.