Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
937 views
in Technique[技术] by (71.8m points)

azure devops - VSTS - Is there a way to only run a task if a specific task has failed?

I need to run a publish task in my build definition but only if a certain task before it has failed. If the other task passes I want this to be ignored and not run.

Is there a way of doing this?

I was hoping that I could set an output variable based on the task success and then use that variable in a custom condition to run the task if it's failed.

I can't see how to set an output variable if the task fails. Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can check previous tasks through PowerShell with build REST API, for example to check taskA (display name) and run taskB if taskA failed:

  1. Check Allow scripts to access the OAuth token option in the Phase
  2. 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"
    }
  1. The taskB (Run this task: Custom conditions; Custom condition: eq(variables['isTaskAFailed'],'true')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...