This post is part of a series of articles on the new TFS Build vNext System.
So I’ve talked before about how the new cross platform build system in TFS 2015 and Visual Studio Online is fully extensible and allows you to create your own custom build tasks but it isn’t immediately obvious how to do it.
I’ll would like to walk through an end to end example creating a custom vnext build task to run a SQL script as part of the build
The tasks for build vNext are open source and Microsoft is accepting contributions so if you write a really nice and useful task it could be included in every Visual Studio Online account.
The first port of call is the GitHub page that hosts the code for the standard Microsoft Build Tasks, fork and clone this and you will find the code for all the existing build tasks. I created a new folder, SqlScript, for my new sql script build task, and it is in here I’ll add the code to create the task.
Anatomy of a vNext Build Task
Each task is defined by a .json file. This file defines the task meta data such as name, version etc and also defines the task GUI that is displayed in the web portal when editing builds. Lets take a look at thefile for the sql script task.
HTML 1 – TRACEY
The first section defines some simple meta data such as a unique guid identifier, name, version, author etc.
Then we start to define the GUI with the ‘inputs’ section, we can also organise inputs into custom group sections using the ‘groups’ section. It’s quite simple and the best way to get an idea is to just look at other tasks and see how they have been defined.
Here’s my implementation of the sql script task in powershell. Note how the script parameters I use match the inputs I defined in thefile.
HTML 2 – TRACEY
Building your Task
The build process for Build vNext tasks uses Gulp. So if you haven’t already, set up node on your machine. Then run
HTML 3 – TRACEY
from the root of the project directory, this will pull down all the node and gulp dependencies for the web. Finally, simply run
HTML 4 – TRACEY
from a command line at the root of the project directory. This will create a _build directory where all the tasks will be copied to ready for uploading into TFS/VSO. Note it also creates afile and country specific resource files that will be used for language localisation, if you need to support that.
Uploading your Task
It’s not obvious how to do this, but looking at the project I can see a TaskUploader folder and script (written in typescript) and further looking in theI can see a build task called simply ‘uploader’. So I try running
HTML 5 – TRACEY
HTML 6 – TRACEY
Which leads me to try the following to upload my task into Visual Studio Online
HTML 7 – TRACEY
Doing so, you should get prompted for your alternate credentials and finally you will see your new task available from the web portal.
A few things to note:
- Tasks are server wide (shared across all projects and project collections)
- When you update an existing task be sure to increase the version number. Failing to do so means your agents will not downland the latest version
- Builds will always use the latest version of your task. If you need to support multiple versions of your task you will need to upload several versions of it under different names and guids.
Sql Script Task in Action
We can configure the task as follows. Instead of leaving the database password in plain text in the task, I create an encrypted variable for the password and reference the variable in the task.
By Hamada Shatha – Consultant
Originally published on shiningdragon.net