0
votes

I'm not seeing "crate work item on failure" option under option tab I'm referring this https://developercommunity.visualstudio.com/content/problem/343557/create-work-item-on-build-failure-lost-work-item-t.html.

Actually I have to create a bug work item when there is a failure in my pipeline in azure devops. I saw couple of post few of them saying use API or few say there is an option in Azure itself but I'm not able to see please have a look in attached image.

Any help will appreciate.

enter image description here

1
Hi @Gaurav Joshi. In Release Pipeline, you need to add a Powershell task to run the Rest API in the Agent Job and set the condition. When the agent job failed, the work item could be created. You could refer to the answer. If you only need to creat a Bug work item. you could also use the extension. Thanks . - Kevin Lu-MSFT
Hi @Gaurav Joshi. Is there any update about this ticket? Feel free to let me know if the answer could give you some help. Just a remind of this. - Kevin Lu-MSFT
Hello @KevinLu-MSFT Yes I managed to create work item. Though i have few questions 1)Do I need to hardcode my organization and project name ..? is there way I can get directly..? 2)How we can assigned newly created task to a user via powershell script. While creating I have to assign it to someone bydefault it created unsigned. 3)Is there any way we can get real info from the release pipeline like reason for failure description, task name or stage name, time when it happen 4)Can we set task Priority, tag.? As per your suggestion I’m trying with $user=”user ” but it did’t work. - Gaurav Joshi
Hi @Gaurav Joshi. Please check the update. I have updated the Powershell Script and added some features in it. Hope it helps. - Kevin Lu-MSFT

1 Answers

1
votes

In Azure Devops, the option crate work item on failure indeed exists, but it only exists in build pipeline for the time being.

enter image description here

From your screenshot, you are using the Releases Pipeline. So you couldn't find it.

In Release Pipeline, you need to use API to create a work item.

Here is an example:

You could Use Powershell Task to run Rest API to create a work.

Set the condition

enter image description here

Or you could directly use this Extension- Create Bug on Release failure.

Powershell script update:

$witType="task"

$token = "PAT Token"

$url="$(SYSTEM.TEAMFOUNDATIONCOLLECTIONURI)/$(SYSTEM.TEAMPROJECT)/_apis/wit/workitems/`$$($witType)?api-version=6.0"

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

  $body="[
          {
            `"op`": `"add`",
            `"path`": `"/fields/System.Title`",
            `"value`": `"titlename`"
          },
          {
            `"op`": `"add`",
            `"path`": `"/fields/System.AssignedTo`",
            `"value`": `"e-mail address`"
          },

          {
            `"op`": `"add`",
            `"path`": `"/fields/Microsoft.VSTS.Common.Priority`",
            `"value`": `"4`"
          },
          {
            `"op`": `"add`",
            `"path`": `"/fields/System.Tags`",
            `"value`": `"Tag1; Tag2`"
          },
          {
            `"op`": `"add`",
            `"path`": `"/fields/System.History`",
            `"value`": `"<div>$(SYSTEM.STAGEDISPLAYNAME)</div>`"
          }

       ]"


$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $body -ContentType application/json-patch+json

Explanation:

This API could add Priority, tag, assign to and set the stage name as the discussion.

Since this task is created when it fails, it can directly output the stage name variable to the discussion.

Result:

enter image description here

Note: You could refer to this doc to create a PAT (personal access tokens).