Let your build work for you, shorten the feedback loop

Tips and tricks Inline Powershell task VSTS, combine webhooks, rest api calls to get more out of your VSTS/TFS build and release pipelines

Maximize how you use your VSTS build and release pipeline with Inline Powershell tasks. In this blog series ‘Tips and Tricks for Inline Powershell’, I will show simple samples on how to get more out of your pipelines. This blog post: Let your build work for you

VSTS Inline Powershell task
The Inline PowerShell VSTS task enables you to execute PowerShell from a textbox within your build or release pipeline. You can run a PowerShell script on you agent or as Azure Powershell.
Introduction Inline Powershell Task
Install Inline Powershell Task

Let your build work for you
Shorten your feedback loop to fix a broken builds as soon as possible. Let the build give precise information on multiple channels that something broke. The sooner someone notices, the sooner it could be fixed. The following Powershell script combines variables, rest apis and webhook in one script. It searches for a failed task in the current build, if found it makes a workitem and than post the link to slack.

[String]$buildID = "$env:BUILD_BUILDID"
[String]$project = "$env:SYSTEM_TEAMPROJECT"

$headers = @{  Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"  }
 $url= $projecturi + $project + "/_apis/build/builds/" + $buildID + "/timeline?api-version=2.0"

$responseBuild = Invoke-RestMethod -Uri $url -headers $headers -Method Get | select records

foreach ($record in $responseBuild.records)
   if ($record.result -eq 'failed'){
     $urlwit= $projecturi + $project + '/_apis/wit/workitems/$bug?api-version=2.0'

        `"op`": `"add`",
        `"path`": `"/fields/System.Title`",
        `"value`": `"build $buildID failed by $buildrequestor `"

     $response= Invoke-RestMethod -Uri $urlwit  -ContentType "application/json-patch+json" -Body $body -headers $headers -Method PATCH   

      $wipurl = $response.url
      $buildrequestor=  "$env:Build_RequestedFor"
      $taskname = $record.name

      $payload = @{
        "channel" = "#random"
         "text" = "@$buildrequestor failed the build on task $taskname\n$wipurl"
         "username"= "build-bot"
         "icon_emoji"= ":speak_no_evil:"

     Invoke-WebRequest -UseBasicParsig `
      -Body (ConvertTo-Json -Compress -InputObject $payload) `
      -Method Post `
      -Uri "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"

More tips and tricks
Use VSTS variables
Let your task fail
Set progress
Change buildnumber
VSTS Command overview
Call a WebHook
Download a file
Install a Powershell Module
Navigate VSTS as filesystem
Make VSTS API Rest calls
Script example: Act on failed build

To access the VSTS Rest API you need to configure the OAuth token in the your build, see VSTS Rest API from Inline Powershell. More information on how to use the Webhook to Slack.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: