Step by step create a remote git for automatically deployment.
On the server (example.com)
Step 1: Create a user on example.com
, as which we (the git client) connect (push) to exmaple.com
. We set git-shell
as the login shell, so it is not possible to interactively login as this user.
sudo useradd -m -s /usr/bin/git-shell git
Step 2: Add your ssh public key to the authorized_keys
file of the created user:
# Because user git can not interactively login, we have to use sudo to get git temporarily
sudo -u git bash
cd ~
# cd /home/git
mkdir -p .ssh
vim .ssh/authorized_keys
# Paste your public key and save
Step 3: Create a git bare
repo for your project:
mkdir testapp
cd testapp
# /home/git/testapp
git init --bare
Step 4: Copy the post-receive
script from this gist to the hooks
dir of the created bare repo.
vim testapp/hooks/post-receive
# Paste the post-receive script from this gist and save
# If you do not need to execute a 'build' and/or 'restart' command,
# just delete or comment the lines 'UPDATE_CMD' and 'RESTART_CMD'
chmod +x testapp/hooks/post-receive
Step 5: Set ownership and permissions of the DEPLOY_ROOT
directory:
sudo chown root:git -R /var/www
sudo chmod 775 /var/www
- (Optional) Add a systemd service file for your app.
If you are using
systemd
, you can use thetestapp.service
file from this gist. Make sure you name it like your repository. The post-receive hook can automatically restart your app. You will also have to allow user git to make thesudo
call. Be very careful and restrictive with this!
On the client
Step 6: Create a git repo and add our newly created remote
:
mkdir testapp
cd testapp
git init
git remote add production git@example.com:~/testapp
Step 7: Commit and push to production:
vim Makefile
#Paste contents of Makefile from this gist (as an example)
git add .
git commit -am "test commit"
git push production master
- Repeat: Develop, test, commit and push :)
make deploy
Congratulations, you just setup git push deployment with automated build and service restart
Here are some more configuration files as a starting point: