如何编程创建Webhook检测ADO仓库代码推送及Azure Function实现方案
Azure Function示例:处理Azure DevOps代码推送事件并转发至Service Bus队列
前置准备
- 给每个需要监控的Azure DevOps项目仓库配置Service Hook,选择「代码推送」事件,将触发地址指向你的Azure Function HTTP端点。
- 为Azure Function配置Azure Service Bus的访问权限:可使用Managed Identity(推荐)或直接在应用设置中添加Service Bus连接字符串
ServiceBusConnectionString。
C# HTTP触发Function示例代码
using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Azure.Messaging.ServiceBus; namespace DevOpsPushToServiceBus { public static class CodePushHandler { [FunctionName("CodePushHandler")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { // 处理Azure DevOps Service Hook的验证请求(GET) if (req.Method == HttpMethods.Get) { var validationToken = req.Query["validationToken"]; return new OkObjectResult(validationToken); } log.LogInformation("Received Azure DevOps code push event."); // 读取Service Hook的POST请求体 string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic payload = JsonConvert.DeserializeObject(requestBody); try { // 提取核心事件信息 string projectName = payload.resourceContainers.project.name; string repoName = payload.resource.repository.name; string pushId = payload.resource.pushId; string pusher = payload.resource.pusher.name; // 遍历所有提交,收集变更文件详情 var commitFiles = new List<object>(); foreach (var commit in payload.resource.commits) { foreach (var item in commit.changes) { commitFiles.Add(new { CommitId = commit.commitId, CommitMessage = commit.comment, FilePath = item.item.path, ChangeType = item.changeType }); } } // 整理要发送到Service Bus的消息内容 var messageContent = new { EventType = "CodePush", ProjectName = projectName, RepoName = repoName, PushId = pushId, Pusher = pusher, Timestamp = DateTime.UtcNow, ChangedFiles = commitFiles }; // 发送消息到Service Bus队列 await SendToServiceBus(JsonConvert.SerializeObject(messageContent), log); return new OkObjectResult("Event processed and message sent to Service Bus."); } catch (Exception ex) { log.LogError(ex, "Failed to process code push event."); return new BadRequestObjectResult("Error processing event: " + ex.Message); } } private static async Task SendToServiceBus(string messageBody, ILogger log) { string connectionString = Environment.GetEnvironmentVariable("ServiceBusConnectionString"); string queueName = Environment.GetEnvironmentVariable("ServiceBusQueueName"); await using var client = new ServiceBusClient(connectionString); ServiceBusSender sender = client.CreateSender(queueName); var message = new ServiceBusMessage(messageBody); await sender.SendMessageAsync(message); log.LogInformation("Message sent to Service Bus queue: {QueueName}", queueName); } } }
配置说明
- 应用设置:在Azure Function的应用设置中添加两个配置项:
ServiceBusConnectionString:Azure Service Bus的连接字符串ServiceBusQueueName:目标队列的名称
- NuGet依赖:需要安装以下NuGet包:
Microsoft.Azure.WebJobs.Extensions.Http Newtonsoft.Json Azure.Messaging.ServiceBus - 多项目适配:只需给每个目标项目的仓库重复配置Service Hook,指向同一个Function端点即可实现多项目监控。
内容的提问来源于stack exchange,提问作者Neo
相关产品推荐
相关产品推荐

