Created
March 16, 2017 22:33
-
-
Save foxxyz/59d07c5d28fd64d882f227e02cd450b6 to your computer and use it in GitHub Desktop.
Python post-receive hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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