Integrate Azure DevOps Pipelines via Powershell

If you read the last post, we saw how you can integrate Azure DevOps via its .NET Client Libraries to automate task import (from Task Groups) into our CI/CD pipelines.

You must have noticed, that I mentioned another alternative VSTeam Powershell as well, so in this post we will see how to automate atleast one of the task import automation via it.

If you read through the VSTeam’s link, the first thing we have to do is download that module. So lets get started…

Firing up powershell, I noticed old version… so I first updated it to the latest version and then installed the module as shown below:

image

image

With that done, I validated my connection to Azure DevOps via PAT:

image

Now, I was ready to script this automation, which I have shown below with comments to explain main steps:

NOTE: i heavily leveraged source and this issue to find the cmdlets and experimented to get the below script working:


# Get the CI Build Definition
$buildDefinition = Get-VSTeamBuildDefinition -Id 4   -Raw  # Use the Id here since its the only Build in the project

# retrieve the steps of the build
$steps = $buildDefinition.process.phases[0].steps

# Get the Build Task Group by its name 
$taskGroup = Get-VSTeamTaskGroup -Name "Export-MetaTags"

# define definition step
$definitionStep = [PSCustomObject]@{
	displayName = $taskGroup.name
	enabled = "True"
	task = [PSCustomObject]@{
		id = $taskGroup.id
		versionSpec = $taskGroup.version
		definitionType = "metaTask"
	}
	inputs = $taskGroup.inputs	
}

# supply value to the definitionStep param
$inputValue = @{}
$inputValue.Add("csprojfile", "<path to csproj>")

$definitionStep.inputs = $inputValue

# update Definitions Tasks
$buildDefinition.process.phases[0].steps = $steps + $definitionStep

# add Comment
Add-Member -InputObject $buildDefinition -NotePropertyName "comment" -NotePropertyValue "Imported Update Tags" -Force

# Update Definition
$buildJson = $buildDefinition | ConvertTo-Json -Depth 100
Update-VSTeamBuildDefinition -ProjectName AzureFunctionDeployment -Id 4 -BuildDefinition $buildJson

The above script is bare minimum and does not perform any validation checks, which you might have to implement on your end

Following is the updated build pipeline after executing the above script

image

So there you see folks… depending on your preference, you can leverage this option as well. For my use case we ended up leveraging the .NET Client libraries

BTW: I’ll leave the updating of CD pipeline as an exercise for you to explore 😉 and keep this post short.

Related Posts