Andrew
Andrew

Reputation: 577

How to get reason for failure of a previous task in vsts CI build

Is there a way to get the reason for a previous task failing so I can use it in a later task. Right now I'm naming and shaming who ever breaks the build and linking to the commit that did it in slack. But you can't tell why it broke unless you go to vsts and look at the logs.

I didn't see any build variables that get set on failures. Right now the only thing I can think of is to route stderr to a file and the console and then on failure read that file into a variable. But that seems hacky.

Upvotes: 1

Views: 660

Answers (1)

Marina Liu
Marina Liu

Reputation: 38116

After the task which you want to get the detail failed reason, you can add a PowerShell task to get the previous tasks’ details by Rest API. Detail steps in the PowerShell task:

1. Execute the rest API to get all the previous build tasks’ details.

2. Search the failed task by task name or build result. An you can get the task’s detail as

{
  "id": "b75e0dd2-9734-4e83-ab5b-dc6001ea037c",
  "parentId": "78e591b0-98f5-4d8a-a46c-417fda2c36dc",
  "type": "Task",
  "name": "Run cd ",
  "startTime": "2017-08-28T05:33:51.86Z",
  "finishTime": "2017-08-28T05:33:53.053Z",
  "currentOperation": null,
  "percentComplete": null,
  "state": "completed",
  "result": "failed",
  "resultCode": null,
  "changeId": 12,
  "lastModified": "0001-01-01T00:00:00",
  "workerName": "machine",
  "order": 4,
  "details": null,
  "errorCount": 1,
  "warningCount": 0,
  "url": null,
  "log": {
    "id": 3,
    "type": "Container",
    "url": "https://account.visualstudio.com/DefaultCollection/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4/_apis/build/builds/1339/logs/3"
  }

3. Then you can get the detail failed logs in the url option (as the url https://account.visualstudio.com/DefaultCollection/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4/_apis/build/builds/1339/logs/3 in above example).

Upvotes: 1

Related Questions