Git Hooks for app deployment

Git is a great tool to manage development code bases. git push upload local codes into a remote repository, which can be purposed for a backup or code sharing.

Another useful case of git is to deploy an application. Traditionally, we have used a FTP application to upload codes onto a production server. However, using a git hook, a deployment step can be very simple and efficient. It is just like committing a git.

Local Server Deployment

I would not recommend this if you develop an Angular app, because an Angular app can run on built-in local development server, localhost:4200 (ng serve).

  1. Create a repo folder in the development computer (/C/dev/testGit/repo).
  2. git init under the repo folder. This will create .git folder under the folder (Windows PC will hide the .git folder, /C/dev/testGit/repo/.git).
  3. Create a git hook (post-commit) under ..repo/.git/hooks/.

The post-commit hook will run after committing a git to a local repo like git commit -m "myfirstcommit" under the repo folder.

--- For example, a post-commit file ---

#!/bin/bash unset GIT_INDEX_FILE git --work-tree=/C/dev/testGit/production --git-dir=/C/dev/testGit/repo/.git checkout -f

After a commit, it will copy the work files into the working directory.

How to use:
Create an index.html under the repo folder.
git add .

git commit -m "update an index file"

After the commit, check the production folder and find that the index.html in the repo folder is now copied to the production folder .

Remote Production Server

The real use case of a git hook is to deploy an app to a production server.

  1. Create a git repository on a production server. Let's say, You create a 'bear' git in /var/www/testGitHook-git with a command, git init --bare under that folder. The bear git does not contain a working directory, which is perfect to a production server.

  2. Remember the 'bear' git repo is not a real production folder. It would just work as a transitional repo, from which the actual codes will be copied to the production folder.

  3. Create a git hook (post-receive) under /var/www/testGitHook-git/hooks/ with the following content.

--- for example, post-receive hook ---<br>
#!/bin/bash`<br>
TARGET="/var/www/sierra-app/html"`<br>
GIT_DIR="/var/www/testGitHook-git"`<br>
BRANCH="master"`

while read oldrev newrev ref
do
    if [[ $ref = refs/heads/$BRANCH ]];
    then
        echo "Master ref received. Deploying master branch to production..."
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout master -f -- dist/ server/ server.js package.json
        cd $TARGET
        cp -r dist/* .
        rm -rf dist
    else
        echo "Ref $ref successfully received. Doing nothing. Only the master branch can be deployed"
    fi
done

The post-receive hook will run after you push a local repo to the remote repository.For doing this, you should first setup a remote repository (next).

  1. Add a remote repository in a local git directory, git remote add production ssh://wwwuser@hostname.com/var/www/testGitHook-git. Now you can push the local repo to the remote with a command git push production master if the remote repository is called production and the branch is master. The outcome of this post-receive hook will be that all local repository files will be pushed to the remote 'bear' git repo (production) and then will be copied to the real production web folder (here, /var/www/sierra-app/html). In the above example, only selected folders (dist/ and server/) and files (server.js and package.json) will be copied into the production web folder. The actual app will kick in when a browser requests an index file under that production folder.

In summary, a git is a very helpful tool for web development. It can be generally used code backup and sharing. More than that, it can be used for deploying an app just with one command line, git push production master.