Skip to content

Instantly share code, notes, and snippets.

@corneil
Last active January 19, 2023 11:10
Show Gist options
  • Save corneil/20585cb944615abb434063b8507d2d8d to your computer and use it in GitHub Desktop.
Save corneil/20585cb944615abb434063b8507d2d8d to your computer and use it in GitHub Desktop.
Determine changed modules in multi-module Maven Project in GitHub Actions
#!/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