-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
50 lines (42 loc) · 1.98 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from flask import Flask, jsonify, request
import os, yaml, requests, time
app = Flask(__name__)
with open(os.path.join(os.path.dirname(__file__), 'config.yaml'), 'r') as ymlfile:
try:
config = yaml.safe_load(ymlfile)
except yaml.YAMLError as exc:
raise Exception("Error parsing config file: {}".format(exc))
VERBOSE = config['VERBOSE']
PROJECT_PATH = config['PROJECT_PATH']
ROOT_PATH = os.getcwd()
def ExecuteBashScript(script, branch):
os.system("chmod u+x " + ROOT_PATH + '/scripts/'+script)
## Pass ROOT_PATH and PROJECT_PATH to the script
os.system('cd ' + PROJECT_PATH + ' && ' + ROOT_PATH + '/scripts/'+script + ' "' + ROOT_PATH + '" "' + PROJECT_PATH + '"' + ' "' + branch + '"')
def SlackAlert(msg):
"""
SlackAlert(msg)
Send a message to a Slack channel.
"""
if VERBOSE == "enabled":
msg = msg + " [" + time.strftime("%Y-%m-%d %H:%M:%S") + "]"
requests.post(config['SLACK_WEBHOOK'], json={"text": msg})
@app.route("/webhook", methods=['POST'])
def webhook():
payload = request.get_json()
branch = payload['ref'].split('/')[-1]
if branch == config['BRANCH_TO_USE']:
## parse github webhook payload response and get event type and branch name
SlackAlert("👀 A new commit has been detected to the {} branch, working on updating source code.".format(branch))
os.system('cd ' + PROJECT_PATH + ' && git pull origin ' + branch)
SlackAlert("🎉 The {} branch has been updated successfully.".format(branch))
SlackAlert("🛠 Executing the build script.")
ExecuteBashScript('build.sh', branch)
SlackAlert("🎉 The build script has been executed successfully.")
SlackAlert("🛠 Executing the deploy script.")
ExecuteBashScript('deploy.sh', branch)
SlackAlert("🎉 The deploy script has been executed successfully.")
return jsonify(payload)
if __name__ == "__main__":
SlackAlert("🚀 Starting the webhook server.")
app.run(host='0.0.0.0', port=5000)