ca9163d9
ca9163d9

Reputation: 29159

Get build start time, end time, result, project name, etc?

I plan to write PowerShell task for Build pipeline. Are there any built-in variable for building start time, end time, result, project name, etc?

Upvotes: 2

Views: 7000

Answers (2)

Leo Liu
Leo Liu

Reputation: 76660

Are there any built-in variable for building start time, end time, result, project name, etc?

I am afraid there is no such built-in variable to get the building start time, end time, result, project name, etc directly.

As you can see the comprehensive list of predefined variables, there is no such built-in variables.

In order to get their value, we need use the PowerShell and Rest API:

https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/get?view=azure-devops-rest-5.1

The powershell scripts:

$url = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1"
$buildPipeline= Invoke-RestMethod -Uri $url -Headers @{   
 Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
} -Method Get

$starttime= $buildPipeline.startTime

Write-Host This is start time: $starttime

$Endtime= $buildPipeline.finishTime

Write-Host This is end time: $Endtime

$result= $buildPipeline.result

Write-Host This is build result: $result

$projectname= $buildPipeline.definition.project.name

Write-Host This is project name: $projectname

The result:

enter image description here

Update:

So I will need to create another pipeline for the rest api call and trigger it from my Build pipeline? BTW, how to trigger it?

Yes, you need to create another pipeline for the rest api call. To trigger it, you can add CallAPI build as build completion:

enter image description here

In this case, when the build pipeline completed, it will trigger the rest api build.

how to pass buildId to the new pipeline?

We could use Builds - List API with definitions Parameters to list the build Ids for the specify build definition, like:

https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1&definitions={DefinitionId}

Then we could get the first build id, then we could use the Logging Command to set that Id as ENV variable:

Write-Host ("##vso[task.setvariable variable=testvar2]testvalue2")

In this case, we could get that ID in the next task.

Hope this helps.

Upvotes: 4

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

Yes, there is a long list of a pre-defined variable for the build pipeline.

If you use PowerShell inlnie you use them in this way:

$(Build.BuildNumber)

If the PS script is in a file you use them in this way:

$env:Build_BuildNumber

Upvotes: 0

Related Questions