Deploy a website to a remote server with Git push

Three steps to configure the deployment process with a Git Hook.

Git is a great tool for version control, but not only. It can also simplify the deployment process, thanks to Git Hooks.

tl;dr

# Copy the 'project-create.sh' bash script https://gist.github.com/francoisromain/58cabf43c2977e48ef0804848dee46c3
# Then create a new project
# It configures a Git repo, and creates a Git hook
$ bash ./path/to/project-create.sh <your-project>
# Add your server as a Git remote called 'deploy'
git remote add deploy ssh://<your-name>@<your-ip>/srv/git/<your-project>.git/
# Push your code and deploy
git push deploy master

User access and permissions

sudo mkdir -p /srv/tmp/
sudo mkdir -p /srv/www/
sudo chgrp -R users /srv
sudo chmod -R g+w /srv

1. Create an empty Git repo on the server

Create an empty Git repo

sudo mkdir -p /srv/git/<your-app>.git

# Init the repo as an empty git repository
cd /srv/git/<your-app>.git
sudo git init --bare

Set the permissions on the Git repo

cd /srv/git/<your-app>.git

# Define group recursively to "users", on the directories
sudo chgrp -R users .
# Define permissions recursively, on the sub-directories
# g = group, + add rights, r = read, w = write, X = directories only
# . = curent directory as a reference
sudo chmod -R g+rwX .

# Sets the setgid bit on all the directories
# https://www.gnu.org/software/coreutils/manual/html_node/Directory-Setuid-and-Setgid.html
sudo find . -type d -exec chmod g+s '{}' +

# Make the directory a Git shared repo
sudo git config core.sharedRepository group

2. Write a Git hook to deploy the code

Create the Git hook file

cd /srv/git/<your-app>.git/hooks

# create a post-receive file
sudo touch post-receive

# make it executable
sudo chmod +x post-receive
#!/bin/sh# The production directory
TARGET="/srv/www/<your-app>"
# A temporary directory for deployment
TEMP="/srv/tmp/<your-app>"
# The Git repo
REPO="/srv/git/<your-app>.git"
# Deploy the content to the temporary directory
mkdir -p $TEMP
git --work-tree=$TEMP --git-dir=$REPO checkout -f
cd $TEMP
# Do stuffs, like npm install…
# Replace the production directory
# with the temporary directory
cd /
rm -rf $TARGET
mv $TEMP $TARGET

3. Deploy from the local computer

Initialize the repo and add the server repo as a remote

cd <your-app>

# Initialize git repo
git init

# Add the server repo as a remote called "deploy"
git remote add deploy ssh://<your-name>@<your-ip>/srv/git/<your-app>.git/

Push to the server (and deploy)

git add . 
git commit -m "<commit message>"
git push deploy master

Freelance developer / designer → http://francoisromain.com

Freelance developer / designer → http://francoisromain.com