VSTS-有一种方法只能在特定任务失败时才运行任务吗?

时间:2018-09-13 10:55:03

标签: azure-devops

我需要在构建定义中运行发布任务,但前提是某个任务失败之前。如果其他任务通过了,我希望它被忽略而不运行。

有没有办法做到这一点?

我希望我可以根据任务成功设置输出变量,然后在失败的情况下在自定义条件下使用该变量来运行任务。

如果任务失败,我看不到如何设置输出变量。这可能吗?

4 个答案:

答案 0 :(得分:2)

您可以使用内置的REST API通过PowerShell检查以前的任务,例如检查taskA(显示名称)并在taskA失败时运行taskB:

  1. 在“阶段”中选中Allow scripts to access the OAuth token选项
  2. 添加PowerShell任务(运行此任务:Only when a previous task has failed;参数:-targetTaskName "taskA" -collectionURL $(Build.Repository.Uri) -projectName $(System.TeamProject) -buildId $(Build.BuildId) -token $(System.AccessToken)

代码:

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. 任务B(运行此任务:Custom conditions;自定义条件:eq(variables['isTaskAFailed'],'true'

答案 1 :(得分:1)

  • 在“任务”设置中,展开“控制选项”
  • 选择“运行此任务”-Only when a previous task has failed

enter image description here

如果仅在上一个/特定任务失败时才运行任务:

假设您的特定任务(根据其状态检查的任务)称为A。目标是仅在B失败的情况下调用另一个构建任务(例如A)。

  • 定义一个custom build variable,将其命名为task.A.status并设置为success
  • 创建另一个构建任务,例如C并将其安排在A之后;使其只有在A失败的情况下才能运行-有一个标准条件
  • 任务C只能做一件事-将task.A.status生成变量设置为'failure'(如果正在使用PowerShell:Write-Host "##vso[task.setvariable variable=task.A.status]failure",则将其设置为'failure')
  • 最后,任务B被安排在Cis conditioned之后的某个时间运行,以防task.A.status等于failure,如下所示:{{1} }

enter image description here

答案 2 :(得分:0)

除了starian chen-MSFT的代码(该代码适用于构建管道)之外,以下是我汇总的发布管道代码(以防对任何人有用)。这将输出管道变量“ deploymentStatus”,该变量等于任务的状态(由Azure DevOps:https://docs.microsoft.com/en-us/rest/api/azure/devops/release/releases/get%20release?view=azure-devops-rest-5.1#taskstatus定义):

$targetTaskName = "Some Task Name"
$token = $env:System_AccessToken
$releaseURL = $env:System_TeamFoundationServerUri
$projectName = $env:System_TeamProject
$releaseId= $env:Release_ReleaseId
$environmentId = $env:Release_EnvironmentId
$releaseAttemptNumber = $env:Release_AttemptNumber

$releaseREST="$releaseURL$projectName/_apis/release/releases/$releaseId/environments/$environmentId/?api-version=5.1-preview.6"
Write-Host "GET request to Azure Pipelines with URL: $releaseREST"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "test",$token)))
$result= Invoke-RestMethod -Method GET -Uri $releaseREST -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$targetReleaseAttempt = $result.deploySteps | where { $_.attempt -eq $releaseAttemptNumber}
$targetTask = $targetReleaseAttempt.releaseDeployPhases.deploymentJobs.tasks | where { $_.name -eq $targetTaskName }
$targetTask

$targetTaskStatus = $targetTask.status
Write-Host "Setting variable deploymentStatus to $targetTaskStatus"
Write-Host "##vso[task.setvariable variable=deploymentStatus;]$targetTaskStatus"

答案 3 :(得分:0)

根据@Merivale的回答,这是对我有用的代码;

$targetTaskName = "SomeTaskName"

$pat = ":$(System.AccessToken)"
$b  = [System.Text.Encoding]::ASCII.GetBytes($pat)
$token = [System.Convert]::ToBase64String($b)

$releaseURL="$(System.TeamFoundationServerUri)"
$releaseId= $(Release.ReleaseId)
$environmentId = $(Release.EnvironmentId)
$releaseAttemptNumber = $(Release.AttemptNumber)

$releaseREST_p1="$releaseURL"
$releaseREST=$releaseREST_p1+"$(System.TeamProject)/_apis/release/releases/$releaseId/environments/$environmentId/?api-version=5.1-preview.6"
Write-Host "GET request to Azure Pipelines with URL: $releaseREST"

$result= Invoke-RestMethod -Method GET -Uri $releaseREST -ContentType "application/json" -Headers @{'Authorization' = "Basic $token"}

$targetReleaseAttempt = $result.deploySteps | where { $_.attempt -eq $releaseAttemptNumber}
$targetTask = $targetReleaseAttempt.releaseDeployPhases.deploymentJobs.tasks | where { $_.name -eq $targetTaskName }
$targetTask

$targetTaskStatus = $targetTask.status
Write-Host "Setting variable releaseStatus to $targetTaskStatus"
Write-Host "##vso[task.setvariable variable=releaseStatus;]$targetTaskStatus"