Skip to content

Instantly share code, notes, and snippets.

@Asenar
Last active December 11, 2015 15:29
Show Gist options
  • Save Asenar/4621684 to your computer and use it in GitHub Desktop.
Save Asenar/4621684 to your computer and use it in GitHub Desktop.
[draft] deploy with gitolite. This is a draft, feel free to comment. This is not bug free (yet), some push may not work properly. Pushing tags produce an error (or something like that), and there is something by pushing new branches
#!/bin/bash
#
# file : serveur-prod:/home/git/scripts/deploy
# desc : This script deploy a pushed branch in web directory specified in conf/$repo-name.conf
#
# author : Michaël Marinetti
# creation : 2012-09-07
# modified : 2013-01-24
#
# LOG
#
# 2012-10-02
# now aborting if some files are not commited
# 2012-10-05
# better informations
# 2012-10-08
# add git show HEAD and git status --short --branch to show diverged information
# 2012-10-08
# rm git show, add git log -n1
# 2012-10-23
# now cancel if branch has diverged
# 2013-01-24
# change variable names
#
branch=$1
repo_name=$2
if (test -z "$repo_name" ) then
echo "repo_name is missing oO"
exit 1
fi
tmp_file_content=$(mktemp)
#################################
# list of updatable directories, relative to their branch
source `dirname "$0"`/conf/$repo_name.conf
if (test -n "${dirs[$branch]}" -o -n "${dirs['all']}") then
do_update=1
if (test -n "${dirs[$branch]}") then
dir=${dirs[$branch]}
echo "<p>path '$dir' will be updated.</p>" >> $tmp_file_content
else
dir=${dirs['all']}
echo "<p>path '$dir' will be updated if the branch correponds.</p>" >> $tmp_file_content
fi
else
do_update=0
echo "<p>No path to update has been found (either branch '$branch' is not configured, and 'all' param is empty).</p>" >> $tmp_file_content
fi
if (test -n "$dir") then
cd $dir
unset GIT_DIR
git_status_branch="$(git status --branch --short)"
fetch_origin=$(git fetch origin)
last_err=$?
behind_or_ahead="$(echo $git_status_branch|grep "ahead\|behind")"
git_status_branch_humanreadable="<p>git status --branch --short<br/>$git_status_branch</p>"
branch_to_update=$branch
current_branch=$(git symbolic-ref HEAD 2>/dev/null)
current_branch=${current_branch#refs/heads/}
if (test $last_err -ne 0) then
echo "<p style='color:red;font-weight:bold'>ERROR on git fetch origin</p>" >> $tmp_file_content
else
if (test "$current_branch" = "$branch_to_update") then
if (test -n "$fetch_origin" ) then
echo "<br/>fetch origin result: <br/>$fetch_origin</pre>" >> $tmp_file_content
fi
count_status=$(git st --porcelain|wc -l)
git_show_head="<p>git show HEAD</p><pre> $(git log -n1 --pretty=medium) </pre>"
if (test $count_status -gt 0) then
echo "<p style='color:orange;'>working directory is not clean. You need to commit all changes and remove untracked files.<br/><b>pull aborted</b></p>" >> $tmp_file_content
else
if (test -n "$behind_or_ahead") then
echo "<p style='color:red;font-weight:bold'>CANCELLED : branch has diverged</p>" >> $tmp_file_content
echo "$git_status_branch_humanreadable" >> $tmp_file_content
else
if (test $last_err -eq 0) then
git reset --hard origin/$branch > /dev/null
last_err=$?
if ( test $last_err -ne 0 ) then
echo "<p style='color:red;font-weight:bold'>ERROR on git reset --hard origin/$branch</p>" >> $tmp_file_content
else
echo "<p style='color:green;font-weight:bold'>MAJ OK</p>" >> $tmp_file_content
fi
fi
fi
fi
echo "<small>" >> $tmp_file_content
echo "$git_show_head" >> $tmp_file_content
echo "</small>" >> $tmp_file_content
else
echo "<p style='color:orange;font-weight:bold'>MAJ SKIPPED (branch used=$current_branch instead of $branch_to_update)</p>" >> $tmp_file_content
echo "<hr/>" >> $tmp_file_content
fi
fi
fi
cat $tmp_file_content
rm $tmp_file_content
#!/bin/bash
#
# put that file in : server-prod:/home/git/scripts/conf/repo-name.conf
declare -A dirs=( ["master"]=/home/www/prod ["all"]=/home/www/dev-or-whatever )
#!/bin/bash
#
# file : gitolite:/home/bin/repository/myrepo/hooks/post-receive
# desc : This hooks will update a git repository (with git pull) after you "git push"
# from your local workstation
#
# author : Michaël Marineti
# creation : 2012-09-21
# modified : 2013-01-24
#
# LOG
# 2012-09-26 : a branch other than master can be updated
# 2012-10-05 : improving things, with the remote scripts
# 2013-01-24 : rename variables / english speaking
#
# INSTALLATION
#
# localhost: local (developer)
# devhost: used for preprod and gitolite
#
# cd /home/www/dev.mywebsite.com
# git clone /home/git/repositories/mywebsite.com
#
# - put that file in /home/git/repositories/mywebsite.com/hooks/post-receive
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
# note michael : delete a branch have some problems
#remote: fatal: Invalid revision range 87187a0205f5a1d8d93bca73de40d69e8b338a99..0000000000000000000000000000000000000000
#remote: fatal: bad object 0000000000000000000000000000000000000000
# - [deleted] testvide
# send me reports
from=git@hostname.com
to=myself@hostname.com
read oldrev newrev refname
# use for choosing correct conf file on remote server
# TODO: make it automatic
repo_name=my-repo-name
branch=${refname#refs/heads/}
tmp_file_headers=`mktemp`
tmp_file_content=`mktemp`
echo "Analyze front ..."
tmp_content=$(ssh serveur-prod scripts/deploy $branch $repo_name)
echo "done."
echo "<h3>prod PUSH RESULT</h3>" >> $tmp_file_content
echo "$tmp_content" >> $tmp_file_content
echo "<hr/>" >> $tmp_file_content
################## SEND THAT EVERYTIME #############
echo "<h4>TECHNICAL INFOS</h4>" >> $tmp_file_content
echo "<p><b>$refname</b> has been updated (from <b>$oldrev</b> to <b>$newrev</b>) </p>" >> $tmp_file_content
echo "<p>commit log:<br/>" >> $tmp_file_content
if (test -n "$oldrev") then
git log --pretty=oneline --abbrev-commit $oldrev..$newrev | sed "s#\n#<br/>#" >> $tmp_file_content
fi
echo "</p>" >> $tmp_file_content
echo "<p>last commit details: <pre>" >> $tmp_file_content
git show $newrev --oneline | sed "s#\n#<br/>#" >> $tmp_file_content
echo "</pre></p>" >> $tmp_file_content
######################################################
### mail content ###
subject="[DEPLOY $repo_name $branch] `date` - push result "
### mail headers ###
echo "From:$from" >> $tmp_file_headers
echo "To:$to" >> $tmp_file_headers
echo "Subject:$subject" >> $tmp_file_headers
echo "Content-Type: text/html; charset=UTF-8" >> $tmp_file_headers
mail_content="$(cat $tmp_file_headers $tmp_file_content)"
echo "$mail_content" | sendmail -t
rm $tmp_file_content $tmp_file_headers
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment