-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
101 lines (87 loc) · 2.59 KB
/
tasks.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# task.py
#
#
# Dennis Wai 3/29/2020
from invoke import Collection, task
import cmd
from colorama import Fore, Style
from datetime import datetime
import sys
@task()
def build(c):
'''
Perform a one time build of the Jekyll source
'''
print(Fore.GREEN +
"Doing one time build of Jekyll site..." +
Style.RESET_ALL)
cmd.jekyll.build(c)
def push_to_server(c, msg, branch):
'''
Creates a commit and then push to master w/ error handling
'''
c.run("git add -u")
res = c.run('git commit -m "%s"' % msg, warn=True)
if res.exited == 1:
print(Fore.GREEN +
"Nothing to commit. Not deploying" +
Style.RESET_ALL)
sys.exit(0)
res = c.run("git push origin %s" % branch, warn=True)
if res.exited != 0:
print(Fore.RED +
"Unable to deploy site to %s" % branch +
Style.RESET_ALL)
print(res.stdout)
print(res.stderr)
sys.exit(1)
@task()
def deploy(c):
'''
Copies current Jekyll src and puts in submodule and git push
'''
print(Fore.GREEN +
"Deploying site..." +
Style.RESET_ALL)
# Figure out if there are uncommitted changes in src before proceeding
res = c.run("git status --porcelain | grep -v site", hide=True, warn=True)
if res and len(res.stdout) > 0:
# Implies that there are uncommitted changes
print(Fore.RED +
"You have uncommitted changes\n" +
"Address them before deploying." +
Style.RESET_ALL)
sys.exit(1)
ts = datetime.now().strftime("%m-%d-%Y %H:%M:%S")
cmt_msg = "Deployed site on %s" % ts
c.run("rsync -r --delete src/_site/* site/", warn=True)
print(Fore.GREEN + "Making commit on master" + Style.RESET_ALL)
with c.cd("site"):
push_to_server(c, cmt_msg, "master")
# This will add the update to the submodule and commit it as well
print(Fore.GREEN + "Updating submodule on master-src" + Style.RESET_ALL)
push_to_server(c, cmt_msg, "master-src")
@task()
def serve(c):
'''
Serves website locally for development
'''
print(Fore.GREEN +
"Running Jekyll as a service..." +
Style.RESET_ALL)
cmd.jekyll.serve(c)
@task()
def prereqs(c):
'''
Installs all the gems required to build this site and cache locally
'''
print(Fore.GREEN +
"Installing gems..." +
Style.RESET_ALL)
cmd.jekyll.bundle(c)
# Add all the top level tasks to the namespace
ns = Collection()
ns.add_task(build)
ns.add_task(serve)
ns.add_task(prereqs)
ns.add_task(deploy)