Gaurav Joshi
Gaurav Joshi

Reputation: 1001

How to create work item on failure in azure Release pipeline not Build pipeline

I'm not seeing Create work item on failure option on the Options tab. I'm using this as a reference 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

Upvotes: 0

Views: 3955

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35564

In Azure Devops, the option Create 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).

Upvotes: 2

Related Questions