2
votes

Using Azure Devops the recommended way to update a task group is to create a draft first and test it on a a few releases. Next by publishing the task group a new version is created with the latest changes. Here is the documentation: https://docs.microsoft.com/en-us/azure/devops/pipelines/library/task-groups?view=azure-devops

Since I have over 100 release pipelines using the same task group I was wondering if there is a way to automatically update all the release pipelines to the latest published version of the task group. Currently I need to manually update every release by choosing the latest version of the task group.

Is there a way to do this automatically when publishing a new version?

1
You can use the API or az devops cli to grab the json for the release definition, search & replace the version and push it backjessehouwing

1 Answers

2
votes

You probably need to use the rest api to update the task group version in the release pipelines. See below steps:

1, Call Release Definitions - List api to get all the releases' id.

GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=6.0

2, Call Release Definition - Get api to get the release definition.

GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=6.0

3, Update the task group version in the request body and Call Release definition update api.

PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=6.0

See below full scripts in powershell: Change the {Taskid} and its version accordingly. See here to get a personal access token

$listurl="https://vsrm.dev.azure.com/{ogr}/{proj}/_apis/release/definitions?api-version=6.0"

$PAT="Personal access token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

#get the releases' ids.
$result = Invoke-RestMethod -Uri $listurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get

#loop the ids to get each release's definition
foreach($release in $result.value){
 
#get each release's definition
 $definitionurl="https://vsrm.dev.azure.com/{ogr}/{proj}/_apis/release/definitions/$($release.id)?&api-version=6.0"
   
  $releaseDefinition = Invoke-RestMethod -Uri $definitionurl-Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get

  #loop through each stage 
  foreach( $environment in $releaseDefinition.environments){
   
     #loop through each tasks to find the task group
     foreach($task in $environment.deployPhases.workflowTasks){
      
       # change the 'taskId'  to the taskId of your task group
        if($task.taskId -eq "{taskId}"){
           
            $task.version = "2.*"  # update the taskgroup version to the newest version
       }
     }
  }

    $updateurl="https://vsrm.dev.azure.com/{ogr}/{proj}/_apis/release/definitions?api-version=6.0"
   
    # update the release definition 
    Invoke-RestMethod -Uri $updateurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -ContentType "application/json" -Method PUT -Body (convertto-json $releaseDefinition -Depth 20)
}