我需要在构建定义中运行发布任务,但前提是某个任务失败之前。如果其他任务通过了,我希望它被忽略而不运行。
有没有办法做到这一点?
我希望我可以根据任务成功设置输出变量,然后在失败的情况下在自定义条件下使用该变量来运行任务。
如果任务失败,我看不到如何设置输出变量。这可能吗?
答案 0 :(得分:2)
您可以使用内置的REST API通过PowerShell检查以前的任务,例如检查taskA(显示名称)并在taskA失败时运行taskB:
Allow scripts to access the OAuth token
选项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"
}
Custom conditions
;自定义条件:eq(variables['isTaskAFailed'],'true'
)答案 1 :(得分:1)
Only when a previous task has failed
。如果仅在上一个/特定任务失败时才运行任务:
假设您的特定任务(根据其状态检查的任务)称为A
。目标是仅在B
失败的情况下调用另一个构建任务(例如A
)。
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
被安排在C
和is conditioned之后的某个时间运行,以防task.A.status
等于failure
,如下所示:{{1} } 答案 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"