Creating custom vNext build task for Team Foundation Server 2015

By Mirek on (tags: build, tfs, vNext, categories: tools, infrastructure)

The new vNext build system in TFS 2015 is simpler and easier to manage than the previous version, Windows Workflow based, build system. It builds up from a collection of build step which you can add and remove. The collection of available build steps is pretty rich and can be extended by a custom build steps. In this post I will show you how to easy create and deploy a custom vNext build step.

UPDATE 08.05.2017: This solution is not valid on TFS 2017 Update 1 any more. Go here for working approach.

Build step in Visual Studio Online or TFS 2015 is basically a nodejs or powershell script which is run by the build agent. It may accept the input parameters and has access to the build variables and build directories.
There is a github repository where you can find most of the build steps used in  VSO and TFS 2015. This is a good place to give you a reference on how to construct your own build steps. Apart from the logic contained in the actual step script, the build step also requires a definition file in json format. I couldn’t find a good documentation on the structure of this file at the moment of writing this post, but we can take and see the structure of it from the mentioned build steps repository.
Apart from the only mandatory files, wchih we need to create and upload to the tfs server, there are also optional files like build step icon (32x32 pixels .png or .svg image) and the multilingual translations. You will see many examples of it in the steps repo too.

Let’s then create a custom build step, which will just, for testing purposes and for the simplicity, lists us few build agent directories. Lets name it ShowDirs. We start with ShowDirs.ps1 file and fill it with these few commands

 

By the way all build variables are available as a environmental variables in step scripts. You access them as shown above, by $env:. More on build variables can be found here.

Next we need to define the task.json file

Most important: the id parameter must be an unique guid, which you should generate your self for each of custom build step you upload. The category string must match one of the existing categories visible in TFS web portal. This file lacks the input parameters, since our task step does not require any. However again, you can find many examples of input parameters in the mentioned tasks repo on the github.
Now we are ready to upload the build step to the TFS server. All we need to do is to place the files in a folder, zip it and upload to the rest API available at

where {id} is the id of the task. Here is the powershell script which will simplify this process

To upload a custom task located in ShowDirs directory just run following command

You will be prompted for the account password and if everything went ok you should see the build step on the build steps collection available in the web portal.

And finally when you add it to your build definition you will get the directories paths in the output console and in the logs

Important note: every time you want to update the custom build step to the next version, you will have to increase the version of the step in task.json file. Otherwise the build server will not accept it.