Developer Logs

Every once in awhile I hit a (technical) wall, stumble upon a great tool or look for a reason to increase my English.
This is my place to share, welcome to my logs.

How to Publish multiple Web Projects in one Solution with TFS Build

There are many ways to handle multiple (web) projects in a solution and deploy them on build. I've seen many examples and all examples required loads of configuration. I believe this is the easiest way, for this to work you need Team Foundation Server as your Version Control software.

The Current Situation

The Solution contains a total three projects. Example:

.- DemoSolution
|
|-- Webshop (Web Project, MVC)
|
|-- DAL (Class Library)
|
|-- API (Web Project, MVC)

On build we want to deploy (web publish) the Webshop and the Api to our development server. Even better, we setup Continues Integration for this solution and deploy our web projects to the development server on TFS checkin.

The problem

At the moment I was using a Build Definition that contained all the MSBuild Arguments to publish to a single IIS Website. As explained in this article (thanks Kunal, for writing this excellent article), but we can't publish every Web Project to the same IIS website. This will result in the last Web Project being published, overwriting our Webshop with in our case the API.

Publish Profiles and Web Deploy

Visual Studio 2012 introduced Publish Profiles this is the first step in solving our problem. But before we can create a Publish Profile we need to make sure that our server is configurated to support Web Deployments, here is great guide to achieve just that!

Creating a Publish Profile in Visual Studio 2015 (and 2013/2012)

In the Solution Explorer file tree select the first Web Project that you want to Deploy on Build, in my case Webshop. Now rightclick and click on Publish.... A wizard is presented, follow these steps:

  1. Click on the pulldown and select the option < New Custom Profile >.
  2. Enter Development as the name of the Publish Profile (**this is an important step**) and click OK.
  3. Choose Web Deploy as the Publish Method.
  4. Enter the Server Url (example: microsoft.com)
  5. Enter the Site name, this is the name of the website in IIS. (Example: microsoftwebsite or if using a virtual directory or application: microsoftwebsite/webshop)
  6. Enter the username and password (this should be an account that has Publish rights on the server).
  7. Optional, enter a destionation url (will be opened automaticly after publish).
  8. Click on Validate, when everything is green continue and click Next.
  9. Select the desired configuration, I used Release.
  10. Click Close, and click Yes to Save Changes.

Example of Publish Profile in Visual Studio

Do this for all Web Projects that you want to Deploy on build, make sure you enter Development for each profile name..

Creating a Continues Integration Build Definition

Click on the Team Explorer tab in the Solution Explorer pane or through View -> Team Explorer in the Visual Studio ribbon. Now select the option Builds (can't find Build, use the home icon at the top of the Team Explorer pane). Then click the blue link Create New Build Definition. Give the Build Definition a logical name, something like 'Continues Integration' and follow these steps:

  1. Select the Trigger section and check Continues Integration - Build each check-in.
  2. Select the Source Settings section and setup the paths as desired.
  3. Select the Build Default section and setup the options as desired, I used my default Build Controller and checked This build does not copy output files to a drop folder.
  4. Select the Process section, this is the important part.

Build Definition - MSBuild Arguments

With the Process section open, expand section 2. Build and then expand section 5. Advanced. Enter the following in as the MSBuild Arguments (as one long line, arguments devided by spaces):

/p:DeployOnBuild=True
/p:PublishProfile=Development
/p:AllowUntrustedCertificate=True
/p:Password=password-of-your-deploy-user-as-earlier-entered-with-publish-profile

Now save the Build Definition (CTRL + S or File -> Save).

_Explanation of the arguments:_
/p:DeployOnBuild: Tells MSBuild that the (Web)Project needs to be deployed on Build.
/p:PublishProfile: Tells MSBuild the name of the Publish Profile to look for in each (Web)Project.
/p:AllowUntrustedCertificate: Tells MSBuild that whenever the destionation server does not have a valid certificate to continue deployment. (Can be set to False)
/p:Password: Tells MSBuild the password to associate with the Publish Profile. I know you entered this allready in the Publish Profile, but Visual Studio will encrypt, MSBuild can't use this, more information here.

Example of MSBuild Arguments in Visual Studio

Testing and Running your first Build with Automated Deployment

This should cover it, when you make a change to your project and do a checkin the Continues Integration Build should kick in and start building. After that the build will be deployed / published by the rules setup in the Publish Profile.

Debugging errors

If, for some reason you encounter errors you can manually try to use the Publish Profile. Rightclick on a Web Project and choose Publish..., in the dialong click on Publish again. This should give you some extra pointers.

Still no luck? Check the build logs or comment on this article below.

Tips and Tricks

The Profile Name binds everything together, you could add another Publish Profile to each project with the name Staging and Another with Production. You can now setup more Build Definition to leverage these profiles. I leave that to you.

If you still run into some troubles or have some questions feel free to comment or e-mail me.

Picture of Johan Dorper
I'm Johan a .NET Developer (prefer C#) / ScrumMaster from Nunspeet, The Netherlands.
Feel free to mention me on Twitter or drop an e-mail.
comments powered by Disqus