zulip/tools/post-receive

61 lines
2.1 KiB
Python
Executable File

#!/usr/bin/python
#
# Humbug's post-receive hook. There is a symlink
# from /srv/git/humbug.git/hooks/post-receive
# to ~humbug/humbug/tools/post-receive
# on git.humbughq.com. So to deploy changes to this script, run
#
# ssh humbug@git.humbughq.com 'cd humbug; git pull'
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
# see contrib/hooks/ for a sample
import os
import sys
import subprocess
import time
from os import path
import os.path
# check_output is backported from subprocess.py in Python 2.7
def check_output(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd, output=output)
return output
subprocess.check_output = check_output
def update_deployment(server, oldrev, newrev, refname):
subprocess.check_call(["ssh", server, "--", "env", "-u", "GIT_DIR",
"/home/humbug/humbug/tools/update-deployment",
oldrev, newrev, refname])
deployments = {
'refs/heads/prod': ('humbughq.com', 'prod'),
'refs/heads/master': ('staging.humbughq.com', 'master'),
'refs/heads/test-post-receive': ('staging.humbughq.com', 'master'),
}
for ln in sys.stdin:
oldrev, newrev, refname = ln.strip().split()
if refname in deployments:
(server, branch) = deployments[refname]
p = subprocess.Popen("/home/humbug/humbug/api/integrations/git/post-receive",
stdin=subprocess.PIPE)
p.communicate(input=ln)
update_deployment(server, oldrev, newrev, branch)