Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alissonbovenzo/7adce006864f02d71f65fe54d16aaea3 to your computer and use it in GitHub Desktop.
Save alissonbovenzo/7adce006864f02d71f65fe54d16aaea3 to your computer and use it in GitHub Desktop.
Push-to-Deploy, Python edition - git post-receive hook script
#!/usr/bin/env python
# post-receive hook for git-based deployments
# https://edmondburnett.com/post/python-push-to-deploy
import sys
import os
from subprocess import call
# configuration
deploy_to_path = '/path/to/deploy/directory/'
deploy_branch = 'master'
def post_receive(from_commit, to_commit, branch_name):
# Don't deploy if pushed branch != deploy_branch
if not branch_name.endswith(deploy_branch):
print('Received branch ' + branch_name + ', not deploying.')
sys.exit()
# copy files to deploy directory
call('GIT_WORK_TREE="' + deploy_to_path + '" git checkout -f ' + branch_name, shell=True)
print('DEPLOY: ' + branch_name + '(' + to_commit + ') copied to ' + deploy_to_path)
# TODO: Deployment Tasks
# i.e. Run a script, restart daemons, etc
if __name__ == '__main__':
# get values from STDIN
fc,tc,bn = sys.stdin.read().split()
post_receive(fc, tc, bn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment