Skip to content

Instantly share code, notes, and snippets.

@rattrayalex
Last active Nov 18, 2015
Embed
What would you like to do?
Github pages deploy script

Deploy dist/ dir to gh-pages

based around the workflow here:

https://gist.github.com/chrisjacob/825950

My structure:

project/
  gulpfile.js
  package.json
  bin/
    deploy.py
  src/
    jade/
    coffee/
    etc...
  dist/
    html/
    js/
    etc...
  

The gulpfile (or grunt, or whatever build system you like) outputs everything to dist/

#!/usr/bin/env python
"""
Deploys dist/ to gh-pages
Author: Alex Rattray <rattray.alex@gmail.com>
"""
import os
import sys
import subprocess
import traceback
from clint.textui import puts, colored, indent
def cyan(msg):
return puts(colored.cyan(msg))
def red(msg):
return puts(colored.red(msg))
def shell(cmd, *args, **kwargs):
# tell the user what's about to go out
puts(colored.blue("-> {}".format(cmd)))
try:
out = subprocess.check_output(cmd, shell=True, *args, **kwargs)
except subprocess.CalledProcessError as e:
# puts(colored.red(traceback.format_exc()))
red("Command failed! (exit code {})".format(e.returncode))
with indent(4):
red("Failed cmd: {}".format(e.cmd))
red("Exit code: {}".format(e.returncode))
red("Output:")
out = e.output
raise e
finally:
with indent(4):
puts(out)
return out
def main():
# go to dist, which is an entirely different git checkout
os.chdir('dist')
puts(os.getcwd())
# should be on the gh-pages branch
current_branch = shell("git branch | grep '*'")
current_branch = current_branch.replace('* ', '').strip()
if current_branch != 'gh-pages':
red('! Why are you not on gh-pages! '
'This is weird, so Im not even going to fix it for you. '
'Get your act together.')
exit()
# add, commit, push
shell('git add -A .')
try:
shell('git commit -m "Automatic Commit; deploying at `date`"')
shell('git push')
except:
cyan('! Nothing sames to have changed; declining to deploy.')
puts('If you\'d like to force a push anyway, '
'just cd to dist/ and `git push`')
if __name__ == '__main__':
puts(os.getcwd())
if 'bin/' not in sys.argv[0]:
red('must be run as bin/deploy.py! from root!')
sys.exit()
main()
#!/usr/bin/env python
"""
Checks out the gh-pages branch to _site/ dir
as a separate repo.
Allows for fancy-pants deploys, a la
https://gist.github.com/chrisjacob/825950
To be used with the sibling deploy script
Author: Alex Rattray <rattray.alex@gmail.com>
"""
import os
import sys
import subprocess
from clint.textui import puts, indent
from clint.textui.colored import cyan, red, blue, yellow
def shell(cmd, silent_err=False, *args, **kwargs):
# tell the user what's about to go out
puts(blue("-> {}".format(cmd)))
try:
out = subprocess.check_output(cmd, shell=True, *args, **kwargs)
except subprocess.CalledProcessError as e:
errcolor = red
if silent_err:
errcolor = yellow
puts(errcolor("Command failed! (exit code {})".format(e.returncode)))
with indent(4):
puts(errcolor("Failed cmd: {}".format(e.cmd)))
puts(errcolor("Exit code: {}".format(e.returncode)))
puts(errcolor("Output:"))
out = e.output
if not silent_err:
raise e
finally:
with indent(4):
puts(out)
return out
def add_to_gitignore(string):
puts(cyan('going to ensure that {} is in .gitignore...'.format(string)))
with open('.gitignore', 'r') as gitignore_f:
for line in gitignore_f.readlines():
if line == string + '\n':
puts(cyan('_site is already in .gitignore!'))
return
with open('.gitignore', 'a') as gitignore_f:
gitignore_f.write(string + '\n')
puts(cyan('wrote _site to .gitignore'))
def main():
add_to_gitignore('_site')
shell('rm -rf _site/')
try:
shell('git branch gh-pages') # throws an error if branch existed
# since it wasn't there already, push gh-pages
shell('git push -U origin gh-pages')
except:
puts('not a problem, just checkin')
remote = shell('git remote -v').split()[1]
shell('git clone -b gh-pages --single-branch --depth 1 {remote} _site'
.format(remote=remote))
# go to _site, which is an entirely different git checkout
os.chdir('_site')
puts('now in folder {}'.format(os.getcwd()))
# turn these back on if you remove `-b gh-pages` from the `clone` command.
# shell('git checkout gh-pages')
# time.sleep(1)
# shell('git branch -D master')
# run jekyll build for initial content
os.chdir('..')
puts('now in folder {}'.format(os.getcwd()))
shell('jekyll build')
if __name__ == '__main__':
puts(os.getcwd())
if 'bin/' not in sys.argv[0]:
puts(red('must be run as bin/setup.py! from projectroot!'))
sys.exit()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment