Deploy with git

In this blog post, I'm going to describe how I deploy my website via git, without a single additional command. When I push master branch to my github repo, I want it to deploy to production. Also, when I push dev branch it will deploy to development environment.

Note: {folder} is meant to be replaced.

Server side

This part is intended to be run on your server machine that you want to deploy to.

Example of hook script:
#!/bin/sh

# colors
GREEN="\033[1;32m"
RED="\033[1;31m"
YELLOW="\033[1;33m"
NOCOLOR="\033[0m"

# temp folder used for building
TEMP="/tmp/{folder}"

# path to the bare repo
REPO="{repo}"

# fetches branch name
while read oldrev newrev refname
branch=$(git rev-parse --symbolic --abbrev-ref $refname 2>/dev/null)

if [ "$branch" == "master" ]; then
    TARGET="{prod_folder}"
    echo -e "${YELLOW}Pushing to production.${NOCOLOR}"
elif [ "$branch" == "dev" ]; then
    TARGET="{dev_folder}"
    echo -e "${YELLOW}Pushing to development.${NOCOLOR}"
else
    echo -e "${YELLOW}Not master/develop branch, ignoring.${NOCOLOR}"
    exit 1
fi

mkdir -p $TEMP
cd $TEMP
git --work-tree=$TEMP --git-dir=$REPO checkout -f $branch

# execute all your build scripts here
# example for my website:
npm install
npm run build

# copy your project to target here
# in my example
rm -rf $TARGET/*
mv ./dist/* $TARGET/

# cleanup
rm -rf $TEMP

Client side

This part is intended to be run on your development machine that you want to deploy from.
Also, the {folder} is an absolute path of folder created in previous part.