如果Azure Devops中的某些其他任务失败,则如何运行任务 [英] How to run a task if some other task has failed in Azure Devops

查看:55
本文介绍了如果Azure Devops中的某些其他任务失败,则如何运行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当一个特定任务A失败时,我才想运行任务B.如何实现这一点是Azure DevOps.我尝试了此PowerShell,但没有用.

  param([string] $ token = $(System.AccessToken),[string] $ targetTaskName =签名和时间戳",[string] $ collectionURL = $(Build.Repository.Uri),[string] $ projectName = $(System.TeamProject),[string] $ buildId = $(Build.BuildId))回显$ token回声$ targetTaskName回声$ collectionURL回声$ projectName回声$ 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"-标头@ {Authorization =("Basic {0}" -f $ base64AuthInfo)}$ targetTask = $ result.records |其中{$ _.Name -eq $ targetTaskName}写入主机$ targetTask.resultif($ targetTask.result -eq"failed"){写主机"## vso [task.setvariable variable = task.signing.status] failure"} 

当我在参数中加上引号时,会出现此错误.

  param([string] $ token ="$(System.AccessToken)",[string] $ targetTaskName =签名和时间戳",[string] $ collectionURL ="$(Build.Repository.Uri)",[string] $ projectName ="$(System.TeamProject)",[string] $ buildId ="$(Build.BuildId)") 

解决方案

这里有一些要注意的地方:

  • 要获取收集网址,您需要使用变量 System.TeamFoundationCollectionUri .
  • 您可以使用 env:xxxx 调用预定义的Azure devops变量,如果您想直接在 ps1 文件中调用这些预定义变量.
  • 请不要将 $(System.AccessToken)转换为 Base64 字符串.此 Base64 转换仅适用于PAT.
  • 除非有必要,否则请勿在变量名称中使用.因为因为Azure开发人员,我们将其视为

    I want to run a task B only if one particular task A is failed. How to achieve this is Azure DevOps. I tried this PowerShell but didn't work.

    param(
        [string]$token = $(System.AccessToken),
            [string]$targetTaskName = "Signing and Time stamping",
            [string]$collectionURL = $(Build.Repository.Uri),
            [string]$projectName = $(System.TeamProject),
            [string]$buildId = $(Build.BuildId)
        )
    echo $token
    echo $targetTaskName 
    echo $collectionURL
    echo $projectName 
    echo $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=task.signing.status]failure"
        }
    
    

    When I put quotes in arguments I get this error.

    param(
        [string]$token = "$(System.AccessToken)",
            [string]$targetTaskName = "Signing and Time stamping",
            [string]$collectionURL = "$(Build.Repository.Uri)",
            [string]$projectName = "$(System.TeamProject)",
            [string]$buildId = "$(Build.BuildId)"
        )
    
    

    解决方案

    Here has some points you should pay attention to:

    • To get collection URL, you need use the variable System.TeamFoundationCollectionUri.
    • You can use env:xxxx to call our Azure devops predefined variables if you would like to call these predefined variables directly in ps1 file.
    • Please don't convert $(System.AccessToken) to a Base64 string. This Base64 conversion is just available for PAT.
    • Unless necessary, do not use dots in variable names. Because for Azure devops, we will treat it as hierarchical of the JSON file. So here you'd better use another variable name that do not has any special characters in it. As example, the name I specified is SigningStatus.

    I made some changes and re-constructed your powershell script. Now everything goes as expected. Refer to below script:

    [String]$projectName = $env:SYSTEM_TEAMPROJECT
    [String]$collectionURL= $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
    [String]$targetTaskName = "Command Line Script"
    [String]$buildId = $env:BUILD_BUILDID
    Write-Host $targetTaskName 
    Write-Host $collectionURL
    Write-Host $projectName 
    Write-Host $buildId
    $buildTimelineREST="$collectionURL$projectName/_apis/build/builds/$buildId/Timeline?api-version=4.1"
    $result = Invoke-RestMethod -Method GET -Uri $buildTimelineREST -Headers @{   
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
    }
    $targetTask=$result.records | where { $_.Name -eq $targetTaskName }
    Write-Host $targetTask.result
    if($targetTask.result -eq "failed"){
    Write-Host "ohhhh, it  failed"
    Write-Host "##vso[task.setvariable variable=SigningStatus]failure"
    }
    

    At last, please do not forget to enable the checkbox of Allow scripts to access the OAuth token at Agent job level.

    这篇关于如果Azure Devops中的某些其他任务失败,则如何运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆