Custom Build Task in Visual Studio Team Services (VSTS)

Posted by: Gouri Sohoni , on 2/18/2018, in Category VSTS & TFS
Views: 8951
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.

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

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!

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook