Skip to content

Instantly share code, notes, and snippets.

@schmich
Last active March 11, 2016 09:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmich/ba192c9cbddeba496946 to your computer and use it in GitHub Desktop.
Save schmich/ba192c9cbddeba496946 to your computer and use it in GitHub Desktop.
Multi-user git webserver deployment
sudo echo 192.168.1.42 webserver >> /etc/hosts
sudo cat >> ~/.ssh/config <<EOF
Host webserver 192.168.1.42
User user1
Hostname webserver
IdentityFile ~/.ssh/id_rsa
EOF
cat ~/.ssh/id_rsa.pub | (ssh user@webserver "mkdir -p .ssh && cat >> ~/.ssh/authorized_keys")
git init project
cd project
echo test > foo.txt
git add .
git commit -am "Initial commit."
git remote add prod user1@webserver:~git/project.git
git push -u prod master
#!/bin/bash
DEPLOY_DIR=/srv/www/project
function deploy() {
echo Deploying $1 to $DEPLOY_DIR.
git --work-tree="$DEPLOY_DIR" checkout -f
}
if [ ! -d "$DEPLOY_DIR" ]; then
echo "Deploy directory $DEPLOY_DIR does not exist."
exit 1
fi
echo --------------------------------------------------------------------------------
if [ -t 0 ]; then
rev=`git log -n 1 --pretty=format:"%h"`
deploy $rev
else
while read oldrev newrev refname; do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "master" == "$branch" ]; then
rev=`git rev-parse --short $newrev`
deploy $rev
else
echo "Not deploying branch $branch. Only master can be deployed."
fi
done
fi
echo Finished.
echo --------------------------------------------------------------------------------
# Assume user1, user2 exist.
sudo useradd -m -s /usr/bin/git-shell git
sudo su
usermod -a -G git user1
usermod -a -G git user2
su git
git init --bare --shared ~/project.git
sudo mkdir /srv/www/project
sudo chown -R git:git /srv/www/project
sudo chmod 770 /srv/www/project
wget https://gist.github.com/schmich/ba192c9cbddeba496946/raw/post-receive project.git/hooks/post-receive
# Update DEPLOY_DIR in post-receive.
chmod +x project.git/hooks/post-receive
@schmich
Copy link
Author

schmich commented Feb 29, 2016

TODO

  • Workflow
  • Mirroring (git push --mirror)
  • GIT_DIR=~/site.git GIT_WORK_TREE=/srv/www/site git status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment