Get task status in next step

[亡魂溺海] 提交于 2021-01-29 08:39:36

问题


I have a task Maven@3 that is running my tests and I would like to get its status in a next task. Is there a variable to get it ?

For example :

  - task: Maven@3
    displayName: UnitTest
    name: UnitTest
    inputs:
      ...maven options...
      goals: 'test'
  - script: echo $(succeeded('UnitTest'))

In the script steps, I would like to print the UnitTest Task status.


回答1:


The workaround is to use multi job pipeline to separate UnitTest task in a separate job and other jobs dependent on it. Then you can use expression $[ dependencies.A.result ] to get the UnitTest status. Please check below example:

jobs:

- job: A
  pool: 
    vmImage: windows-latest
  steps:
    - checkout: none  #use checkout step to void syncing repo

    - task: Maven@3

- job: B 
  dependsOn: A
  condition: always()
  variables:
    testStatus: $[ dependencies.A.result ]
  steps:
    - script: echo $(testStatus) 

You can also submit a feature request(Click a suggest a feature and choose Azure devops) for supporting variable to get Task status to Microsoft Development team.

Update:

Use Azure devops rest api to get Task status. Below is the scripts:

- powershell: |  
    $url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)/timeline?api-version=5.1"

    $result = Invoke-RestMethod -Uri $url -Headers @{authorization = "Bearer $(system.accesstoken)"} -ContentType "application/json" -Method get

    $taskResult = $result.records | where {$_.name -eq "UnitTest"} | select result

    echo $taskResult 
  condition: always()


来源:https://stackoverflow.com/questions/60598945/get-task-status-in-next-step

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!