DotNetCurry Logo

Build Customization in Team Foundation Server (TFS) 2015

Posted by: Gouri Sohoni , on 9/23/2015, in Category Visual Studio, VSTS & TFS
Views: 16842
Abstract: Customize Build activities in TFS 2015. We will use PowerShell to add custom tasks.

In a previous article New Build Features in TFS 2015 and Visual Studio Online  we discussed various new features of Team Foundation Server 2015 Build. In this article, we will discuss how to customize Build activities. First we will add existing step to the build definition, and later create a PowerShell script and add it to build definition.

 

Team Foundation Server 2015 Build Process

Visual Studio 2010 along with Team Foundation Server 2010 introduced a major change in build workflow by making it XAML based. Let us compare this old architecture with the new one with Team Foundation Server 2015.

With the old architecture, there was a one-to-one relationship with project collection and Build Controller and similarly at least one build agent was required for one build controller. Thus if we wanted Build to be triggered for 2 collections within a Team Foundation Server, we needed 2 Build Controllers and at least 2 Build Agents. Following diagram shows this architecture :

tfs-build-previous

Figure 1: Build Architecture for previous TFS versions

We have a significant difference with the new build architecture in TFS 2015, where instead of build controller bound to the project collection, we have a  link between the Build Agent and Team Foundation Server

tfs-build-2015

Figure 2: Build architecture for TFS 2015

Build Process

The complete build process is as follows:

1. The build system is task based. Team Foundation Server sends tasks to agent. A list of these tasks is available at open source link.

2. The agent is chosen from pool based on capabilities. These capabilities can be from Operating System, .NET framework to the processor for agent. This comes in the form of name value pairs. There can be multiple agents.

system-capabilities

Figure 3: Capabilities for the Host

3. The agent in turn assigns environment variables with build information. The list of environment variables is at this link.

4. The agent performs various tasks

5. The agent sends logs back to TFS to show real time log. These logs are shown real time.

Team Foundation Server 2015 provides support to non-windows builds. There is no need to create complete build definition as templates are available. The customization is easy as we only have to add custom tasks.

Creating a Build Definition

1. Let us create a build definition which comprises of a single step of “Visual Studio Build”. The following figure shows the build definition

build-def-single-step

Figure 4: Simple Build Definition

2. Click on Add build step, select step from Build tab named “Publish Build Artifacts” and click on Add.

add-steps-build-def

Figure 5: Adding a step to Build Definition

Close the window for adding steps.

 

3. We have to configure the newly added step. We need to specify values for following arguments.

  • Copy Root: this specifies the root folder which contains the files you want to copy. If you keep it empty, it will copy from repository.
  • Contents: the files which you want to copy to the location. Entering **\bin will copy files in any folder named bin.
  • Artifact Name: name of the artifact
  • Artifact Type: this can have 2 options, server (TFS) and file share (use UNC path)

I have created a shared folder named BuildDrops where I want the binaries to be dropped.

configure-arguments-build-step

Figure 6: Configure arguments for Build Step

4. Save the build and Queue it. After successful completion of the build, we can select the Artifacts tab and find our artifact in it.

build-completion-artifact

Figure 7: Build artifact explored

5. Explore the DropLoc and find the binaries dropped at the specified location.

Now that we have seen how to add an existing step to the build definition, let us create a new task by creating a PowerShell script and add it as a step to the build definition.

I am going to create a simple PowerShell script file and add it to the source control. Later I will add a new step to build definition and point the location where the .ps1 file exists.

6. Create a PowerShell Script fie with some statements. I have created a basic file which displays some arguments passed and also displays environment variable.

param ( $User )
Write-Host 'This is coming from build task'
$message = [System.String]::Format(“Hello {0}!", $User) 
$BuildNo = [System.String]::Format(“Build no {0}!", $Env:BUILD_BUILDNUMBER)
$BuildName = [System.String]::Format(“Build no {0}!", $Env:Build_DefinitionName)
$BuildQueuedBy = [System.String]::Format(“Build no {0}!", $Env:Build_QueuedBy)
Write-Host $message
Write-Host $BuildNo
Write-Host $BuildName
Write-Host $BuildQueuedBy

7. Now we need to check in this .ps1 file into the source control.

check-in-powershell

Figure 8: Adding PowerShell Script file to Source Control

8. Let us add this script as a new task. Select Add Build step and select PowerShell script as type.

9. We need to specify script file name by browsing to the source control path and also the parameter which goes as argument needs to be given.

add-powershell-script

Figure 9: Script file name

10. Save the build with new task added in the PowerShell Script. Trigger the build and find the result after successful completion (triggering will not be specifically required if the trigger is set to Continuous Integration)

powsershell-artifact-after-build

Figure 10: Build Status

Conclusion

In this article we discussed how easy it is to add a custom task for Team Foundation Server 2015 Build. A new task can be a PowerShell script file, a json file with plugin or it can even be an icon we need to embed. We discussed how to use a PowerShell script in this article.

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on Google+
Further Reading - Articles You May Like!
Author
Gouri Sohoni is a Trainer and Consultant for over two decades. She specializes in Visual Studio - Application Lifecycle Management (ALM) and Team Foundation Server (TFS). She is a Microsoft MVP in VS ALM, MCSD (VS ALM) and has conducted several corporate trainings and consulting assignments. She has also created various products that extend the capability of Team Foundation Server.


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!