Skip to content

Deploying the CommonBike app by using the git hook

bartwr edited this page Apr 15, 2017 · 2 revisions

In this post you'll find information on how to deploy the CommonBike Meteor app to a server.

Introduction

The CommonBike repository contains Docker files in the root. This makes it possible to build and run the Meteor app as a Docker container.

Follow the next steps to deploy the CommonBike app to your server.

Log in via SSH

First, login on your VPS.

ssh root@IP -C

Create a folder for your app

mkdir -p /var/app/commonbike
cd /var/app/commonbike

Create a bare git repo

An easy way is to create a Docker container with a git hook combined.

git init --bare

Create a git hook that will auto update the app

ls
cd hooks
vi post-recieve

The git hook that will rebuild the Docker container looks like this:

    #!/bin/bash
    deployDir="../deploy"
    buildConfig="--rm=true"
    runConfig="-p 49153:3000"
    deployingBranch="master"

    while read oldrev newrev refname
    do
      branch=$(git rev-parse --symbolic --abbrev-ref $refname)
      if [ "$deployingBranch" == "$branch" ]; then
        #docker img name
        imageName='commonbike'

        #docker container name
        containerName="$imageName"

        #get last commit for img tag
        lastCommit=`git log --pretty=format:'%h' -n 1`
        echo "Hook : deploy to dir $deployDir"
        mkdir -p $deployDir
        git --work-tree="$deployDir" --git-dir="$(pwd)" checkout -f "$deployingBranch"
        cd $deployDir
        docker tag $imageName:latest $imageName:pendingDeletion
        echo "Building Meteor app"
        cd app
        npm install --production
        meteor build --verbose --allow-superuser --directory ../mrt_build --server-only
        cd ..
        echo "Docker : Building $imageName:$lastCommit"
        # This build and tag the image with "latest" tag
        buildID=`docker build $buildConfig -t $imageName .`

        # Add the commit tag
        docker tag $imageName:latest $imageName:$lastCommit

        if [ $? -ne 0 ]; then
            echo "Docker : Build failed, aborting"
        fi

        echo "Docker : stop and rm $containerName"
        docker-compose down

        echo "Docker : removing old image"
        docker rmi -f $imageName:pendingDeletion

        if [ $? -ne 0 ]; then
            echo "Docker : Stop failed, aborting"
        fi

        docker-compose up -d
        if [ $? -ne 0 ]; then
            echo "Docker : Run failed, aborting"
        fi

    fi
    done

Then, don't forget to:

chmod a+x post-receive

Install docker & docker-compose

docker

curl -sSL https://get.docker.com/ | sh

docker-compose

curl -L "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Install nodejs

Install nodejs (link):

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

Install some dependencies

If we want to run the Meteor app, we have have to install these programs as well:

Install python

apt install python

Install Meteor

curl https://install.meteor.com/ | sh

Install npm

sudo apt-get install npm
npm install -g node-gyp

Also

ln -s /usr/bin/nodejs /usr/bin/node

RESULT

So, now we can easily push new updates to the server. On your localhost, push the latest changes to the production server:

git remote add deploy root@YOUR_SERVER_IP:/var/app/commonbike
git push deploy

Result: on every push the online app gets updated automatically!


Soon in the wiki: nginx configuration.