You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bamboo构建无法按预期触发失败的技术问询

Fixing Bamboo Build Failure Triggers for Your Dispatcher Instance Script

Hey there, let's tackle this issue head-on. The core problem here is that Bamboo relies entirely on your script's exit code to determine if a build failed. If your script always exits with 0 (the universal "success" code) even when things go sideways, Bamboo will just mark the build as successful—even if you intended it to fail.

Here's how to fix this step by step:

1. Add explicit exit codes for error scenarios

Right now, your script likely runs through its logic but doesn't signal errors to Bamboo. You need to make it return a non-zero exit code whenever something unexpected happens: like failing to query the target group, failing to remove an instance when it should, or hitting an unexpected number of instances.

Here's an adjusted script example with proper error handling:

#!/bin/bash

# Enable strict mode to catch common mistakes early
set -euo pipefail

# Replace these with your actual values
TARGET_GROUP_ARN="arn:aws:elbv2:us-east-1:123456789012:targetgroup/your-target-group/abc123"
DISPATCHER_INSTANCE_PREFIX="i-*dispatcher*"

# Query the number of active Dispatcher instances
INSTANCE_COUNT=$(aws elbv2 describe-target-health --target-group-arn "$TARGET_GROUP_ARN" \
    --query 'TargetHealthDescriptions[?starts_with(Target.Id, `'"$DISPATCHER_INSTANCE_PREFIX"'`)].Target.Id | length(@)' \
    --output text)

# Handle instance count logic with error checks
case $INSTANCE_COUNT in
    2)
        echo "Found 2 Dispatcher instances — removing one..."
        # Grab the first instance ID to deregister (adjust this logic if you need a specific instance)
        INSTANCE_TO_REMOVE=$(aws elbv2 describe-target-health --target-group-arn "$TARGET_GROUP_ARN" \
            --query 'TargetHealthDescriptions[?starts_with(Target.Id, `'"$DISPATCHER_INSTANCE_PREFIX"'`)].Target.Id | [0]' \
            --output text)
        
        # Attempt to remove the instance
        aws elbv2 deregister-targets --target-group-arn "$TARGET_GROUP_ARN" --targets Id="$INSTANCE_TO_REMOVE"
        
        # Exit with error if the deregister command fails
        if [ $? -ne 0 ]; then
            echo "ERROR: Failed to deregister instance $INSTANCE_TO_REMOVE"
            exit 1
        fi
        echo "Successfully removed one Dispatcher instance"
        exit 0
        ;;
    1)
        echo "Only 1 Dispatcher instance found — no action taken."
        # Keep this as exit 0 since you confirmed this is the intended "no-op" case
        exit 0
        ;;
    *)
        echo "ERROR: Unexpected number of Dispatcher instances: $INSTANCE_COUNT"
        # Trigger a build failure for unexpected counts
        exit 1
        ;;
esac

2. Enable strict mode in your script

I added set -euo pipefail at the top of the example — this is a huge help for catching errors early:

  • set -e: Makes the script exit immediately if any command fails
  • set -u: Treats unset variables as errors (catches typos in variable names)
  • set -o pipefail: Ensures that if any command in a pipeline fails, the whole pipeline returns an error code

This way, even a small mistake (like a typo in your AWS CLI command) will make the script exit with a non-zero code, which Bamboo will recognize as a build failure.

3. Verify your Bamboo task configuration

Double-check your Bamboo task settings to make sure it's set to react to non-zero exit codes:

  • Go to your task's configuration page
  • Look for options related to "failure conditions" or "exit code handling"
  • Confirm it's set to fail the build when the script returns a non-zero exit code (this is the default, but it's worth verifying)

4. Test the script locally first

Before pushing changes to Bamboo, run the script manually with different scenarios to confirm it returns the correct exit codes:

  • Test with 2 instances (should remove one and exit 0)
  • Test with 1 instance (should exit 0)
  • Test with 0 or 3 instances (should exit 1 to trigger a failure)
  • Test with an invalid target group ARN (should exit 1 thanks to strict mode)

After running the script, type echo $? to see the exit code — 0 means success, any other number means failure.

By making these adjustments, your script will properly communicate failures to Bamboo, so you'll get the build failure triggers you need when something goes wrong.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:52:46