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 :
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
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.
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
Figure 4: Simple Build Definition
2. Click on Add build step, select step from Build tab named “Publish Build Artifacts” and click on Add.
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.
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.
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.
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.
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)
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.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Gouri is a Trainer and Consultant on Azure DevOps and Azure Development. She has an experience of three decades in software training and consulting. She is a graduate from Pune University and PGDCA from Pune University. Gouri is a Microsoft Most Valuable Professional (MVP) - Developer Technologies (Azure DevOps), Microsoft Certified Trainer (MCT) and a Microsoft Certified Azure DevOps Engineer Expert. She has conducted over 150 corporate trainings on various Microsoft technologies. She is a speaker with Pune User Group and has conducted sessions on Azure DevOps, SQL Server Business Intelligence and Mobile Application Development. Gouri has written more than 75 articles on Azure DevOps, TFS, SQL Server Business Intelligence and SQL Azure which are published on
www.sqlservercurry.com and
www.dotnetcurry.com. You can connect with her on
LinkedIn.