Skip to content

Instantly share code, notes, and snippets.

@fabiomontefuscolo
Created December 3, 2015 09:36
Show Gist options
  • Save fabiomontefuscolo/301bfc3054a9cbafa03a to your computer and use it in GitHub Desktop.
Save fabiomontefuscolo/301bfc3054a9cbafa03a to your computer and use it in GitHub Desktop.
Script that update git repositories and run make build when someone access port 1500
#!/bin/bash
PORT=1500
PROGRAM=$(readlink -f $0)
GIT=$(which git)
NCAT=$(which ncat)
MAKE=$(which make)
SLACK_URL="https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
SLACK_BOT_NAME="Tripa-Seca"
SLACK_CHANNEL="#chapolin"
REPOS=(
"/srv/www/working_copy1"
"/srv/www/working_copy2"
)
git_pull() {
report="";
for repo in "${REPOS[@]}";
do
gitreset=$($GIT -C $repo reset HEAD --hard 2>&1);
gitout=$($GIT -C $repo pull --no-stat 2>&1);
branch=$($GIT -C $repo rev-parse --abbrev-ref HEAD);
commit=$($GIT -C $repo rev-parse --short HEAD);
report="$report>> $repo\n";
report="$report>> DISCARDING LOCAL CHANGES: $gitreset\n";
report="$report============== ($branch - $commit)\n";
report="$report$gitout\n";
report="$report\n";
if [[ ! "$gitout" =~ "Already up-to-date" ]] && [ -s "$repo/Makefile" ] && grep -q '^build:' "$repo/Makefile";
then
report="$report>> Runing make build\n--------------\n";
build=$($MAKE -C $repo build 2>&1);
report="$report $build"
report="$report\n";
fi
echo "$branch - $commit" > $repo/src/revision.txt;
done
length=$(echo -e "$report" | wc -m)
echo "HTTP/1.1 200 OK";
echo "Content-Type: text/plain; charset=utf-8";
echo "Content-Length: $length";
echo "";
echo -e "$report";
echo -e "$report" 1>&2;
if [ -n "$SLACK_URL" ];
then
curl -X POST --data-urlencode "payload={\"channel\": \"$SLACK_CHANNEL\", \"text\": \"$report\", \"username\": \"$SLACK_BOT_NAME\" }" "$SLACK_URL" 1>&2;
fi;
}
run_server() {
ok=1;
while test "$ok";
do
$NCAT -l -p $PORT -e "$PROGRAM git_pull $1";
ok=$?;
done;
}
if test "$1" == "git_pull";
then
git_pull;
else
run_server;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment