TFS Tutorial: TFS for Automating Build, Test, and Deployment for .NET Projects

Using Microsoft TFS 2015 Update-3 for .NET (Build, Test and Deploy): TFS Tutorial

TFS is more widely used for .NET development using Visual Studio .NET IDE. With TFS 2015 Update 3, one can connect to any Team Foundation Server Git repo, using an SSH key.

Team Foundation Server (TFS) is an ALM product from Microsoft which provides the capabilities for an end-to-end development and testing using Work Item Management, Project Planning (Waterfall or Scrum), Version Control, Build/Release (Deploy) and Testing capabilities.

NOTE: This TFS tutorial has many images so allow it to load properly. 

Also read => TFS for JAVA Projects with Eclipse in DevOps

Using Microsoft TFS 2015 Update 3 for .NET (Build, Test and Deploy)

Introduction

TFS is tailored for Microsoft Visual Studio and Eclipse on all platforms, however, it can also be used as a back-end to several IDEs (Integrated Development Environments).

We will now take a look at how Team Foundation Server (TFS) will be used to Build, Test and Deploy .NET Web Applications which is traditionally the strength of the tool.

Prerequisite:

Typically to perform the steps mentioned in the tutorial you will need a Build Server, where Builds will be performed, and Deployment machines or environments where, applications will be deployed to IIS, with agents installed and running. Please refer to my earlier tutorial to know how to install agents.

Setup a C# Application

Assuming TASK work items are created in TFS and is assigned to developer’s to work on the same. I have always noticed that Traceability is very important from the point of view of tracking any work across the software lifecycle.

Before adding a .NET application to TFS source control repository, ensure whether a Collection and Team Project exists or not.

A Collection is created by the TFS Administrator. It consists of a group of Team Projects in any service organization, where projects for multiple customers are being executed. You can create individual collections for each customer projects in TFS.

Once a collection is created you can create multiple team projects within it. A single team project consists of all work items, source code, test artifacts, metrics for reports etc., Team project can be created using various inbuilt process templates like Scrum, Agile, CMMI etc.

1 Setup a C# Application

2 create team project

3 Setup a C# Application-select template

4 Setup a C# Application-Specify source control

5Setup a C# Application- team project setting

6 Setup a C# Application- Team project creation

7 Setup a C# Application-new team project created.1

Launch TFS Web interface using the URL http://<ServerName>:port/tfs and you can see the project created.

8. Projects and teams

 

Click on the project and you will get on to the Team Dashboard

(Note: Click on any image for enlarged view)

12. My TFS project.1

Now we have a collection and a team project created. Let’s launch Visual Studio.NET and create a new C# Web application and share the project to TFS source control repository. This is the first step towards establishing Continuous Integration (CI) practice.

1) Launch Visual Studio.NET and set TFS as the default source control repository. Go to Tools => Options => Source Control. Then click OK.

Launch Visual Studio.NET

2) Go to View => Team Explorer and connect to TFS server using the icon16connect to TFS server

17View- Team Explorer

3) Create a C# ASP.NET Web project

19Create a C# ASP.NET Web project

21 Create a C# ASP.NET Web project

4) Since we are creating a web application, Select the Web Forms template

23 Select the Web Forms template

Click OK to create the project.

5) The project created can be viewed in Solution Explorer. .NET uses the concept of .sln file or solution to contain all the projects. Once you open the solution all the associated projects will also open. We need to add the solution to the TFS source control repository

25 project created can be viewed in Solution Explorer

 

6) Modify the file Default.aspx as shown, Save it and then add the whole solution to the TFS source control repository

27file Default.aspx

 

Select the Design view and you will be able to see the entire page

29 Design view

 

7) Add the solution to TFS source control. Right click on the solution and select ‘Add solution to Source Control’

31Add solution to Source Control

 

8) Select the Team Project created earlier and then click OK

33 Select the Team Project

 

9) The solution is not yet checked-in to the TFS. In the Team Explorer click on the source control explorer and you can see the solution added to be checked in.

35source control explorer

 

10) Check-in changes. Go to Team Explorer => Pending Changes

Enter a comment and drag-drop a TASK work item to ensure traceability. Click on the Check-in button.

37 Check-in

39 Pending Changes

41drag-drop a TASK work item to ensure traceability

11) To test the website running locally, Click on the Firefox icon in Visual Studio.NET. Remember it is not yet deployed to IIS on any particular environment.

43 Click on the Firefox icon in Visual Studio.NET

website

Creating Build Definition with Code Analysis

A build definition consists of a series of Tasks which is executed during an automated build process. Examples of the tasks can consist of running a Visual Studio Build, MS Build, executing PowerShell or Shell scripts etc.

1) To create a Build Definition, login to TFS web interface and go to the Builds TAB. Click on + to create a build definition. Start with EMPTY definition and then click Next.

47 website running locally

Select the Team Project and click on Create

49create a Build

 

Click on Edit, which is found next to the Empty definition

51Click on Edit

 

Save the build definition as something like ‘Main Build’

53Save the build definition

 

Since Sonarqube will be used for Code analysis, hence add the 2 Sonar steps ‘SonarQube Scanner for MSBuild – Begin Analysis’ and the ‘SonarQube Scanner for MSBuild – End Analysis’ tasks.

Add the Begin Analysis step before any MS Build or Visual Studio Build. This step fetches details from Sonarqube server to configure the analysis.

Add End Analysis step later on.

55Add End Analysis step

 

The steps added will look like the following with MS Build step in between.

Start to define the details of Sonarqube server. Define Endpoint where the Sonarqube server and authentication details are added. Click on ‘Manage’ to add the Sonarqube server details.

Click on ‘New Service Endpoint => Generic’

57 New Service Endpoint-Generic

59 Generic

Now go back to the main Build Definition screen and select the endpoint which was just created.

Completed configuration for Begin analysis, looks as shown below

61 select the endpoint

 

Select the solution. In the Advanced => Additional Settings enter the following and save the Build Definition

/d:sonar.scm.enabled=true /d:sonar.scm.provider=tfvc /d:sonar.tfvc.username=niranjan /d:sonar.tfvc.password.secured=<password>

63save the Build Definition

 

SonarQube – End Analysis. Finish the analysis and then upload the results to the SonarQube project.

65SonarQube – End Analysis

 

Add a step to Publish Artifacts to the server. The artifacts will be stored in a drop folder in the server and will be used during deployment.

67 Publish Artifacts to the server

 

2) Install the agent on the Build and Deployment machine. You can refer to my previous tutorial to know how to install the agent. Now assuming that the agent is installed, ensure whether the agent is running or not.

69 Install the agent on the Build and Deployment machine

 

3) Ensure the SonarQube SCM TFVC plugin is downloaded from here. and copied to the SonarQube installation\extensions\plugins directory. This plugin ensures that the source code is taken from the TFS source control repository and is made available to SonarQube for code analysis.

71 SonarQube SCM TFVC plugin is downloaded

 

4) After the plugin is downloaded and copied, Launch the sonar server

73 Launch the sonar server

5) Initiate a Build to check if the steps work fine. Open the Build Definition and click on ‘Queue Build’

75Queue Build

 

Build Successful. All the steps ran fine.

77Build Successful. All the steps ran fine.

 

Click on the Build number, in this case, it is Build 217 and go to Artifacts tab to look at the drop folder created at the server level.

79 Build 217-Artifacts tab

 

Note: In the next section the release process shows how any of changes can be reflected throughout the deployment process. For this ensure that the project artifacts are copied through the COPY step in the build definition after compilation step or manually copy the project artifact directory to the C:\inetpub\wwwroot directory. This has to be done only once.

81wwwroot directory

 

Creating Release for Deployment

In the previous section, we saw about Build, followed by code analysis using SonarQube. We will now create a Release to deploy the artifacts from the ‘drop’ folder to IIS.

With the creation of Release, the entire Continuous Integration and Continuous Delivery is automated without any manual intervention.

Go to Release hub and Create a Release Definition.

83 Release hub and Create a Release Definition

 

Start with Empty definition and click OK.

85 Empty definition

 

Save the Release definition and rename the Default Environment to QA. Based on the projects, additional environments like Staging Pre-Prod etc. can also be added and deployment would be automated to the entire environments one after the other.

87 Save the Release definition

 

Link the Build definition to Release definition so as the deployment is automated. Click on ‘Link to a build definition’. Select the build definition created earlier.

89 Link to a build definition

 

Click on Link

Enable the Deployment Condition to initiate the deployment immediately after Release creation

91 Enable the Deployment Condition

93 initiate the deployment

Also, enable the Trigger for deployment after the build is successful. In the Release definition, go to the Trigger tab and enable ‘Continuous Deployment’, select the build definition.

Later Save the Release Definition.

95 enable the Trigger for deployment

 

Back in Environments tab of the release definition add the tasks to deploy the artifacts to the IIS server.

Add a task to copy files from ‘drop’ folder created during the build process to IIS wwwrootdirectory.

97Add a task to copy files from ‘drop’ folder

99 IIS wwwrootdirectory

Source folder – Browse and select the Webapplication1 project in the drop folder

101select the Webapplication1

 

Target folder should be the inetpub\wwwroot directory – C:\inetpub\wwwroot\WebApplication1

Executing Release for Deployment

In the release hub, create a release to start the deployment

103 create a release to start the deployment

 

Select the last stable build and Click on Create to Start the Deployment.

105last stable build

 

Deployment is successful to QA environment

107Deployment is successful to QA environment

 

Run inetmgr which is the IIS manager, where you can manage all the web sites/applications installed to IIS. Browse to the web application deployed.

109Browse to the web application deployed

111the web application deployed

 

 

To conclude once you initiate the Build, the deployment will also get completed to all the environments defined, as the Release is linked to the build definition.

Conclusion

In this TFS tutorial, we have now seen how Microsoft ALM platform can be used for automating Build, Test, and Deployment for .NET applications. TFS plays a major role here.

Hence in today's world, AUTOMATION is the key for successful and faster delivery to stay ahead.

Recommended Reading

6 thoughts on “TFS Tutorial: TFS for Automating Build, Test, and Deployment for .NET Projects”

  1. I am beginner to .Net Framework. I am obliged for this informative post, I have got many new points and has cleared my doubts. It also helped me understand this topic in more detailed way.

    Reply
  2. Under the section: Setup A C# Application
    I get a 404 page for the link: “More on creating collections can be found @ Manage team project collections in Team Foundation Server”

    In Creating Build Definition With Code Analysis
    Your reference: “2) Install the agent on the Build and Deployment machine. You can refer to my previous tutorial…”
    A link would be good there, I cannot immediately find that tutorial.

    Reply
  3. How to Use TFS build automation for other than .NET applications Ie Java, Python etc..and also how do we integrate TFS deploying build in Azure instance?

    Reply
  4. Hello Author,
    this blog was very useful, it would be great help if you post how to Build and release to a database and if possible how to do when we have multiple databases in an instance.

    Reply
  5. If you are achieving the CI/CD process using TFS Builds and release, why do we need AZURE Develops which should have paid subscriptions ?

    Reply

Leave a Comment