What Are Azure DevOps Extensions?
Prerequisites for Creating Azure DevOps Custom Task:
Install Required Tools (Node.js, Azure DevOps CLI, SDK)
Run below command to install Node js
brew install node
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
How to Create a Custom Azure DevOps Task Using PowerShell
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 .
[CmdletBinding()]param()Trace-VstsEnteringInvocation $MyInvocationtry {# 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}
How to Package and Publish Azure DevOps Extension
{"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"}}]}
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.
FAQs
What is a custom task in Azure DevOps?
How do I create a PowerShell task in Azure DevOps?
What is task.json in Azure DevOps?