Skip to content
Permalink
Browse files

Fix #2186 -- commit to source branch automatically

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Dec 27, 2015
1 parent 1d90dfc commit 405dbd10595a59ebbbc2f51bce9757e052c1567c
Showing with 64 additions and 35 deletions.
  1. +5 −15 docs/manual.txt
  2. +7 −0 nikola/conf.py.in
  3. +5 −4 nikola/nikola.py
  4. +47 −16 nikola/plugins/command/github_deploy.py
@@ -1178,6 +1178,9 @@ sure you have ``nikola`` and ``git`` installed on your PATH.
deployed. We default to ``master``, but user pages should use ``src`` or
something else.
* ``GITHUB_REMOTE_NAME`` is the remote to which changes are pushed.
* ``GITHUB_COMMIT_SOURCE`` controls whether or not the source branch is
automatically committed to and pushed. We recommend setting it to
``True``.

4. Create a ``.gitignore`` file. We recommend adding at least the following entries:

@@ -1188,24 +1191,11 @@ sure you have ``nikola`` and ``git`` installed on your PATH.
__pycache__
output

5. Switch to your source branch (if necessary) and commit to your source branch:

.. code:: text

git checkout -b src
git add .
git commit -am "Initial commit"

5. If you set ``GITHUB_COMMIT_SOURCE`` to False, you must switch to your source
branch and commit to it. Otherwise, this is done for you.
6. Run ``nikola github_deploy``. This will build the site, commit the output
folder to your deploy branch, and push to GitHub. Your website should be up
and running within a few minutes.
7. You should push your source branch to GitHub, too — this way, you have a
backup in case of catastrophic disk failure, and you can work on your
website from around the world.

.. code:: text

git push -u origin src

If you want to use a custom domain, create your ``CNAME`` file in
``files/CNAME`` on the source branch. Nikola will copy it to the
@@ -463,6 +463,9 @@ REDIRECTIONS = ${REDIRECTIONS}
# ]
# }

# github_deploy configuration
# For more details, read the manual:
# https://getnikola.com/handbook.html#deploying-to-github
# For user.github.io OR organization.github.io pages, the DEPLOY branch
# MUST be 'master', and 'gh-pages' for other repositories.
# GITHUB_SOURCE_BRANCH = 'master'
@@ -471,6 +474,10 @@ REDIRECTIONS = ${REDIRECTIONS}
# The name of the remote where you wish to push to, using github_deploy.
# GITHUB_REMOTE_NAME = 'origin'

# Whether or not github_deploy should commit to the source branch automatically
# before deploying.
GITHUB_COMMIT_SOURCE = True

# Where the output site should be located
# If you don't use an absolute path, it will be considered as relative
# to the location of conf.py
@@ -255,10 +255,10 @@
zh_cn='zh-cn'
),
'PYPHEN_LOCALES': {
'bg': 'bg',
'ca': 'ca',
'cs': 'cs',
'cz': 'cs',
'bg': 'bg',
'ca': 'ca',
'cs': 'cs',
'cz': 'cs',
'da': 'da',
'de': 'de',
'el': 'el',
@@ -512,6 +512,7 @@ def __init__(self, **config):
'GITHUB_SOURCE_BRANCH': 'master',
'GITHUB_DEPLOY_BRANCH': 'gh-pages',
'GITHUB_REMOTE_NAME': 'origin',
'GITHUB_COMMIT_SOURCE': False, # WARNING: conf.py.in overrides this with True for backwards compatibility
}

# set global_context for template rendering
@@ -97,30 +97,61 @@ def _execute(self, command, args):

return

def _commit_and_push(self):
"""Commit all the files and push."""
source = self.site.config['GITHUB_SOURCE_BRANCH']
deploy = self.site.config['GITHUB_DEPLOY_BRANCH']
remote = self.site.config['GITHUB_REMOTE_NAME']
source_commit = uni_check_output(['git', 'rev-parse', source])
commit_message = (
'Nikola auto commit.\n\n'
'Source commit: %s'
'Nikola version: %s' % (source_commit, __version__)
)
output_folder = self.site.config['OUTPUT_FOLDER']

command = ['ghp-import', '-n', '-m', commit_message, '-p', '-r', remote, '-b', deploy, output_folder]

def _run_command(self, command, xfail=False):
"""Run a command that may or may not fail."""
self.logger.info("==> {0}".format(command))
try:
subprocess.check_call(command)
return 0
except subprocess.CalledProcessError as e:
if xfail:
return e.returncode
self.logger.error(
'Failed GitHub deployment — command {0} '
'returned {1}'.format(e.cmd, e.returncode)
)
return e.returncode
raise SystemError(e.returncode)

def _commit_and_push(self):
"""Commit all the files and push."""
source = self.site.config['GITHUB_SOURCE_BRANCH']
deploy = self.site.config['GITHUB_DEPLOY_BRANCH']
remote = self.site.config['GITHUB_REMOTE_NAME']
autocommit = self.site.config['GITHUB_COMMIT_SOURCE']

try:
if autocommit:
commit_message = (
'Nikola auto commit.\n\n'
'Nikola version: {0}'.format(__version__)
)
e = self._run_command(['git', 'checkout', source], True)
if e != 0:
self._run_command(['git', 'checkout', '-b', source])
self._run_command(['git', 'add', '.'])
# Figure out if there is anything to commit
e = self._run_command(['git', 'diff-index', '--quiet', 'HEAD'], True)
if e != 0:
self._run_command(['git', 'commit', '-am', commit_message])
else:
self.logger.notice('Nothing to commit to source branch.')

source_commit = uni_check_output(['git', 'rev-parse', source])
commit_message = (
'Nikola auto commit.\n\n'
'Source commit: {0}'
'Nikola version: {1}'.format(source_commit, __version__)
)
output_folder = self.site.config['OUTPUT_FOLDER']

command = ['ghp-import', '-n', '-m', commit_message, '-p', '-r', remote, '-b', deploy, output_folder]

self._run_command(command)

if autocommit:
self._run_command(['git', 'push', '-u', remote, source])
except SystemError as e:
return e.args[0]

self.logger.info("Successful deployment")

0 comments on commit 405dbd1

Please sign in to comment.
You can’t perform that action at this time.