You can check previous tasks through PowerShell with build REST API, for example to check taskA (display name) and run taskB if taskA failed:
- Check
Allow scripts to access the OAuth token
option in the Phase
- Add PowerShell task (Run this task:
Only when a previous task has failed
; Arguments: -targetTaskName "taskA" -collectionURL $(Build.Repository.Uri) -projectName $(System.TeamProject) -buildId $(Build.BuildId) -token $(System.AccessToken)
)
Code:
param(
[string]$token,
[string]$targetTaskName,
[string]$collectionURL,
[string]$projectName,
[string]$buildId
)
$buildTimelineREST="$collectionURL$projectName/_apis/build/builds/$buildId/Timeline?api-version=4.1"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "test",$token)))
$result= Invoke-RestMethod -Method GET -Uri $buildTimelineREST -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$targetTask=$result.records | where { $_.Name -eq $targetTaskName }
Write-Host $targetTask.result
if($targetTask.result -eq "failed"){
Write-Host "##vso[task.setvariable variable=isTaskAFailed;]true"
}
- The taskB (Run this task:
Custom conditions
; Custom condition: eq(variables['isTaskAFailed'],'true'
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…