3

I'm trying to automate the deployment process, and as part of it, I need to run my release build from command line. I can do it, using command like

.\TFSBuild start http://server-name:8080/tfs/project-collection project-name build-name priority:High /queue

It even returns some code for the queued build — Build queued. Queue position: 2, Queue ID: 11057.

What I don't know, is how to get info about currently running builds, or about the state of my running build from powershell command line? The final aim is to start publishing after that build completes.

I've already got all necessary powershell scripts to create the deployment package from the build results, zip it, copy to production and install there. All I need now — to know when my build succeedes.

AndreyTS
  • 232
  • 2
  • 13

3 Answers3

5

This function will wait for a build with the Queue ID given by TFSBuild.exe:

function Wait-QueuedBuild {
    param(
        $QueueID
    )

    [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Build.Client')
    [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Client')

    $uri = [URI]"http://server-name:8080/tfs/project-collection"
    $projectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($uri)
    $buildServer = $projectCollection.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
    $spec = $buildServer.CreateBuildQueueSpec('*','*')

    do {
        $build = $buildServer.QueryQueuedBuilds($spec).QueuedBuilds| where {$_.Id -eq $QueueID}
        sleep 1
    } while ($build)
}

You can get the id returned by TFSBuild.exe, then call the function.

$tfsBuild = .\TFSBuild start http://server-name:8080/tfs/project-collection project-name build-name priority:High /queue
Wait-QueuedBuild [regex]::Match($tfsBuild[-1],'Queue ID: (?<id>\d+)').Groups['id'].Value
Rynant
  • 23,153
  • 5
  • 57
  • 71
1
    # load classes for execution
[Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") | Out-Null

# declare working variables
$Uri = New-Object System.Uri "http://example:8080/tfs"

# get reference to projection collection
$ProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($Uri)

# get reference to build server
$BuildServer = $ProjectCollection.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

# loop through the build servers
foreach($Controller in $BuildServer.QueryBuildControllers($true))
{
    # loop through agents
    foreach($BuildAgent in $Controller.Agents)
    {
        Write-Host "$($BuildAgent.Name) is $($BuildAgent.IsReserved)"
    }
}
Twerthi
  • 36
  • 4
1

Using the work by E.Hofman available here it is possible to write a C# console app that uses TFS SDK and reveals if any build agent is currently running as follows:

using System;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;

namespace ListAgentStatus
{
    class Program
    {
        static void Main()
        {
            TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://TFSServer:8080"));
            var buildServer = teamProjectCollection.GetService<IBuildServer>();

            foreach (IBuildController controller in buildServer.QueryBuildControllers(true))
            {
                foreach (IBuildAgent agent in controller.Agents)
                {
                    Console.WriteLine(agent.Name+" is "+agent.IsReserved);
                }
            }
        }
    }
}

The parameter .IsReserved is what toggles to 'True' during execution of a build.

I 'm sorry my powershell skills are not good enough for providing with a PS variant of the above. Please take a look here, where the work by bwerks might help you do that.

Community
  • 1
  • 1
pantelif
  • 8,524
  • 2
  • 33
  • 48