Creating a custom pipeline task for Azure DevOps

06 mei 2020 om 14:43 by ParTech Media - Post a comment

Not too long ago we published an article on creating your first Azure DevOps Build Pipeline.

You may want to implement a pipeline task that is not readily available. In that case you can create your own custom task that can later be executed from your pipeline.

Here is how you do it:

What Will I Need?

Even before you create a custom pipeline task, it is important to have the relevant prerequisites. Here’s what you’ll need for the process.

NodeJS

This is one of the first things you’ll need for executing the customizations. You can download the LTS or Long-Term Support version (10.15.3) of NodeJS. Install the application by following the instructions entailed by the installation wizard.

Typescript

You’ll also need Typescript for this process. Install it through the g-command. Note that Typescript will only work on your system when you have already installed NodeJs.

Node CLI

Node CLI is a command line application that interacts with DevOps and Microsoft’s servers.

Getting Started

Now that you’ve installed the required tools, here’s what you need to do next.

Step One

Start by creating this folder structure.

img

Step Two

Once you’re done, create the task definition. The idea here is to come up with the metadata file called task.json. Save this in your ‘Task’ folder. If you’re unfamiliar with task.json, check out this schema on Microsoft’s GitHub Page.

As a part of the process, you’ll also need to fill out and understand the following parameters.

  • Task ID: Give your task a new GUID. After this, key in a name, a short description, and the name of the author.
  • Category: This will assess the tab that is to be assigned once you add a specific task to the build.
  • Versions: Every build leverages your task’s latest version within the existing version. That means, if the build runs on an older version, but you create a newer version, the agents will start running on the newer version without any manual tweaking.
  • Input: This is the section where you can different variables that are included in your script. You can add anything from String to Filepath.
  • Script: This part will inform which script will be running after the execution of a task.

Step Three

For the third step, you need to add a code snippet to your existing PowerShell script. Note that this script will be automatically saved in the Task Folder bearing the same name as available in the original task.json file.

Here’s a simple example of a similar Powershell script.

img

Resource: https://www.parksq.co.uk/azure-dev-ops/custom-build-tasks

Step Four

We will now create a custom task, a command line tool in this instance. For this, you need to first need to create a console for your .NET application. The arguments available from the PowerShell script can be accessed by interpreting the args collection in the Main() method. Refer to the following code snippet for better understanding.

static void Main(string [] args)
{
    foreach(var arg in args)
    {
        Console.WriteLine(arg);
    }
}

Any code written in the Console window will be automatically reflected in Microsoft’s Team Foundation Server Logs. The developer needs to run the correct code to ensure that the TFS accurately identifies whether the application has properly exited or met with a new error.

After you’re done writing the code for your CLI application make sure the code is built in the Release mode. You should further ensure that it’s properly zipped with the json schema and additional task artifacts before it’s finally uploaded in the Team Foundation Server log.

Step Five

After completing the task, upload it in Microsoft’s extension packaging tool or the TFX. Alternatively, you can also the TFS API for the upload.

Since this is the final step, your custom task is now available and can be added to your existing Azure DevOps pipeline.

Guidelines

Since you’re now aware of the steps required to create a custom pipeline task, here are some additional guidelines that’ll help you get through without any roadblocks.

Different Task Versions in a Single Extension

If you’ve already tried building a custom pipeline task, you’re probably familiar with multiple task versions in one extension. Versioned tasks can be created with a unique folder structure. Here, the version of every task will be right under their names.

Avoid the Official Repository of Azure Pipeline Tasks

While the official repository is one of the best places to familiarize yourself with the working mechanism of SDK, you should avoid following this standard pattern for the layout or build of your repository. That is because unlike your custom tasks that are packaged and deployed in the Video Studio Extension Installer format, the official tasks follow an entirely different pattern.

Package Limitation

While packaging your custom pipeline, make sure its size doesn’t exceed 50 MB. If it doesn’t follow the limit, the Visual Marketplace will immediately reject it.

The Task Needs to Be in a Folder

Although we previously mentioned this, many developers prefer putting the task.json right at the repository’s root. This isn’t recommended because DevOps can’t easily find the task if it’s saved in the root. That is why, the task needs to be saved in a folder with the exact same name as the task.json file.

Each Task Should Have a Separate Repository and Extension

While there isn’t any hard and fast rule about this, it is best to allocate each task in a separate repository and extension. This will not merely align your task with the specific size limit but will also give you the option for automating the versions and task, both of which will now be in sync. What’s more, the build will be fairly less complicated, and the code integration will be significantly easier.

Conclusion

Now that you know how to create a custom Pipeline Task for Azure, follow our guidelines and build your first task right away!