Create Custom task using powershell in Azure DevOps
What are extensions?
Pre-requisite:
Installation
Run below command to install Node js
brew install node
npm install -g tfx-cli
Run below command to install CLI
npm install azure-devops-extension-sdk --save
Folder structure
Create Custom task
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}
Package of 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.