Tuesday, 27 January 2026

Custom task in Azure Devops

 Create Custom task using powershell in Azure DevOps



What are extensions?

Extensions are simple add-ons that can be customize and extend  your Devops experience.
Extensions provide new capabilities when they are installed in the Azure devops orgnization. 

Pre-requisite:

1. Azure DevOps Orgnization
2. Code editor (suggested to have visual studio code)
3. Node.js
4. Azure DevOps CLI 


Installation

Here i am using mac os,  (please follow steps for your OS)

1 . Node js 
Run below command to install Node js

brew install node

Run below command to install CLI

npm install -g tfx-cli

3. Azure devops extension SDK 

Run below command to install CLI

npm install azure-devops-extension-sdk --save


Folder structure

Follow below folder structure

|--- README.md    
|--- images                        
    |--- extension-icon.png  
|--- buildandreleasetask            // Task scripts location
    |--- task.json                  // Task definition
    |--- .ts (or) .ps1                   // Main task logic (as per code typescript or powershell)
    |--- package.json               // Node.js dependencies
    |--- tests/                     // Unit tests
        |--- 
|--- vss-extension.json             // Extension manifest


Create Custom task

Here we start the creation of custom task, all files and command i am giving below should be run in the buildandreleasetask folder.

1. Lets initialize the node js project with the help of npm command below:
npm init --yes

This will create the package.json file in the folder, this file lists the node js dependensies.

2. Install azure pipeline task lib:

npm install azure-pipelines-task-lib --save 

3. Create task.json file in  buildandreleasetask, initially you can take the content from the link later change the parameters as per your requirement.

note: make sure you updated data as per requirement and existing info. Update properties "id", "name", "friendlyname", "description" and "author"

task.json describes the indivisual task and what it executes. This includes the parameters that user need to provide and which script to execute.

The task.json configures that what user going to see when they select the task in the UI.

Sample task.json file, with types of input code: 

{
"$schema": "https://raw.githubusercontent.com/Microsoft/azure-pipelines-task-lib/master/tasks.schema.json",
"id": "{{taskguid}}",
"name": "{{taskname}}",
"friendlyName": "{{taskfriendlyname}}",
"description": "{{taskdescription}}",
"helpMarkDown": "",
"category": "Utility",
"author": "prashant",
"version": {
"Major": 0,
"Minor": 1,
"Patch": 0
},
"instanceNameFormat": "Echo $(samplestring)",
"inputs": [
{
"name": "samplestring",
"type": "string",
"label": "Sample String",
"defaultValue": "",
"required": true,
"helpMarkDown": "A sample string (content in i button)"
},
{
"name": "sampleboolean",
"type": "boolean",
"label": "Sample Boolean",
"defaultValue": false,
"required": true,
"helpMarkDown": "A sample boolean (content in i button)"
},
{
"name": "samplespick",
"type": "pickList",
"label": "Sample pickList",
"defaultValue": "test1",
"required": true,
"helpMarkDown": "A sample pickList (content in i button)",
"options": {
"test1": "test1",
"test2": "test2"
}
},
{
"name": "sampleradio",
"type": "radio",
"label": "Sample radio",
"defaultValue": false,
"required": true,
"helpMarkDown": "A sample radio (content in i button)",
"options": {
"test1": "test",
"test" : "some other info needed"
},
"visibleRule": "sampleboolean = true"
}
],
"execution": {
"PowerShell": {
"target": "test.ps1"
},
"workingDirectory": "$(currentDirectory)"
}
}


4. Install vstsTasksdk:

Create folder ps_modules i the buildandreleasetask and open powershell in the ps_module folder. Run below command in the folder using powershell

Save-Module -name VstsTaskSdk -Path .

in case of any version inside the ps_module folder, take the data ouside of folder and past it in ps_module folder and remove the version folder, else you will run into issues when it tries to find the references.

5. Powershell Script

Write the script where functionality implemented. (no need to write complete logic, as we creating initial draft)

for example:

[CmdletBinding()]
param()
Trace-VstsEnteringInvocation $MyInvocation

try {
# Get inputs
$Command = Get-VstsInput -samplestring "samplestring" -required
$IsEnabled = Get-VstsInput -sampleboolean "sampleboolean" -required
$PickListValue = Get-VstsInput -samplespick "samplespick" -required
$RadioValue = Get-VstsInput -sampleradio "sampleradio"

Write-Host "Command: $Command"
Write-Host "Is Enabled: $IsEnabled"
Write-Host "Pick List Value: $PickListValue"
Write-Host "Radio Value: $RadioValue"
}

finally {
Trace-VstsExitingInvocation $MyInvocation
}

Package of extension

Once you have written code (complete mvp of extension) the next step is getting into market place. For this you need to pack all the files in a package. All extesnsions are packed in VSIX 2.0 compatible. tfx-cli provide the functionality to pack the files together. 
for this we need configuration file for pacakage which is nothing but the vss-extension.json  

On extension root directory, create file vss-extension.json  take sample file from here : link
or you can take below and make changes as per your requirement
{
"manifestVersion": 1,
"id": "testextension-task",
"name": "My Custom Tasks",
"version": "1.0.0",
"publisher": "your-publisher-id",
"targets": [
{
"id": "Microsoft.VisualStudio.Services"
}
],
"description": "Custom build and release tasks for Azure DevOps",
"categories": [
"Azure Pipelines"
],
"icons": {
"default": "images/pathto.png"
},
"files": [
{
"path": "buildandreleasetask"
}
],
"contributions": [
{
"id": "testextension",
"type": "ms.vss-distributed-task.task",
"targets": [
"ms.vss-distributed-task.tasks"
],
"properties": {
"name": "buildandreleasetask"
}
}
]
} 


Once the file is ready run the below command in the extension root folder:
tfx extension create --manifest-globs vss-extension.json

note: make sure before running the command you have updated task.json file also make sure you have provided correct path in json files.


Thank you. In case of any questions or issues let us know

 

Post Top Ad

Your Ad Spot

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template