floAr
floAr

Reputation: 829

Pass content from build back into Visual Studio Team Services Build

I am running build on Azure with a custom build agent (using Unity3d to be precise) I generate output of the build within a .txt file on the build machine and would like to include content within work items created during build.

Example:

  1. Unity build fails and an error is logged to Build.log.
  2. New bug is created with reference to build and the error message from the logfile

Right now I am using a powershell script

$content = [IO.File]::ReadAllText("$(Build.Repository.LocalPath)\BuildProjectPC\Build.log")
Write-Host "##vso[task.setvariable variable=logContent;]$content"

To format the bug i use System.Description = $logContent but the content of the variable from PS does for some reason not end up in the bug item (it just contains "$logContent").

Any idea or direction how to fix this, respectively how to feed info back into vsts?

Upvotes: 0

Views: 284

Answers (2)

starian chen-MSFT
starian chen-MSFT

Reputation: 33738

The variable value that used for creating work item is initialized before running build steps, so you can’t specify a dynamic variable or change the variable value during the build step that used for creating work item. You can follow up these steps to verify it:

  1. Open your build definition > Variable > Add a new variable (e.g. logContent, value: oldValue)
  2. Select Options > Check Work Item on Failure > Additional Fields > System.Title $(logContent)
  3. Add PowerShell build step: Write-Host "$(logContent)"
  4. Add PowerShell build step: Write-Host "##vso[task.setvariable variable=logContent;]newValue"
  5. Add PowerShell build step: Write-Host "$(logContent)"
  6. Add PowerShell build step: [Environment]::Exit(1)

The log result:

  1. Step 3 output oldValue
  2. Step 5 output newValue
  3. The created work item title oldValue.

The workaround for your requirement is that, you can create a work item and associated to build through PowerShell with Rest API (add PowerShell step at the end of other steps and Check Always run option).

Associate work item to build:

Request Type: Patch

Request URL: https://[your vsts account].visualstudio.com/DefaultCollection/_apis/wit/workitems/[work item id]?api-version=1.0

Content-Type:application/json-patch+json

Body:

[
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "ArtifactLink",
      "url": "vstfs:///Build/Build/[build id]",
      "attributes": {
        "comment": "Making a new link for the dependency"
      }
    }
  }
]

You can refer to this blog for PowerShell scripts to create and associate work item.

Build association with work Items in vNext

Upvotes: 1

Luca Cappa
Luca Cappa

Reputation: 1999

In the Additional Fields you need to reference the build/release definition variable with the following notation: $(logContent)

as shown here below:

reference to build variable

Upvotes: 0

Related Questions