I have been looking around for quick and easy ways for deploying website content from our version control system, without having a build process ( the site is ninety nine percent static html and has nothing to build ). The only aspects that are interesting from a build process is a minifying resources and less compilation.
I have seen a few posts around the web suggesting a git post-receive hook. The idea of pushing the desired codebase to the desired server is desired, is in fact quite appealing. I have worked a lot in the past with solutions such as Jenkins, that work great for build solutions, but seemed like overkill for this context.
I use git for the repository management so adding an additional remote is really easy.
Here are the steps that i went through to set this up
Create a user on the linux server specifically for this
</p> <p>useradd -d /home/website -m websiteuser -s /bin/shell<br />
In the home folder create two folders, one for the website code, and the second to hold the git repository.
inside the git folder initialize the repo like this
</p> <p>git init --bare</p> <p>
Once that is done, create your post-receive hook inside the git_repo/hooks directory
my post-receive hook looks like this
<br /> #!/bin/bash<br /> WEB_ROOT=/home/website/www<br /> LIVE_LINK=live-site<br /> FOLDER=`date +"%Y-%m-%d"`<br /> GIT_WORK_TREE="$WEB_ROOT/$FOLDER"<br /> mkdir -p $GIT_WORK_TREE<br /> echo "GIT WORK TREE: $GIT_WORK_TREE"<br /> export GIT_WORK_TREE<br /> git checkout -f</p> <p>echo "Checking for $WEB_ROOT/$LIVE_LINK"</p> <p>if [ -h "$WEB_ROOT/$LIVE_LINK" ]; then<br /> echo "removing old live link $WEB_ROOT/$LIVE_LINK"<br /> rm -rf "$WEB_ROOT/$LIVE_LINK"<br /> fi<br /> echo "CREATING $LIVE_LINK"<br /> ln -s "$WEB_ROOT/$FOLDER" "$WEB_ROOT/$LIVE_LINK"<br />
That is all you need for the hook side. Now i would recommend changing the shell of the user for SSH key based login, meaning, copy your SSH key to the authorized_keys file of the website account, well your key and anyone else you want to authorize to deploy to this server. Once the keys are copied, change the shell of the user to git-shell, to prevent anyone trying to login to that account.
That should be all. Now simply add the remote location
<br /> git remote add production_deploy website@host:repo.git<br /> git push production_deploy master<br />
You now have the code deployed. The rest is simply configuring your VirtualHost configuration of your apache or nginx server.