How to: Build Definitions and Tasks Mapping on Azure DevOps
This script and report extract all Build Tasks on any Build Definitions to map where they are being used within the Azure DevOps organization
This report was based on the extension Build Task Explorer. Thanks, Mathias Olausson for inspiring this publication.
An original script is available on my GitHub repository. See below this script:
Let’s go understand each command used.
- PowerShell script will receive the following parameters:
- $PAT = Personal Access token to connect on Azure DevOps;
- $Organization = Organization URL to list all Team Projects, Build Definitions, and Build Tasks;
- $Connstr = connection string to Azure SQL Database that stores the report information. To create this report, it’s necessary to create previously a Azure SQL Server and Database and run a script below:
2. Projects List = uses this REST API to list all Team Projects on Azure DevOps organization
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }$UriOrganization = "https://dev.azure.com/$($Organization)/"$uriProject = $UriOrganization + "_apis/projects?`$top=500"$ProjectsResult = Invoke-RestMethod -Uri $uriProject -Method get -Headers $AzureDevOpsAuthenicationHeaderForeach ($project in $ProjectsResult.value)
{
Write-Host $project.name
}
3. Definitions List = uses this REST API to list all Build Definitions on each Team Project
$uriBuildDefinitions = $UriOrganization + "$($project.id)/_apis/build/definitions?api-version=6.1-preview.7" $BuildDefintionsResult = Invoke-RestMethod -Uri $uriBuildDefinitions -Method get -Headers $AzureDevOpsAuthenicationHeader Foreach ($builDef in $BuildDefintionsResult.value)
{
Write-Host $buildDef.name
}
4. Definitions Get = uses this REST API to get details about Build Definition
$uriBuildDef = $UriOrganization + "$($project.id)/_apis/build/definitions/$($builDef.id)?api-version=6.1-preview.7" $BuildDefResult = Invoke-RestMethod -Uri $uriBuildDef -Method get -Headers $AzureDevOpsAuthenicationHeaderWrite-Host $BuildDefResult.process.phases
5. Build Task Details = uses this REST API to get details about Build Task
$uriTaskName = $UriOrganization + "_apis/distributedtask/tasks/$($step.task.id)?api-version=6.1-preview.1" $TaskNameResult = Invoke-RestMethod -Uri $uriTaskName -Method get -Headers $AzureDevOpsAuthenicationHeaderWrite-Host $TaskNameResult.value[0].name
6. After extract all Team Projects, Build Definitions, and Build Tasks, this information is stored in a table in Azure SQL.
7. After insert information into a table, I connected this database on Power BI:
- Task Name (1) = Filter report using Task Name field;
- Team Project Name (2) = Filter report using Team Project Name field;
- Build Definition Name (3) = filter report using Build Definition Name field;
- Build Definitions and Tasks Mapping Information (4) = List all information about Build Tasks, Project Name, Definition Name, and Number of usages from a respective task;
- Number of usages = Looking at the example above, the DotNetCoreCli task is being used 4 times in the Build Definition ANP.Admin CI.