/
deploy.py
83 lines (73 loc) · 2.41 KB
/
deploy.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
# -*- coding: utf-8 -*-
import logging
import os
import click
from .main import main
from .tools import check_call
from .tools import log_cmd
_logger = logging.getLogger(__name__)
@click.command(
help="Deploy sphinx docs to gh_pages branch by pulling from output dir."
)
@click.option(
'-b', '--branches',
help='Comma separated list of branches to build on',
default='master',
show_default=True
)
@click.option(
'-c', '--cname',
help='Write a CNAME file with the given CNAME.'
)
@click.option(
'-m', '--message',
default='Update documentation',
help='The commit message to use on the target branch.',
show_default=True
)
@click.option(
'-x', '--deploy-host',
envvar='DEPLOY_HOST',
default='github.com',
help='Specify a custom domain for GitHub, useful for enterprise domains.',
show_default=True,
)
@click.pass_context
def deploy(ctx, branches, cname, message, deploy_host):
"""
Deploy built docs to gh-pages, uses ``GH_TOKEN`` for pushing built
documentation files located in *target/doc* to gh
If the environment variable ``GH_REPO_SLUG`` is set, the docs will be
deployed to the specified github repository. Otherwise, the environment
variable of the travis build will be used (``TRAVIS_REPO_SLUG``).
"""
branch = os.environ.get('TRAVIS_BRANCH', '')
pr = os.environ.get('TRAVIS_PULL_REQUEST', '')
token = os.environ.get('GH_TOKEN', '')
repo = os.environ.get('GH_REPO_SLUG')
if repo is None:
repo = os.environ.get('TRAVIS_REPO_SLUG', '')
tag = os.environ.get('TRAVIS_TAG', '')
is_pytest_mode = 'PYTEST' in os.environ
outdir = ctx.obj['outdir']
if token is None:
click.ClickException("ERROR: GH_TOKEN is missing!")
branches = branches.split(',')
if (branch in branches and (pr == 'false')) or tag:
remote = 'https://%s@%s/%s.git' % (token, deploy_host, repo)
call = ['ghp-import', '-p', '-f', '-n', '-r', remote]
if cname:
call.extend(['-c', cname])
if message:
call.extend(['-m', message])
call.append(outdir)
_logger.info('uploading docs...')
if is_pytest_mode:
log_cmd(call, echo=True)
else:
check_call(call)
_logger.info('success!')
else:
_logger.info("Deploy triggered for non-master branch '%s': skipping "
"deploy", branch)
main.add_command(deploy)