简体   繁体   中英

Queueing TFS builds via PowerShell

TFS2012 with a one 2010 build controller with one 2010 build agent. Also have one 2012 build controller with multiple 2012 build agents.

We have multiple builds for multiple versions of our software. The builds are named according to a convention eg Foo_version_1_0 and Foo_version_2_0.

When I run this code on my local machine, all the builds are queued. When I run this code manually on a 2012 build agent, the builds are queued. When I run this code manually on a 2010 build agent, no builds are queued. When the code is executed as part of a triggered build in TFS (either on the 2010 or 2012 controller/agent), it doesn't queue any builds and errors out with my custom exception saying no definitions returned from TFS.

My questions:

Is the $buildServer.QueryBuildDefinitions() function an administrator function only? Ie if a non-admin user account (like TFSService) runs it, it won't be able to get the data from the TFS api?

Is the $buildServer.QueryBuildDefinitions() a new function that is only available in 2012?

Is there another way of doing this that will work? Previously, we had all our build names hard coded - this is not a viable way forward for us.

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

$serverName="http://tfs:8080/tfs"
$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
$buildserver = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

$buildServer.QueryBuildDefinitions("FooProject") | foreach {
if ($_.EndsWith("version_1_0"))
{
echo "Queueing build: $_.Name"
$buildServer.QueueBuild($buildServer.GetBuildDefinition("FooProject",$_.Name))
        }
    }
}

Edit: removed $buildDefinitions = $buildServer.QueryBuildDefinitions("FooProject").Name , replaced it with $buildServer.QueryBuildDefinitions("FooProject") | foreach... $buildServer.QueryBuildDefinitions("FooProject") | foreach...

Builds are now queued programmatically.

The API hasn't changed, and I suppose that both agents are using the same account.

The line

$buildDefinitions = $buildServer.QueryBuildDefinitions("FooProject").Name

seems wrong: the Name property get will raise an exception for an empty result.

You can also utilize one of the built in APIs, to prevent downloading dll binaries.

The following will work for

TFS 2017:

https://github.com/sameer-kumar/adhoc-posh/blob/master/QueueTfsBuild.ps1

$rootTfsUri = "http://myTFS:8080/tfs"
$collectionName = "Default"
$projectName = "Project1"
$tfsUri = $rootTfsUri + "/" + $collectionName + "/" + $projectName
$buildDefinition = "DevCI-vnext"
$buildDefinitionUri = "$tfsUri/_apis/build/definitions?api-version=3.1&name=$buildDefinition" 

# first get build definition id
$buildResponse = Invoke-WebRequest -Uri $buildDefinitionUri -UseDefaultCredentials -Method Get -Verbose -UseBasicParsing -ContentType "application/json" 
$buildResponseAsJson = $buildResponse.Content | convertfrom-json
$buildDefinitionId = $buildResponseAsJson.value.id

# Now queue this build definition
$requestContentString = @"
{
    "definition": {
        "id" : "$buildDefinitionId"
    }
}
"@

$buildUri = "$tfsUri/_apis/build/builds?api-version=3.1"
$buildResponse = Invoke-WebRequest -Uri $buildUri -UseDefaultCredentials -Method Post -Verbose -UseBasicParsing -ContentType "application/json" -Body $requestContentString
$buildNumber = ($buildResponse.Content | ConvertFrom-Json).buildNumber

TFS 2015

Utilizes a slightly different structure where uri definition replace with this,

$buildDefinitionUri = "$tfsUri/_apis/Build/builds?api-version=2.0&name=$buildDefinition" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM