You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何接收TFS的「Build queued」通知/警报?开发团队Slack机器人

Great question! The built-in SOAP alerts in TFS are indeed limited to post-completion events, but there are a few reliable ways to get build queue/start notifications for your Slack bot. Here are the most practical approaches:

1. Poll the TFS REST API for Queue/In-Progress Builds

This is the most straightforward method, since TFS's REST API exposes real-time build queue and status data. Here's how to implement it:

  • Build a lightweight service (e.g., a Node.js/Python script, or an Azure Function) that periodically calls TFS's build endpoints. Focus on two key filters: notStarted (queued builds) and inProgress (started builds).
  • Maintain a local state store (like in-memory cache, Redis, or a simple database) to track builds you've already notified on. Compare each poll's results against this state to detect new queued/started builds.
  • When a new event is detected, format the build details into a Slack-friendly message and send it via Slack's Incoming Webhook or API.
  • Example API call (adjust your TFS server URL and project name):
    GET https://your-tfs-server/DefaultCollection/YourProject/_apis/build/builds?statusFilter=notStarted,inProgress&api-version=6.0
    
    Extract fields like definition.name (build name), queueTime, startTime, and requestedBy.displayName to populate your Slack notification.

2. Use TFS Service Hooks for Event-Driven Notifications

TFS has a hidden gem called Service Hooks that lets you subscribe to more events than the default Alerts UI exposes—including build queue and start events. Here's how to set this up:

  • Navigate to your TFS project's Project Settings > Service Hooks > Create subscription.
  • Select Web Hooks as the target service, then choose the relevant trigger events:
    • For queued builds: Look for Build queued or Build requested (naming varies slightly by TFS version)
    • For started builds: Select Build started
  • Configure a webhook URL (you may need a small intermediate service here, since TFS's event payload format doesn't match Slack's expected format). This service will parse the TFS event data, convert it to Slack's block or markdown format, then forward it to your Slack bot/webhook.
  • This approach is more efficient than polling because it's event-driven—you get notified immediately when a build is queued or starts, no repeated API calls needed.

3. Leverage TFS Client SDKs or Command-Line Tools

If you prefer a code-first approach without dealing with REST APIs directly, you can use TFS's official SDKs to listen for build events:

  • For .NET developers, use the Microsoft.TeamFoundationServer.Client NuGet package to connect to your TFS server and subscribe to BuildQueuedEvent and BuildStartedEvent directly.
  • Example C# snippet to get started:
    using Microsoft.TeamFoundation.Build.WebApi;
    using Microsoft.VisualStudio.Services.Common;
    
    // Initialize connection to TFS (use a PAT token for authentication)
    var tfsUri = new Uri("https://your-tfs-server/DefaultCollection");
    var credentials = new VssBasicCredential("", "your-pat-token");
    var connection = new VssConnection(tfsUri, credentials);
    
    // Get build client to interact with build services
    var buildClient = connection.GetClient<BuildHttpClient>();
    
    // Implement event listening or periodic status checks here
    
  • Package this as a background service (e.g., a Windows Service or Docker container) to keep it running and listening for events.

Bonus: Formatting Slack Notifications

Make your alerts useful by formatting them with Slack's block kit. Here's an example payload for a queued build notification:

{
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "*New Build Queued*\nBuild Name: `MyApp-Prod-Build`\nQueued At: 2024-05-20 14:45:00\nRequested By: Jane Smith"
      }
    }
  ]
}

The desktop notification tools you've seen likely use one of these methods—either polling the API or subscribing to TFS events—to detect build state changes and push notifications.

内容的提问来源于stack exchange,提问作者flamey

火山引擎 最新活动