-
Notifications
You must be signed in to change notification settings - Fork 0
/
travis_fabfile.py
175 lines (114 loc) · 3.91 KB
/
travis_fabfile.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
""" A fabric file for deploying the site from TravisCI. """
import os
from fabric.api import local, settings, shell_env
DATA = {u'BLOG_AUTHOR': u'test',
u'BLOG_DESCRIPTION': u'askjh',
u'BLOG_EMAIL': u'test@test.de',
u'BLOG_TITLE': u'test'}
def build_and_deploy():
""" Build and deploy the output. """
with shell_env(TZ=_get_timezone()):
_create_output_branch()
_build_html()
_git_commit_all()
_git_push(_get_output_branch())
def git_config_setup():
""" Setup git for pushing from Travis. """
local('git config user.email $GIT_EMAIL')
local('git config user.name $GIT_NAME')
local(
'git remote set-url --push origin '
'https://$GH_TOKEN@github.com/$TRAVIS_REPO_SLUG.git'
)
def init_site():
""" Initialize a nikola demo site. """
from nikola.plugins.command.init import CommandInit, SAMPLE_CONF
command = CommandInit()
SAMPLE_CONF['SITE_URL'] = _get_site_url()
SAMPLE_CONF.update(DATA)
command.execute({'demo': True}, ['demo'])
local('mv demo/* . && rmdir demo')
local('touch files/.nojekyll')
local('rm *.pyc')
def populate_source():
""" Populate the source branch with a demo site. """
if os.path.exists('conf.py'):
return
branch = _get_source_branch()
local('git checkout %s' % branch)
init_site()
_git_commit_all('Initial commit\n[skip ci]')
_git_push(branch)
def main():
""" Main script to kick off the deployment. """
if not os.environ.get('TRAVIS_PULL_REQUEST', 'false') == 'false':
return
git_config_setup()
populate_source()
build_and_deploy()
#### Private protocol #########################################################
def _build_html():
""" Run the build command and get rid of everything else. """
# Build twice until getnikola/nikola#1032 is fixed.
local('nikola build && nikola build')
## Remove all the source files, we only want the output!
local('ls | grep -v output | xargs rm -rf')
with settings(warn_only=True):
local('mv output/* output/.* .')
def _create_output_branch():
branch = _get_output_branch()
with settings(warn_only=True):
local('git branch -D %s' % branch)
local('git checkout --orphan %s' % branch)
def _get_output_branch():
return 'master' if _user_pages() else 'gh-pages'
def _get_repo_name():
repo_slug = os.environ.get('TRAVIS_REPO_SLUG', '/')
user, repo = repo_slug.split('/')
return user, repo
def _get_source_branch():
return 'deploy' if _user_pages() else 'master'
def _get_site_url():
user, repo = _get_repo_name()
if _user_pages():
site_url = 'http://%s.github.io/' % user
else:
site_url = 'http://%s.github.io/%s' % (user, repo)
return site_url
def _get_timezone():
if not os.path.exists('conf.py'):
timezone = 'UTC'
else:
ns = {}
execfile('conf.py', ns)
timezone = ns.get('TIMEZONE', 'UTC')
return timezone
def _git_commit_all(message=''):
""" Commit all the changes to the repo. """
# Remove deleted files
result = local('git ls-files --deleted -z', capture=True)
for path in result.split('\x00'):
if len(path.strip()) > 0:
local('git rm %s' % path, capture=True)
# Add new files
local('git add .', capture=True)
# Commit
with settings(warn_only=True):
if not message:
message = "$(date)"
local('git commit -m "%s"' % message)
def _git_push(branch):
""" Push any changes, to the specified branch. """
local(
'git push -f origin %(branch)s:%(branch)s' % {'branch': branch},
capture=True
)
print('Pushed to %s' % branch)
def _user_pages():
user, repo = _get_repo_name()
if repo.startswith(user) and repo.endswith(('github.io', 'github.com')):
user_pages = True
else:
user_pages = False
return user_pages