I have a mono repo in which there are 13 spring boot microservices and whenever commits are pushed, the bitbucket pipeline runs checkstyles jobs in the pipeline that run for each microservice.
We intend to optimize this behavior by determining the base commit of the current feature branch once we get the current commit hash and base commit hash using git diff we can determine, which microservices files have changed and hence only run check styles in those microservices resulting in optimization.
I tried various commands to determine the parent branch and extract base commit and some of them worked in the local git repo but none of them worked for me in the bitbucket pipeline
unless I checkout the dev branch inside the bitbucket pipeline in case the branch parent is dev
we want a dynamic behavior
1-Determine parent branch name
2-Determine base commit
3-Perform git diff
this is the command that gives me the parent branch name in the local but not bitbucket unless I checkout to the dev branch in the pipeline
git show-branch -a 2>/dev/null | grep '\*' | grep -v "$BITBUCKET_BRANCH" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'
The complete pipeline is
image: gradle:7.6
pipelines:
branches:
"**":
- step:
name: Detect Changed Microservices
caches:
- gradle
script:
- git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
- git fetch --all
- echo $BITBUCKET_COMMIT
- echo $BITBUCKET_BRANCH
- echo $BITBUCKET_PR_DESTINATION_BRANCH
- git checkout dev
- git show-branch -a 2>/dev/null | grep '\*' | grep -v "$BITBUCKET_BRANCH" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'
- PARENT_BRANCH=$(git show-branch -a 2>/dev/null | grep '\*' | grep -v "$BITBUCKET_BRANCH" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//')
- echo $PARENT_BRANCH
- START_COMMIT=$(git merge-base HEAD "$PARENT_BRANCH")
- echo $START_COMMIT
- git checkout $BITBUCKET_BRANCH
- CHANGED_MICROSERVICES=$(git diff --name-only $START_COMMIT $BITBUCKET_COMMIT | cut -d'/' -f1 | sort | uniq)
- echo $CHANGED_MICROSERVICES
- echo "$CHANGED_MICROSERVICES" > CHANGED_MICROSERVICES.txt
artifacts:
- CHANGED_MICROSERVICES.txt
- parallel:
- step:
name: Run Checkstyles
caches:
- gradle
script:
- echo "Running Checkstyles for changed microservices"
- if grep -qw "auth-service" CHANGED_MICROSERVICES.txt; then
gradle auth-service:checkstyleMain auth-service:checkstyleTest;
fi
- if grep -qw "banking-engine" CHANGED_MICROSERVICES.txt; then
gradle banking-engine:checkstyleMain banking-engine:checkstyleTest;
fi
- if grep -qw "common" CHANGED_MICROSERVICES.txt; then
gradle common:checkstyleMain common:checkstyleTest;
fi
- if grep -qw "config-server" CHANGED_MICROSERVICES.txt; then
gradle config-server:checkstyleMain config-server:checkstyleTest;
fi
- if grep -qw "discovery-server" CHANGED_MICROSERVICES.txt; then
gradle discovery-server:checkstyleMain discovery-server:checkstyleTest;
fi
- if grep -qw "gateway" CHANGED_MICROSERVICES.txt; then
gradle gateway:checkstyleMain gateway:checkstyleTest;
fi
- if grep -qw "invoicing-service" CHANGED_MICROSERVICES.txt; then
gradle invoicing-service:checkstyleMain invoicing-service:checkstyleTest;
fi
- if grep -qw "kafka-service" CHANGED_MICROSERVICES.txt; then
gradle kafka-service:checkstyleMain kafka-service:checkstyleTest;
fi
- if grep -qw "notification-service" CHANGED_MICROSERVICES.txt; then
gradle notification-service:checkstyleMain notification-service:checkstyleTest;
fi
- if grep -qw "payment-card-service" CHANGED_MICROSERVICES.txt; then
gradle payment-card-service:checkstyleMain payment-card-service:checkstyleTest;
fi
- if grep -qw "private-gateway" CHANGED_MICROSERVICES.txt; then
gradle private-gateway:checkstyleMain private-gateway:checkstyleTest;
fi
- if grep -qw "rules-engine" CHANGED_MICROSERVICES.txt; then
gradle rules-engine:checkstyleMain rules-engine:checkstyleTest;
fi
- if grep -qw "third-party-config-service" CHANGED_MICROSERVICES.txt; then
gradle third-party-config-service:checkstyleMain third-party-config-service:checkstyleTest;
fi
- if grep -qw "transaction-control" CHANGED_MICROSERVICES.txt; then
gradle transaction-control:checkstyleMain transaction-control:checkstyleTest;
fi
- if grep -qw "transaction-map" CHANGED_MICROSERVICES.txt; then
gradle transaction-map:checkstyleMain transaction-map:checkstyleTest;
fi
- if grep -qw "users-engine" CHANGED_MICROSERVICES.txt; then
gradle users-engine:checkstyleMain users-engine:checkstyleTest;
fi
artifacts:
- CHANGED_MICROSERVICES.txt
- step:
name: Run Tests
caches:
- gradle
script:
- echo "Running Tests for changed microservices"
- if grep -qw "auth-service" CHANGED_MICROSERVICES.txt; then
gradle auth-service:test;
fi
- if grep -qw "banking-engine" CHANGED_MICROSERVICES.txt; then
gradle banking-engine:test;
fi
- if grep -qw "common" CHANGED_MICROSERVICES.txt; then
gradle common:test;
fi
- if grep -qw "config-server" CHANGED_MICROSERVICES.txt; then
gradle config-server:test;
fi
- if grep -qw "discovery-server" CHANGED_MICROSERVICES.txt; then
gradle discovery-server:test;
fi
- if grep -qw "gateway" CHANGED_MICROSERVICES.txt; then
gradle gateway:test;
fi
- if grep -qw "invoicing-service" CHANGED_MICROSERVICES.txt; then
gradle invoicing-service:test;
fi
- if grep -qw "kafka-service" CHANGED_MICROSERVICES.txt; then
gradle kafka-service:test;
fi
- if grep -qw "notification-service" CHANGED_MICROSERVICES.txt; then
gradle notification-service:test;
fi
- if grep -qw "payment-card-service" CHANGED_MICROSERVICES.txt; then
gradle payment-card-service:test;
fi
- if grep -qw "private-gateway" CHANGED_MICROSERVICES.txt; then
gradle private-gateway:test;
fi
- if grep -qw "rules-engine" CHANGED_MICROSERVICES.txt; then
gradle rules-engine:test;
fi
- if grep -qw "third-party-config-service" CHANGED_MICROSERVICES.txt; then
gradle third-party-config-service:test;
fi
- if grep -qw "transaction-control" CHANGED_MICROSERVICES.txt; then
gradle transaction-control:test;
fi
- if grep -qw "transaction-map" CHANGED_MICROSERVICES.txt; then
gradle transaction-map:test;
fi
- if grep -qw "users-engine" CHANGED_MICROSERVICES.txt; then
gradle users-engine:test;
fi
artifacts:
- CHANGED_MICROSERVICES.txt
- step:
name: Run Builds
caches:
- gradle
script:
- echo "Running Builds for changed microservices"
- if grep -qw "auth-service" CHANGED_MICROSERVICES.txt; then
gradle auth-service:build;
fi
- if grep -qw "banking-engine" CHANGED_MICROSERVICES.txt; then
gradle banking-engine:build;
fi
- if grep -qw "common" CHANGED_MICROSERVICES.txt; then
gradle common:build;
fi
- if grep -qw "config-server" CHANGED_MICROSERVICES.txt; then
gradle config-server:build;
fi
- if grep -qw "discovery-server" CHANGED_MICROSERVICES.txt; then
gradle discovery-server:build;
fi
- if grep -qw "gateway" CHANGED_MICROSERVICES.txt; then
gradle gateway:build;
fi
- if grep -qw "invoicing-service" CHANGED_MICROSERVICES.txt; then
gradle invoicing-service:build;
fi
- if grep -qw "kafka-service" CHANGED_MICROSERVICES.txt; then
gradle kafka-service:build;
fi
- if grep -qw "notification-service" CHANGED_MICROSERVICES.txt; then
gradle notification-service:build;
fi
- if grep -qw "payment-card-service" CHANGED_MICROSERVICES.txt; then
gradle payment-card-service:build;
fi
- if grep -qw "private-gateway" CHANGED_MICROSERVICES.txt; then
gradle private-gateway:build;
fi
- if grep -qw "rules-engine" CHANGED_MICROSERVICES.txt; then
gradle rules-engine:build;
fi
- if grep -qw "third-party-config-service" CHANGED_MICROSERVICES.txt; then
gradle third-party-config-service:build;
fi
- if grep -qw "transaction-control" CHANGED_MICROSERVICES.txt; then
gradle transaction-control:build;
fi
- if grep -qw "transaction-map" CHANGED_MICROSERVICES.txt; then
gradle transaction-map:build;
fi
- if grep -qw "users-engine" CHANGED_MICROSERVICES.txt; then
gradle users-engine:build;
fi
artifacts:
- CHANGED_MICROSERVICES.txt