How to create Build Definitions through VSTS REST API

徘徊边缘 提交于 2019-12-06 03:17:17

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!