Last active
August 29, 2015 14:05
-
-
Save anubhavsinha/125bd2449abf5909dcea to your computer and use it in GitHub Desktop.
A flask script to listen for POST hooks for the production branch.
This file contains 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
################################################################### | |
# TODO: | |
# add logging and ses email alerts for deployment failed and passed. | |
# Also pipe in the log file to logstash etc. | |
# Also write test cases | |
# Make all of the deployment task async and return to the POST request immediately. | |
# Already uses auto-reload - flask debug=True provides auto-reload out of t | |
# You will need to | |
# cd ~ | |
# git clone --mirror git@bitbucket.org:<username>/<repo-name>.git | |
# cd ~/<repo-name>.git | |
# GIT_WORK_TREE=/home/ubuntu/application git checkout -f production | |
# | |
################################################################## | |
import json | |
import subprocess | |
from flask import Flask, render_template, request, url_for | |
app = Flask(__name__) | |
@app.route('/deploy', methods=['POST']) | |
def deploy(): | |
repo_dir = '/home/ubuntu/repo-name.git' | |
application_dir = '/home/ubuntu/application' | |
git_bin_path = '/usr/bin/git' | |
update = False | |
payload = json.loads(request.form['payload']) | |
# merging and pushing to bitbucket, the commits array will be empty. | |
# no way to know what branch was pushed to, so we will do an update. | |
if payload.get("commits",None)==None: | |
update = True | |
else: | |
for each_commit in payload["commits"]: | |
branch = each_commit["branch"] | |
if branch=='production' or (each_commit["branches"] and 'production' in each_commit["branches"]): | |
update = True | |
if update: | |
# git checkout to the application directory | |
git_fetch = 'cd '+repo_dir+' && ' + git_bin_path + ' fetch' | |
print git_fetch | |
subprocess.call(git_fetch, shell=True) | |
git_checkout = 'cd '+repo_dir+' && ' + 'GIT_WORK_TREE=' + application_dir + ' ' + git_bin_path + ' checkout -f' | |
print git_checkout | |
subprocess.call(git_checkout, shell=True) | |
rebuild_docker_image = 'cd ' + application_dir + '/qsc && ' + 'docker build --tag image-name .' | |
print rebuild_docker_image | |
subprocess.call(rebuild_docker_image, shell=True) | |
stop_all_containers = 'docker stop $(docker ps -a -q)' | |
print stop_all_containers | |
subprocess.call(stop_all_containers, shell=True) | |
remove_all_containers = 'docker rm $(docker ps -a -q)' | |
print remove_all_containers | |
subprocess.call(remove_all_containers, shell=True) | |
start_rails_app_container = 'docker run -p 80:3000 -d image-name' | |
print start_rails_app_container | |
subprocess.call(start_rails_app_container, shell=True) | |
print 'production updated' | |
return "payload recieved" | |
if __name__ == '__main__': | |
app.run( | |
host="0.0.0.0", | |
port=int("8080"), | |
debug=True | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment