Create bug in a different project when build fails

2019-02-25 00:03发布

We have setup a build definition which creates a bug and assigns it to "requestor" incase the build fails. See below : enter image description here

By default, the bugs are created in the same project. How do I change this to create the bugs in a different project ? The reason is, our work item tracking is in a different project than our Git repos (due to reshuffling and what not). Note that both the projects are under the same account.

Looking at the tool tip, it provides some sample fields, is there a corresponding field for "project" ? (System.Project ?!)

2条回答
来,给爷笑一个
2楼-- · 2019-02-25 00:44

You can not do this out-of-the-box, but ...

You could do it using the API, something like this:

1 - Add a task to the build process to call a rest API and call the api to create a workitem. See https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#create-a-work-item

2 - Change your agent phase to allow access to the token. Your task will need this.

3 - Change your task in Control Options and change the "Run this task" to "Custom condition" and fill the field with

eq(variables['Agent.JobStatus'], 'Failed')

Ref https://docs.microsoft.com/en-us/vsts/build-release/concepts/process/conditions?view=vsts

This should do it.

查看更多
The star\"
3楼-- · 2019-02-25 01:03

The Create work item on failure option in VSTS build can only create work item in the same project.

The workaround to create a Bug if build failed can be achieved by adding a PowerShell task. Detail steps as below:

1. Add a PowerShell in the end of your build definition

In the PowerShell script, you can create a Bug for a different project by REST API. Below is an example script:

$witType="Bug"
$witTitle="Build $(build.buildNumber) failed"


$u="https://account.visualstudio.com/DefaultCollection/project/_apis/wit/workitems/`$$($witType)?api-version=1.0"
$body="[
          {
            `"op`": `"add`",
            `"path`": `"/fields/System.Title`",
            `"value`": `"$($witTitle)`"
          }
       ]"
$user = "user"
$token = "PAT"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$result=Invoke-RestMethod -Method PATCH -Uri $u -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json-patch+json" -Body $body

2. Specify Run this task option as Even if a previous task has failed, unless the build was canceled

From Second task to the end task (PowerShell task), change Run this task option as Even if a previous task has failed, unless the build was canceled:

enter image description here

So when build fails, a Bug will be create to the different project as you specified in PowerShell script.

查看更多
登录 后发表回答