The following is mainly taken from this article from Digital Ocean, but includes other issues/fixes I ran into along the way.

Server setup

  • Log into server and create bare git repo for the site. I keep my html files at /var/www/site.com/html so I put the repo for that same site at /var/www/site.com/repo.git

    cd /var/www/site.com/

    mkdir repo.git && cd repo.git

    git init --bare

    Then create a file in repo.git/hooks called post-receive

    nano hooks/post-receive

    That file should look like this:

    #!/bin/bash
    while read oldrev newrev ref
    do
        if [[ $ref =~ .*/master$ ]];
        then
            echo "Master ref received.  Deploying master branch to production..."
            git --work-tree=/var/www/site.com/html --git-dir=/var/www/site.com/repo.git checkout -f
        else
            echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
        fi
    done
    				

    And then make it executable:

    chmod +x hooks/post-receive

Local Machine Setup

  • Now back on the local machine, go to the project's git repo, add a connection to the remote repo.

    git remote add live ssh://user@mydomain.com:port/var/www/site.com/repo.git

    The remote name "live" can be anything.

    Then anytime you want to deploy, run:

    git push live master

    The post-receive script has a check to only allow the master branch to be deployed.

    **If there are issues, check the permissions!

Deploying a Jekyll site

If your site is compiled into a dist folder of some kind, as with jekyll, your post-receive script will need to be updated. Steps for jekyll:

  • Must have ruby, jekyll, and JS runtime (node.js) on the server.

    **May run into issues if using node version manager on the server. See this and/or this.

    Update the post-receive hook:

  • #!/bin/bash
    GIT_REPO=/var/www/site.com/repo.git
    TMP_GIT_CLONE=$HOME/tmp/repo.git
    PUBLIC_WWW=/var/www/site.com/html
    JEKYLL=$HOME/.rvm/gems/ruby-2.2.1/wrappers/jekyll
    
    git clone $GIT_REPO $TMP_GIT_CLONE
    $JEKYLL build -s $TMP_GIT_CLONE -d $PUBLIC_WWW
    rm -Rf $TMP_GIT_CLONE
    exit
    				
  • **GOTCHAS**The "jekyll" command in the post-receive script was not found when I ran git push, so I added a JEKYLL variable pointing to the output of 'which jekyll' (line 5) per this github issue/discussion.

    But still no "jekyll", so I changed /bin/ to /wrappers/ per this stack answer.

    And then success.

Other stuff: