Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger a build in TFS 2015 using REST API

Tags:

rest

c#

tfs

I have TFS 2015 RC2 installed on-premise. I'm trying to use REST API to queue a build in a vNext definiton.

I'm using the code sample from VSO with slight modifications (mostly change of URL and authentication method to work with on-premise TFS).

There are two REST API calls I'm using.

The first is: GET http://mytfssrv:8080/tfs/DefaultCollection/myproject/_apis/build/definitions/

Which returns all specified project build definitions: build definition with ID 1, which is a XAML build definition I'm not interested to queue in and build definition with ID 2, which is vNext build definition - that's where I I want to queue my build

Note that I omitted the ?api-version=1.0 part - that's because if I don't, I get only the XAML build definition.

The second call is to queue a new build in the vNext build definition:

POST http://mytfssrv:8080/tfs/DefaultCollection/myptoject/_apis/build/requests?api-version=1.0

with the following data:

{"definition":{"id":**2**},"reason":"Manual","priority":"Normal","queuePosition":0,"queueTime":"0001-01-01T00:00:00","requestedBy":null,"id":0,"status":null,"url":null,"builds":null}

The response I get from the server is:

TF215016: The build definition 2 does not exist. Specify a valid build definition and try again.

I tried changing the API version, changing the post data in various ways but never succeeded.

Any idea how to cure TFS from its DID?

like image 285
kDar Avatar asked Jul 13 '15 05:07

kDar


People also ask

What is trigger build task in TFS?

Trigger Build Task. This build task enables the chaining of builds within TFS. It makes use of the built-in TFS API to queue a new build of any build definition (within the same Team Project or even across projects) and has support for different conditions if the Build should be triggered.

How to enable basic authentication on TFS instances?

On TFS Instances (2015 - 2018) this is located under the Options of the Build Definition. Otherwise your build task will fail. This option will work with Hosted Build Agents from VSTS. If you have enabled Basic Authentication, you can as well use this sort of authentication by providing the username and password.

Can I trigger a VSTs/ADO/TFS build from another team project?

If you want to trigger a build from a different Team Project but within the same VSTS/ADO/TFS instance you can still use OAuth Authentication. In any case if you trigger a build from a different team project, no matter if on the same or a different server instance, the following Advanced Configuration options shall not be enabled to prevent issues:

What are the team Foundation Server APIs based on?

The Team Foundation Server APIs are based on REST, JSON and service hooks - all standard web technologies broadly supported in the industry. Note The REST APIs documented here are for older versions of the APIs (TFS 2015 up to TFS 2018 Update 1).


1 Answers

This works like a charm without REST

var tfsurl = new Uri("http://localhost:8080/tfs/<***projectname***>/");    
var ttpc = new TfsTeamProjectCollection(tfsurl);
var bhc = ttpc.GetClient<BuildHttpClient>();
var builds = bhc.GetBuildsAsync("<***projectname***>").Result;
var build = builds
    .Where(x => x != null && x.Definition.Name.Equals("***buildDefinitionName***>"))
    .OrderByDescending(y => y.LastChangedDate)
    .FirstOrDefault();

bhc.QueueBuildAsync(build);
like image 166
Phil Landolt Avatar answered Sep 21 '22 06:09

Phil Landolt