如何仅当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:
We’ll use two tools to make this reliable:
gitto compare the current commit with the previous one (or the PR target branch)yq(a lightweight YAML parser) to safely extract thehdp.versionvalue, avoiding false positives from naive string matching
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
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
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
- 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_BRANCHenvironment variable. - YAML Safety: Using
yqensures we’re only checking the exacthdp.versionfield, not random strings that happen to match "version:".
内容的提问来源于stack exchange,提问作者vjancich

