Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2199 from getnikola/github_deploy-auto-commit
Fix #2186 -- commit to source branch automatically
  • Loading branch information
Kwpolska committed Dec 27, 2015
2 parents 5792a3a + 55f7bb1 commit e90de1f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 31 deletions.
7 changes: 7 additions & 0 deletions CHANGES.txt
Expand Up @@ -4,8 +4,13 @@ New in master
Features
--------

* Commit to source branch automatically in ``github_deploy``
if ``GITHUB_COMMIT_SOURCE`` is set to True (Issue #2186)
* Hugo-like shortcodes (Issue #1707)

Bugfixes
--------

New in v7.7.4
=============

Expand Down Expand Up @@ -1690,3 +1695,5 @@ Bugfixes
* Colorbox support in restructured text figures
* Fix for content displaying too wide
* Changelog

vim: tw=70 syntax=rst
20 changes: 5 additions & 15 deletions docs/manual.txt
Expand Up @@ -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:

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions nikola/conf.py.in
Expand Up @@ -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'
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions nikola/nikola.py
Expand Up @@ -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
Expand Down
63 changes: 47 additions & 16 deletions nikola/plugins/command/github_deploy.py
Expand Up @@ -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")

Expand Down

0 comments on commit e90de1f

Please sign in to comment.