Skip to content

Instantly share code, notes, and snippets.

@foxxyz
Created March 16, 2017 22:33
Show Gist options
  • Save foxxyz/59d07c5d28fd64d882f227e02cd450b6 to your computer and use it in GitHub Desktop.
Save foxxyz/59d07c5d28fd64d882f227e02cd450b6 to your computer and use it in GitHub Desktop.
Python post-receive hook
#!/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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment