Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Last active February 5, 2016 13:04
Show Gist options
  • Save Kwpolska/a2b87d648a28b9eccb6c to your computer and use it in GitHub Desktop.
Save Kwpolska/a2b87d648a28b9eccb6c to your computer and use it in GitHub Desktop.
Git ↔ FTP bridge
#!/usr/bin/python3
# Git ↔ FTP bridge
# Takes incoming HTTP requests (from eg. GitHub or BitBucket webhooks), pulls a git repo, and sends it through FTP.
# Requires ncftp.
# I know shell=True is unsafe. This was thrown together in five minutes for a private project, and it runs in tmux. I don’t care.
# Copyright © 2016, Chris Warrick.
# Licensed under WTFPL.
from flask import Flask
import datetime
import subprocess
import os
app = Flask(__name__)
ACCEPTED_REPOS = ['repo1', 'repo2']
@app.route("/<repo_name>", methods=['POST', 'GET'])
def on_push():
cwd = os.getcwd()
if not repo_name:
return "Must specify a name", 400
elif repo_name not in ACCEPTED_REPOS:
return "Unknown repository", 403
os.chdir("/PATH/TO/YOUR/REPO") # change command and path according to repo_name!
subprocess.Popen("git pull && ncftpput -Ru 'USERNAME' -p 'PASSWORD' HOST /REMOTE/DESTDIR /PATH/TO/YOUR/REPO/* && echo -n 'Done ' && date", shell=True)
os.chdir(cwd)
return "Done " + datetime.datetime.utcnow().isoformat()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=12345)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment