如何通过REST API创建Azure DevOps Pipeline并启用PR触发器
解决方案
可以通过Azure DevOps REST API实现两种方式启用PR触发器:
1. 创建Pipeline时直接启用PR触发器
调用/_apis/pipelines接口创建Pipeline时,在请求体中添加triggers字段,配置GitHub PR触发器并设置为启用状态。示例请求体核心部分如下:
{ "folder": "\\", "name": "MyGitHubPipeline", "configuration": { "type": "yaml", "path": "/azure-pipelines.yml", "repository": { "id": "你的GitHub仓库ID", "type": "GitHub", "name": "GitHub用户名/仓库名", "url": "https://github.com/用户名/仓库名.git", "defaultBranch": "main" }, "triggers": [ { "triggerType": "githubPullRequest", "enabled": true, "settings": { "branchFilters": ["+main", "+release/*"], "pathFilters": ["+/*"], "isCommentTriggerEnabled": true } } ] } }
其中:
triggerType指定为githubPullRequest适配GitHub PR场景enabled设为true直接启用触发器branchFilters定义需要触发的分支规则,+表示包含,-表示排除pathFilters可控制哪些路径的变更会触发Pipeline
2. 创建Pipeline后通过API启用PR触发器
如果已经创建了Pipeline,可通过PUT请求更新Pipeline配置:
请求路径:/_apis/pipelines/{pipelineId}?api-version=7.1
请求体同样包含上述triggers配置,覆盖原有触发器设置即可。
注意:操作前确保Azure DevOps项目已正确配置GitHub服务连接,且当前调用API的账号拥有Pipeline编辑权限。
内容的提问来源于stack exchange,提问作者Rodrigo Farias Rezino
相关产品推荐
相关产品推荐

