Last active
March 11, 2016 09:59
-
-
Save schmich/ba192c9cbddeba496946 to your computer and use it in GitHub Desktop.
Multi-user git webserver deployment
This file contains hidden or 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
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 |
This file contains hidden or 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 | |
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 -------------------------------------------------------------------------------- |
This file contains hidden or 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO
git push --mirror
)GIT_DIR=~/site.git GIT_WORK_TREE=/srv/www/site git status