Skip to content

Instantly share code, notes, and snippets.

@bcourtine
Forked from dgageot/git-build
Last active October 4, 2015 22:47
Show Gist options
  • Save bcourtine/2711212 to your computer and use it in GitHub Desktop.
Save bcourtine/2711212 to your computer and use it in GitHub Desktop.
Unbreakeable build
#!/bin/bash
# which silent mode (-s) is not supported on all systems.
function alert_user {
echo "${1}";
which growlnotify && growlnotify `basename $0` -m "${1}"
which notify-send && notify-send -u "${2}" "${1}" `basename $0`
}
function exit_ko {
alert_user "${1}" "critical"; exit 1
}
function exit_ok {
alert_user "${1}" "normal"; exit 0
}
BUILD_LOCATION="/tmp/build"
BUILD_CMD="mvn clean install -T2"
LOCATION=$(pwd)
REMOTE=${1:-origin}
REMOTE_URL=$(git remote show -n ${REMOTE} | awk '/Fetch/ {print $3}')
BRANCH=$(git symbolic-ref -q HEAD)
BRANCH=${BRANCH##refs/heads/}
# Git black magic to pull rebase even with uncommited work in progress
git fetch ${REMOTE}
git add -A; git ls-files --deleted -z | xargs -0 -I {} git rm {}; git commit -m "wip"
git rebase ${REMOTE}/${BRANCH}
if [ "$?" -ne 0 ]; then
git rebase --abort
git log -n 1 | grep -q -c "wip" && git reset HEAD~1
exit_ko "Unable to rebase. please pull or rebase and fix conflicts manually."
fi
git log -n 1 | grep -q -c "wip" && git reset HEAD~1
# Private build
rm -Rf ${BUILD_LOCATION}
git clone -slb "${BRANCH}" . ${BUILD_LOCATION}
cd ${BUILD_LOCATION}
# Build with maven
set MAVEN_OPTS=-Xmx1G
${BUILD_CMD}
if [ $? -ne 0 ]; then
exit_ko "Unable to build"
fi
# Push
git push ${REMOTE_URL} ${BRANCH}
if [ $? -ne 0 ]; then
exit_ko "Unable to push"
fi
# Update working directory
cd ${LOCATION} && git fetch ${REMOTE}
exit_ok "Yet another successful build!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment