Skip to content

Instantly share code, notes, and snippets.

@foxxyz
Created March 16, 2017 22:33

Revisions

  1. foxxyz revised this gist Mar 16, 2017. No changes.
  2. foxxyz created this gist Mar 16, 2017.
    47 changes: 47 additions & 0 deletions post-receive
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/usr/bin/env python3
    # post-receive hook for git-push deployments
    import sys
    from subprocess import call

    deploy_path = '/path/to/project/'
    deploy_branch = 'master'


    class Colors:
    FAIL = '\033[91m'
    OK = '\033[92m'
    END = '\033[0m'


    def error(msg):
    "Error message print"
    print(Colors.FAIL + msg + Colors.END, flush=True)


    def info(msg):
    "Info message print"
    print(Colors.OK + msg + Colors.END, flush=True)


    def post_receive(from_commit, to_commit, branch):

    # Only deploy if branch matches
    if not branch.endswith('/{}'.format(deploy_branch)):
    error('Received branch {}, not deploying'.format(branch))
    return

    # Copy files
    info('Deploying {} ({}) to {}...'.format(branch, to_commit, deploy_path))
    call('GIT_WORK_TREE="{}" git checkout -f {}'.format(deploy_path, branch), shell=True)

    # Install dependencies
    info('Installing dependencies...')
    call('/usr/bin/npm install', shell=True, cwd=deploy_path)

    # Compile with webpack
    info('Compiling code...')
    call('/usr/bin/npm run build', shell=True, cwd=deploy_path)


    if __name__ == '__main__':
    post_receive(*sys.stdin.read().split())