Last active
January 19, 2023 11:10
-
-
Save corneil/20585cb944615abb434063b8507d2d8d to your computer and use it in GitHub Desktop.
Determine changed modules in multi-module Maven Project in GitHub Actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function itemInModules() { | |
local e | |
for e in ${MODULES}; do | |
if [[ "$e" == "$ITEM" ]]; then | |
echo "1" | |
return 0 | |
fi | |
done | |
echo "0" | |
} | |
function addItem() { | |
if [ "$MODULES" == "" ]; then | |
echo "$1" | |
else | |
echo "$MODULES $1" | |
fi | |
} | |
# This will only work in a github workflow trigger by a pull request. | |
PR=$(echo "${{ github.ref_name }}" | grep -o '[[:digit:]]*') | |
gh pr checkout $PR | |
CHANGED=$(gh pr diff --name-only) | |
ALL_MODULES=$(find . -name "pom.xml" -type f -exec dirname '{}' \; | sed 's/\.\///' | sort -r) | |
MODULES= | |
for file in $MODIFIED; do | |
FILE=$(realpath $file) | |
echo "$file was changed" | |
for ITEM in $ALL_MODULES; do | |
if [[ "$ITEM" != "." ]] && [[ "$file" == *"$ITEM"* ]]; then | |
echo "Matched $ITEM" | |
HAS_ITEM=$(itemInModules) | |
if ((HAS_ITEM == 0)); then | |
MODULES=$(addItem "$ITEM") | |
fi | |
break | |
fi | |
done | |
done | |
# you can export MODULES or use it here to build to modified modules. -amd ensures that dependent modules are also built. | |
for module in $MODULES; do | |
./mvnw -pl "$ITEM" install -amd | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment