Skip to content

Instantly share code, notes, and snippets.

@ronaldocherokee
Forked from rafael-neri/post-receive
Created August 10, 2022 19:18
Show Gist options
  • Save ronaldocherokee/dd1578b9e3a6c5fb8c00ec3c957c3dd2 to your computer and use it in GitHub Desktop.
Save ronaldocherokee/dd1578b9e3a6c5fb8c00ec3c957c3dd2 to your computer and use it in GitHub Desktop.

Revisions

  1. @geekforbrains geekforbrains created this gist May 18, 2012.
    37 changes: 37 additions & 0 deletions post-receive
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/bin/bash

    REPO=NAME_HERE

    # Dir paths on remote server
    # These are associated with branches within a git project
    LIVE_BRANCH="master"
    LIVE="git@host:/var/www/live/"
    STAGE_BRANCH="develop"
    STAGE="git@host:/var/www/stage/"

    if ! [ -t 0 ]; then
    read -a ref
    fi

    # Get branch name from ref head
    IFS='/' read -ra REF <<< "${ref[2]}"
    branch="${REF[2]}"

    # Make tmp dir for extracting files and cleaning up .git (we dont want them on the live site)
    tmpdir="/tmp/$REPO/$branch"
    mkdir -p $tmpdir

    # Assuming git is installed at /home/git/...
    git --work-tree=$tmpdir --git-dir="/home/git/repositories/$REPO.git" checkout -f $branch

    # If pushing to LIVE_BRANCH, deploy on LIVE
    if [ "$LIVE_BRANCH" == "$branch" ]; then
    rsync -vzre ssh --delete "$tmpdir/" $LIVE
    fi

    # If pushing to STAGE_BRANCH, deploy on STAGE
    #if [ "$STAGE_BRANCH" == "$branch" ]; then
    #rsync -vzre ssh --delete "$tmpdir/" $STAGE
    #fi

    rm -rf "/tmp/$REPO"