Custom Build Task in Visual Studio Team Services (VSTS)

Posted by: Gouri Sohoni , on 2/18/2018, in Category VSTS & TFS (Azure DevOps)
Views: 22252
Abstract: Create a custom build task in Visual Studio Team Services (VSTS) which can be used for build or release definitions. Also learn how to package and publish the custom build task.

Microsoft’s Visual Studio Team Services (VSTS) is a cloud-based service. It provides many features like SCM (Source Control Management), Work Item Tracking, Build and Release Management and Test Management.

VSTS makes software development relatively easy for a team with all these features and services.

Microsoft provides a lot of build, release and test related tasks in VSTS. Most of the times, these tasks fulfil our needs while creating a new Build Definition. Over and above these tasks, there are many more tasks available in the Marketplace, which can be installed for your VSTS account.

But in some situations, we may need to use a task which is not available!

In this case, we can create a Custom Build Task. We may require this when we are using a third-party testing tool for which we want to capture the result and display it in build or release. We may also need a custom build task if we want to frequently execute PowerShell scripts as a part of build definition. The task once created can be used in either Build or Release Definition.

Creating Custom Build Task in VSTS

In this article, I will discuss how to create a custom build task and how to package and publish it. Let us divide the users in two categories - one who are the designers of the task like us, and others who are going to use it. The designers will know the internals of the task e.g. whether the task is encapsulating a PowerShell script, or a batch file or a shell script. The second type of users will not be exposed to all these intricacies, they will just give the parameter(s) and the task will get executed.

I had created a custom build task some time back for a client who was using a third-party testing software and they wanted the results to be available in VSTS, as the build was being done using VSTS. The process of creating custom build task has changed a bit since then. Now it is very near to creating an extension for VSTS and is also shown in extension tab for VSTS account.

In this article, I will encapsulate a PowerShell script in the task which in turn will take parameters as jar files and execute Selenium Test written in Java.

Pre-requisites for Custom Build Task

1. VSTS Account (if you do not have a one, it can be created from this url)

2. Editor to write code (it can be done in Visual Studio or even Visual Studio Code)

3. Latest version of node.js

4. TFS Cross Platform Command Line Interface to package the extension

5. A home directory for your project

Once the task is created and published, it will start appearing in the same list where all Microsoft’s tasks are listed.

Let us have a look at how Home directory looks in this case:

home-dir

Let us see a step-by-step walkthrough for the same.

Step 1. Create directory structure as shown above.

Step 2. Create and add respective .png files to the folders. The icon.png file must exist in the same folder as task.json and must be of the size 32 X 32 pixels. Any change in file name or size will not show the icon for the custom task.

Step 3. I have created a PowerShell script which will execute the Selenium Test written in Java and send test results to a file.

Create metadata file for the task

Step 4. Take a look at the task.json file:

{ "id": "926b06f1-c250-4123-9e76-82a33c81b806",
  "name": "SeleniumExecution",
  "friendlyName": "Selenium Test Execution",
  "description": "Execute Selenium Test",
  "author": "SSGSIT",
  "helpMarkDown": "Execute Selenium Test using input parameters",
  "category": "Test",
  "visibility": [ "Build", "Release" ],
  "demands": [],
  "version": {"Major": "1","Minor": "1","Patch": "5"},
  "minimumAgentVersion": "1.95.0",
  "instanceNameFormat": "Selenium Test Execution",
  "inputs": [{"name": "JarFiles",
      "type": "string",
      "label": "Selenium Test",
      "defaultValue": "-cp ;c:\\TestJar\\Validator.jar;c:\\junit\\junit-4.12.jar;c:\\junit\\hamcrest-core-1.3.jar;c:\\users\\subodh\\downloads\\selenium-server-standalone-3.5.3.jar org.junit.runner.JUnitCore MyPkg.SeleniumTest",
      "required": true,
      "helpMarkDown": "parameter to powershell script"}],
  "execution": {
    "PowerShell": {
      "target": "SeleniumTest.ps1"}}
 }

Step 5. The following components are used in this file:

components-used

Create Extension manifest (vss-extension.json)

Step 6. All information about the extension is given in this file. It includes links to our image files with reference to the folder names. The example manifest file looks as follows:

{ "manifestVersion": 1,
  "id": "build-task",
  "name": "SSGS-Selenium",
  "version": "2.1.5",
  "publisher": "ssgsit",
  "targets": [{"id": "Microsoft.VisualStudio.Services"}],
  "description": "Tools for building with SSGS. Includes one build task.",
  "categories": ["Build and release"],
  "icons": {"default": "images/ssgsLogo.png"},
  "files": [{"path": "buildtask"}],
  "contributions": [{"id": "custom-build-task",
      "type": "ms.vss-distributed-task.task",
      "targets": ["ms.vss-distributed-task.tasks"],
      "properties": {"name": "buildtask"}}]
}

Step 7. The following components are used for this file:

components-manifest

Package your Custom Build Task

Step 8. Once the task is ready we need to put it in MarketPlace after combining all files together. That is why it is very important to have all the required files in the same folder. In order to create .vsix file we can use tfx cli command as follows:

tfx extension create --manifest-globs vss-extension.json

Note: if you need to update your task with some additional functionality, make sure that you change the version number and create a new .vsix file.

Publish your Custom Build Task

Step 9. Before you publish your task, we need to create a publisher. You can refer to www.dotnetcurry.com/visualstudio/1330/build-release-task-visual-studio-team-system to learn how to create a publisher.

Step 10. Sign to Visual Studio Marketplace. If you are not a publisher, follow the steps to become one. Follow the article link for more information. You need to specify a name of your choice, just like I have given SSGSIT. Review the agreement and click on Create.

Upload the Custom Build Task

Step 11. Now that the publisher is created, you need to upload the packaged task. Select New Extension, select Visual Studio Team Services and either drag and drop the file or upload the file and click on Upload button. The custom build task will be displayed after verification as follows.

upload-ext

 

uploaded-task

Step 12. You can now share the extension (task) with VSTS account.

 

share-task

Install Custom Build Task and Test it

Step 13. Now that the custom build task is already shared with one of your VSTS account, we just need to install and test it. I am going to add this task to the build definition as well as release definition. You will realize the importance of this step with release definition, after the application has been deployed.

Step 14. In order to install the custom build task, go to the Extensions tab for the VSTS account and you will find the extension (task) name appearing there. Select and click on Install.

Step 15. I am going to use a project which already has a build definition for Java with Ant build. For more information about this you can refer to article.

Modify Build Definition

Step 16. We can add the custom build task to the build definition. You can find this under the test tab. The icon for the task is taken as icon.png file, as mentioned earlier.

add-task-to-build-def

Step 17. Save and queue the build. We can see the selenium test getting executed and view the following result after a successful build. I have used Default build agent for this. I have already explained about build agent in the following article.

build-execution

We can find out the results for the test execution in the xml file specified.

Step 18. Create a Release Definition, upload the application to the web service. Add the custom task to your release definition and create release. You should be able to see the execution for Selenium Test successfully.

release-execution

Conclusion

In this article, I discussed how to create a custom build task in Visual Studio Team Services (VSTS) which can be used for build or release definitions. We also learnt how to package and publish the custom build task.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

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!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
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.


Page copy protected against web site content infringement 	by Copyscape




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