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

如何实现Git/Bitbucket特定文件未提交变更时TeamCity构建控制

Solution: Control TeamCity Build Triggers and Abort Logic for Pending Changes

Let's tackle both of your requirements with practical scripts and TeamCity configuration steps tailored for Git/Bitbucket:

Scenario 1: Prevent TeamCity Build Trigger When Specific Files/Folders Have Pending Changes

This is about stopping the build from even starting when the targeted files have uncommitted changes. We'll use a custom script in TeamCity's trigger conditions to block the trigger entirely.

Implementation Steps:

  1. Navigate to your TeamCity build configuration, go to Triggers, and add a VCS Trigger (or edit an existing one).
  2. Under Trigger Conditions, select Custom script condition.
  3. Paste this Git-compatible script (adjust the blocked paths to match your needs):
# Fetch list of files with pending changes
CHANGED_FILES=$(git diff --name-only HEAD)

# Define files/folders that should block the build trigger
BLOCKED_PATHS=("src/local-config/" ".env" "dev-only-scripts/")

# Check if any blocked path appears in the changed files
for path in "${BLOCKED_PATHS[@]}"; do
  if echo "$CHANGED_FILES" | grep -q "$path"; then
    echo "Build blocked: Pending changes detected in $path"
    exit 1 # Non-zero exit tells TeamCity to skip triggering the build
  fi
done

exit 0 # Allow build trigger if no blocked changes are found

For Bitbucket using Mercurial, replace the git diff line with:

CHANGED_FILES=$(hg status --modified --added --removed | awk '{print $2}')

Scenario 2: Abort Scheduled Builds When Specific Files Have Pending Changes

Here, the build runs on a fixed schedule, but we need to abort it immediately if targeted files have uncommitted changes. We'll add a pre-build script that checks for changes and stops the build if needed.

Implementation Steps:

  1. In your TeamCity build configuration, add a Command Line Build Step (make sure it's the first step in your build chain).
  2. Set the Execution Condition to Always run so it checks for changes regardless of prior step status.
  3. Use this script to check for changes and abort the build:
TARGET_FILE="deploy/prod-secrets.yaml"

# Check if the target file has pending changes
if git diff --name-only HEAD | grep -q "$TARGET_FILE"; then
  # Send TeamCity service message to mark build as failed and abort
  echo "##teamcity[buildStatus status='FAILURE' text='Build aborted: Pending changes found in $TARGET_FILE']"
  exit 1 # Abort the build process immediately
fi

exit 0 # Proceed with subsequent build steps if no changes are found
  1. To set up the scheduled run: Go to Triggers > Add Scheduled Trigger, then configure your desired frequency (e.g., daily at 2 AM).

Key Notes:

  • Ensure your build agents have Git/Mercurial installed, and the TeamCity service account has permissions to run VCS commands.
  • You can expand the scripts to check multiple files by using an array (like in Scenario 1) instead of a single TARGET_FILE.
  • If you need to check remote pending changes (e.g., unmerged Bitbucket PRs), you can extend the script to call the Bitbucket API, but most use cases focus on local workspace changes.

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

火山引擎 最新活动