How to create Build Definitions through VSTS REST API

后端 未结 1 1908
渐次进展
渐次进展 2020-12-19 14:59

Situation:

I\'m currently working on automating CI/CD configurations through bitbucket -> vsts -> azure.

My ideal result is to be able to copy paste (or ma

相关标签:
1条回答
  • 2020-12-19 15:07

    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.

    0 讨论(0)
提交回复
热议问题