TFS2018构建触发器配置咨询:如何限制每小时最多1次构建?
Absolutely, you can restrict your project to run at most one build per hour even with multiple frequent check-ins. Let’s break down the best approaches for your scenario:
1. Built-In TFS Trigger Configuration (Recommended)
Your current setup uses the CI trigger with "Batch changes when a build is in process" enabled, which merges check-ins that happen during an active build—but it won’t cap builds to once per hour on its own. Here’s how to adjust this:
- First, disable your existing CI trigger (set "Trigger status" to disabled).
- Add a Scheduled Trigger to your build definition:
- Set the schedule to run every 1 hour (e.g., "Hourly" recurrence, starting at the top of any hour).
- Check the box for "Build only if there are changes in the source".
This way, no matter how many check-ins happen in an hour, TFS will only run one build at your scheduled interval—and only if there are actual changes to build. It’s clean, no custom scripts required, and leverages TFS’s native functionality.
2. Agent-Level Project Build Limits
If you need to enforce limits via your Job Agent infrastructure, here are two practical methods:
a. Custom Pre-Build Script Check
Add a PowerShell task at the very start of your build definition to check if a successful build for this project ran in the last hour. If it did, terminate the current build early. Here’s a simplified script snippet:
$tfsUrl = "http://your-tfs-server/DefaultCollection/YourProject/_apis/build/builds" $buildDefId = "123" # Replace with your build definition ID $oneHourAgo = (Get-Date).AddHours(-1).ToString('o') $params = @{ Uri = "$tfsUrl?definitions=$buildDefId&statusFilter=completed&resultFilter=succeeded&minTime=$oneHourAgo" Method = "GET" Headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } } $recentBuilds = Invoke-RestMethod @params if ($recentBuilds.count -gt 0) { Write-Host "##vso[task.complete result=Canceled;]A successful build ran in the last hour. Skipping this build." exit 0 }
Make sure to enable "Allow scripts to access OAuth token" in your build definition’s "Agent job" settings for this to work.
b. Agent Pool Queue Concurrency + Tagging
While this doesn’t enforce a time-based limit directly, it can prevent concurrent builds and pair well with the scheduled trigger:
- Assign a unique tag (e.g.,
YourProject-Limited) to a single Job Agent in your pool. - In your build definition’s "Agent job" settings, set "Demands" to require this tag.
- Configure that agent to allow only 1 concurrent job (in the Agent Pool settings under "Capabilities").
This ensures only one build for your project can run at a time, and when combined with the hourly scheduled trigger, you’ll get your desired once-per-hour cap.
内容的提问来源于stack exchange,提问作者borjaps

