Skip to content

Instantly share code, notes, and snippets.

@JustusAdam
Last active October 7, 2015 18:38
Show Gist options
  • Save JustusAdam/c207851fe7cd2e2ee465 to your computer and use it in GitHub Desktop.
Save JustusAdam/c207851fe7cd2e2ee465 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from subprocess import check_call, CalledProcessError, STDOUT
import os
import json
import io
import sys
APP_DIR = ""
DEPLOYMENT_DIR = ""
JEKYLL_COMMAND = "jekyll"
os.environ['HOME'] = ""
EVENT_TYPE = 'event'
aliases = {
EVENT_TYPE: ('X-GitHub-Event', 'X_GITHUB_EVENT', 'HTTP_X_GITHUB_EVENT')
}
class Event:
def __init__(self, url, prerelease, draft, tag_name, **kwargs):
self.url = url
self.is_release = not prerelease and not draft
self.tag_name = tag_name
def checkout(self):
check_call(["git", "fetch", "--all"], stdout=sys.stdout, stderr=STDOUT)
check_call(["git", "checkout", "tags/" + self.tag_name], stdout=sys.stdout, stderr=STDOUT)
def build(self, target=None):
check_call([JEKYLL_COMMAND, 'build'] + (['--destination', target] if target is not None else []), stdout=sys.stdout, stderr=STDOUT)
def get_header(name):
header_aliases = aliases[name]
for alias in header_aliases:
if alias in os.environ:
return os.environ[alias]
else:
raise KeyError('For key {}'.format(name))
def update_site(event):
os.chdir(APP_DIR)
try:
event.checkout()
except CalledProcessError as e:
print("Exception during fetch/checkout, error code: {}".format(e.returncode))
return
try:
event.build(target=DEPLOYMENT_DIR)
except CalledProcessError as e:
print("Exception during jekyll build, error code: {}".format(e.returncode))
return
print('Successfully deployed tag {} to {}'.format(event.tag_name, DEPLOYMENT_DIR))
def handle(payload):
event = Event(**payload['release'])
if event.is_release:
update_site(event)
else:
print('No update. (prerelease/draft)')
def receive():
event_type = get_header(EVENT_TYPE)
if event_type == 'ping':
print('ping received')
elif event_type == 'release':
payload = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8').read()
payload = json.loads(payload)
handle(payload)
else:
print('Unrecognized event {}'.format(event_type))
def main():
print('Content-Type: text/plain; charset=utf-8')
print('')
sys.stdout.flush()
try:
receive()
except Exception as e:
print('Unexpected exception: {}'.format(e))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment