Last active
July 12, 2018 10:31
-
-
Save danixland/3195eef72f92209dbecb6f4fec37e02b to your computer and use it in GitHub Desktop.
script to create a bare git repository and fill it up with some settings to allow git to send notification emails as well as deploy the code. Save it inside the git-shell-commands directory as "create"
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 | |
# usage: create <PROJECT> - create a git bare repository named PROJECT.git | |
# this command will setup the repo and send a mail for confirmation. | |
GITDIR="/var/git" | |
MULTIMAIL="/usr/doc/git-2.14.4/contrib/hooks/multimail/git_multimail.py" | |
GITUSER="git" | |
GITGRP="git" | |
function is_bare() { | |
repodir=$1 | |
if "$(git --git-dir="$repodir" rev-parse --is-bare-repository)" = true | |
then | |
true | |
else | |
false | |
fi | |
} | |
function git_init() { | |
PROJECT=$1 | |
echo "creating project \"${PROJECT}.git\"" | |
if [ ! -d ${GITDIR}/${PROJECT}.git ]; then | |
mkdir ${GITDIR}/${PROJECT}.git | |
fi | |
cd ${GITDIR}/${PROJECT}.git | |
git init --bare | |
mkdir custom-hooks | |
ln -s $MULTIMAIL custom-hooks/ | |
touch hooks/post-receive | |
cat > hooks/post-receive <<EOPR | |
#!/bin/sh | |
/usr/bin/pee ${GITDIR}/${PROJECT}.git/custom-hooks/deploy.sh \ | |
${GITDIR}/${PROJECT}.git/custom-hooks/git_multimail.py | |
EOPR | |
cat >> config <<EOT | |
[multimailhook] | |
mailer = "sendmail" | |
refchangeShowGraph = true | |
mailingList = "receiver@someemail.tld" | |
commitEmailFormat = "html" | |
htmlInIntro = true | |
htmlInFooter = true | |
from = "sender@someemail.tld" | |
administrator = "admin@someemail.tld" | |
quiet = true | |
logFile = "/var/log/multimail.log" | |
errorLogFile = "/var/log/multimail_err.log" | |
EOT | |
touch custom-hooks/deploy.sh | |
cat > custom-hooks/deploy.sh <<EODP | |
#!/bin/bash | |
# Directory where to deploy files from repository | |
DPTARGET="" | |
# Directory containing repository | |
DPGIT_DIR="${GITDIR}/${PROJECT}.git" | |
# Branch that is going to be deployed to server | |
DPBRANCH="master" | |
while read oldrev newrev ref | |
do | |
# if DPTARGET is empty we don't want deploy for this project | |
if [[ ! "" == \$DPTARGET ]]; then | |
# let's check that we are deploying to the correct branch | |
if [[ \$ref = refs/heads/\${DPBRANCH} ]]; then | |
echo "Ref \$ref received. Deploying \${DPBRANCH} branch to production..." | |
git --work-tree=\$DPTARGET --git-dir=\$DPGIT_DIR checkout -f $DPBRANCH | |
NOW=\$(date +"%d%m%Y-%H%M") | |
git tag release_\$NOW \$DPBRANCH | |
echo " /===============================" | |
echo " | DEPLOYMENT COMPLETED" | |
echo " | Target branch: \$DPTARGET" | |
echo " | Target folder: \$DPGIT_DIR" | |
echo " | Tag name : release_\$NOW" | |
echo " \==============================" | |
else | |
echo "Ref \$ref received. Doing nothing: only the \${DPBRANCH} branch may be deployed on this server." | |
fi | |
else | |
echo "Target directory not declared. Skipping deploy to server." | |
fi | |
done | |
EODP | |
chmod 0755 hooks/post-receive custom-hooks/deploy.sh | |
cd ${GITDIR}/ | |
chown -R ${GITUSER}:${GITGRP} ${GITDIR}/${PROJECT}.git | |
echo "All done, you can now work on \"${PROJECT}.git\"" | |
exit 0 | |
} | |
if [ ! -z $1 ]; then | |
PROJECT=$1 | |
else | |
read -p 'Project name: ' PROJECT | |
fi | |
if [ ! -d ${GITDIR}/${PROJECT}.git ]; then | |
git_init $PROJECT | |
else | |
echo "Project directory ${PROJECT}.git already exists." | |
if [ $(ls -A ${GITDIR}/${PROJECT}.git) ]; then | |
if is_bare ${GITDIR}/${PROJECT}.git | |
then | |
echo "looks like \"${PROJECT}.git\" is an existing git project directory, choose another name." | |
exit 171 | |
else | |
echo "\"${PROJECT}.git\" is not empty, I can't create a Git Project in it. Choose another name." | |
exit 172 | |
fi | |
else | |
echo "\"${PROJECT}.git\" is an empty directory. Do you want to initialize a Git Project here? [y/N]" | |
read answer | |
case $answer in | |
Y|y) | |
git_init $PROJECT | |
;; | |
N|n) | |
echo "Aborting due to user request." | |
exit 173 | |
;; | |
*) | |
# we assume no as default answer. | |
echo "you said \"$answer\" which I don't understand, so to me is no. Aborting." | |
exit 177 | |
;; | |
esac | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment