The format for the REST API to create a build definition as below:
POST https://{account}.visualstudio.com/{project}/_apis/build/definitions?api-version=5.0-preview.6
application/json example:
{
"process": {
"phases": [
{
"steps": [
],
"name": "Phase 1",
"refName": "Phase_1",
"condition": "succeeded()",
"target": {
"executionOptions": {
"type": 0
},
"allowScriptsAuthAccessOption": false,
"type": 1
},
"jobAuthorizationScope": "projectCollection",
"jobCancelTimeoutInMinutes": 1
}
],
"type": 1
},
"repository": {
"properties": {
"cleanOptions": "0",
"labelSources": "0",
"labelSourcesFormat": "$(build.buildNumber)",
"reportBuildStatus": "true",
"gitLfsSupport": "false",
"skipSyncSource": "false",
"checkoutNestedSubmodules": "false",
"fetchDepth": "0"
},
"id": "4ba24767-e5a6-4987-80cc-ebaeca01fdbc",
"type": "TfsGit",
"name": "product1",
"url": "https://marinaliu.visualstudio.com/Git2/_git/product1",
"defaultBranch": "refs/heads/master",
"clean": "false",
"checkoutSubmodules": false
},
"processParameters": {},
"drafts": [],
"queue": {
"id": 324,
"name": "ownPC",
"pool": {
"id": 23,
"name": "ownPC"
}
},
"name": "definitionCreatedByRESTAPI",
"type": "build",
"queueStatus": "enabled"
}
To use the REST API in C#, you can convert as below:
var personalaccesstoken = "PAT";
var base64Token = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{personalaccesstoken}"));
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Token);
var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://account.visualstudio.com/project/_apis/build/definitions?api-version=5.0-preview.6");
requestMessage.Content = new StringContent("{"process": { "phases": [{"steps": [], "name": "Phase 1","refName": "Phase_1","condition": "succeeded()","target": { "executionOptions": { "type": 0 },"allowScriptsAuthAccessOption": false, "type": 1 }, "jobAuthorizationScope": "projectCollection", "jobCancelTimeoutInMinutes": 1 }],"type": 1 }, "repository": { "properties": { "cleanOptions": "0","labelSources": "0","labelSourcesFormat": "$(build.buildNumber)", "reportBuildStatus": "true","gitLfsSupport": "false", "skipSyncSource": "false","checkoutNestedSubmodules": "false", "fetchDepth": "0"},"id": "4ba24767-e5a6-4987-80cc-ebaeca01fdbc","type": "TfsGit","name": "product1", "url": "https://marinaliu.visualstudio.com/Git2/_git/product1", "defaultBranch": "refs/heads/master", "clean": "false","checkoutSubmodules": false },"processParameters": {}, "drafts": [],"queue": { "id": 324, "name": "ownPC","pool": {"id": 23, "name": "ownPC"}}, "name": "definitionCreatedByRESTAPI", "type": "build","queueStatus": "enabled"}", Encoding.UTF8, "application/json");
HttpResponseMessage response = client.SendAsync(requestMessage).Result;
response.EnsureSuccessStatusCode();
By referring the blog Accessing TFS/VSTS 2017 programmatically for the C# program.