Search code examples
powershelltfstfsbuild

How to get list of TFS builds running at the moment from command line?


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.


Solution

  • 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