How to create an Azure DevOps Pipeline via RestAPI and enable

52 Views Asked by At

My project's code is on Github, and the CI/CD pipelines are defined in Azure DevOps. The idea is that when a new PR happens, it triggers the CI pipeline in Azure.

I've automated the creations of pipelines, using the Azure RestAPI, for that we are using: /_apis/pipelines version 7.1. I did it using this reference.

When registering it automatically through the API, the PRs are not triggering the CI pipeline; after some investigation, I found out that the trigger for PRs is, by default, disabled. As can be seen in the picture below.

Do you know any way to modify it via API, either by default enabling it or changing it after it's created?

enter image description here

1

There are 1 best solutions below

0
Alvin Zhao - MSFT On BEST ANSWER

You can Override the YAML the PR trigger and Enable pull request validation of a GitHub repo with this API.

Per the request body of above request, you may first get the latest pipeline definition revision from the response of the following API first.

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=7.2-preview.7

We can then copy the response as the body of the Definition-Update-API request and simply update the triggerS section. Here is a sample json contents of the modified trigger section for your reference. In the second object for "triggerType": "pullRequest", I removed "settingsSourceType": 2, and updated "branchFilters": ["+refs/heads/main"].

PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=7.2-preview.7

{
    …
    "triggers":  [
                         {
                             "branchFilters":  "",
                             "pathFilters":  "",
                             "settingsSourceType":  2,
                             "batchChanges":  false,
                             "maxConcurrentBuildsPerBranch":  1,
                             "triggerType":  "continuousIntegration"
                         },
                         {
    
                             "branchFilters":  [
                                "+refs/heads/main"
                             ],
                             "forks":  "@{enabled=False; allowSecrets=False; allowFullAccessToken=False}",
                             "pathFilters":  "",
                             "requireCommentsForNonTeamMembersOnly":  false,
                             "requireCommentsForNonTeamMemberAndNonContributors":  false,
                             "isCommentRequiredForPullRequest":  false,
                             "pipelineTriggerSettings":  "@{forkProtectionEnabled=True; buildsEnabledForForks=True; enforceJobAuthScopeForForks=False; enforceNoAccessToSecretsFromForks=False; isCommentRequiredForPullRequest=False; requireCommentsForNonTeamMembersOnly=False; requireCommentsForNonTeamMemberAndNonContributors=False}",
                             "triggerType":  "pullRequest"
                         }
                     ],
    …
}