Last active
January 25, 2019 23:51
-
-
Save marksteve/8243318 to your computer and use it in GitHub Desktop.
Push-to-deploy static sites with Pelican, Flask and Github
This file contains hidden or 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
""" | |
Simple web server that accepts Github serice hooks | |
to deploy Pelican static sites | |
Usage: | |
$ python ./pelican_deployer.py | |
Settings are loaded from env: | |
SECRET=<secret key> | |
REMOTE=<git remote> (defaults to origin) | |
ROOT=<output root> (defaults to /srv/pelican) | |
PORT=<port> (defaults to 5000) | |
""" | |
import os | |
from subprocess import check_output | |
from flask import Flask, json, jsonify, request | |
app = Flask(__name__) | |
def sh(cmd, **kwargs): | |
app.logger.info(check_output(cmd.format(**kwargs), shell=True)) | |
@app.route('/{}'.format(os.environ['SECRET']), methods=['POST']) | |
def deploy(): | |
payload = json.loads(request.form['payload']) | |
branch = payload.get('ref').split('/')[2] | |
sh( | |
'git pull {remote} {branch}', | |
remote=os.environ.get('REMOTE', 'origin'), | |
branch=branch, | |
) | |
sh( | |
'pelican -d -o {root}/{branch} content', | |
root=os.environ.get('ROOT', '/srv/pelican'), | |
branch=branch, | |
) | |
return jsonify(dict(ok=True)) | |
if __name__ == '__main__': | |
app.run(port=int(os.environ.get('PORT', 5000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment