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

如何仅当config.yml中hdp.version配置变更时触发Bitbucket Pipeline执行AWS CLI操作

Absolutely! You can pull this off with a bash script in your Bitbucket Pipeline—no fancy native features required. The key is to check if the specific hdp.version field in config.yml was modified in the commit, then trigger the AWS CLI only if it was. Here's a step-by-step solution:

Solution Overview

We’ll use two tools to make this reliable:

  • git to compare the current commit with the previous one (or the PR target branch)
  • yq (a lightweight YAML parser) to safely extract the hdp.version value, avoiding false positives from naive string matching
1. Create a Bash Script to Check for YAML Field Changes

First, create a script (e.g., check_hdp_version.sh) in your repo root. This script will compare the hdp.version value between the current commit and the prior state:

#!/bin/bash
set -euo pipefail

# Define our target file and YAML key
YAML_FILE="config.yml"
TARGET_KEY=".hdp.version"

# Install yq if not present (adjust for your image's package manager if needed)
if ! command -v yq &> /dev/null; then
  echo "Installing yq..."
  wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq
  chmod +x /usr/bin/yq
fi

# Get the current version from the commit being tested
CURRENT_VERSION=$(yq e "$TARGET_KEY" "$YAML_FILE")

# Get the version from the reference commit (handles both direct commits and PRs)
if [ -n "${BITBUCKET_PR_DESTINATION_BRANCH:-}" ]; then
  # For pull requests: compare against the target branch
  git fetch origin "$BITBUCKET_PR_DESTINATION_BRANCH"
  PREVIOUS_VERSION=$(yq e "$TARGET_KEY" <(git show origin/"$BITBUCKET_PR_DESTINATION_BRANCH":"$YAML_FILE"))
else
  # For direct commits: compare against the previous commit
  PREVIOUS_VERSION=$(yq e "$TARGET_KEY" <(git show HEAD~1:"$YAML_FILE"))
fi

# Compare versions and take action
if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
  echo "✅ hdp.version changed from $PREVIOUS_VERSION to $CURRENT_VERSION. Running AWS CLI operations..."
  # Add your AWS CLI commands here (e.g., deploy, update configs)
  aws s3 sync ./build s3://your-bucket/hdp-$CURRENT_VERSION
  exit 0
else
  echo "ℹ️ no relevant changes pushed"
  exit 1
fi
2. Update Your Bitbucket Pipeline Config

Next, update your bitbucket-pipelines.yml to run this script. We’ll also add a condition to skip the step entirely if config.yml wasn’t modified (to save resources):

pipelines:
  default:
    - step:
        name: Check hdp.version & Run AWS CLI
        image: ubuntu:22.04 # Use an image that supports apt/git/wget
        script:
          # Install dependencies: git, curl, unzip (for AWS CLI)
          - apt-get update && apt-get install -y git curl unzip wget
          # Install AWS CLI v2
          - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
          - unzip awscliv2.zip
          - ./aws/install
          # Make our script executable and run it
          - chmod +x check_hdp_version.sh
          - ./check_hdp_version.sh
        # Optional: Skip this step if config.yml wasn't touched (saves time)
        condition:
          changesets:
            includePaths:
              - "config.yml"
        # Add AWS credentials as pipeline variables (never hardcode!)
        variables:
          AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
          AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
          AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
Alternative: String Matching (Less Reliable)

If you can’t use yq for some reason, you can use git diff with grep—but be warned, this can trigger false positives if other version: fields exist in config.yml:

#!/bin/bash
# Only use this if yq isn't an option!
git diff HEAD~1 HEAD -- config.yml | grep -E '^(\+|-)\s*version:' | grep -A1 -B1 'hdp:' | grep -v '^--'
if [ $? -eq 0 ]; then
  echo "hdp.version changed. Running AWS CLI..."
  aws your-command-here
  exit 0
else
  echo "no relevant changes pushed"
  exit 1
fi
Key Notes
  • AWS Credentials: Store your AWS keys as secure variables in Bitbucket (never commit them to code).
  • PR Support: The script handles both direct branch commits and pull requests by checking the BITBUCKET_PR_DESTINATION_BRANCH environment variable.
  • YAML Safety: Using yq ensures we’re only checking the exact hdp.version field, not random strings that happen to match "version:".

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 19:57:38