Skip to content

Commit b7be785

Browse files
committed
Add option ci_https_proxy and use it to define .gitlab-ci.yml so that GitLab can run its CI pipeline.
Delete .gitlab-ci.yml if we are not using GitLab. If we put null as the default, then for some reason that argument is required instead of defaulting to null. If the user sets an SSH_PRIVATE_KEY, then set up SSH. Use prebuilt dahanna/python.3.7-git-tox-alpine with git and tox already installed. Deploy GitLab Pages even if tests fail, because if we include a link to the documentation in the README, then sphinx linkcheck will fail, so the pages will never get deployed, so sphinx linkcheck will keep failing. Use python -m sphinx, so that we can preinstall sphinx and use it with --sitepackages.
1 parent 2bbe9f9 commit b7be785

6 files changed

Lines changed: 138 additions & 4 deletions

File tree

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ You will be asked for these fields:
139139
- Use ``"no"`` for no hosting (various links will disappear). You can also use ``"gitlab.com"`` and such but various
140140
things will be broken (like Travis configuration).
141141

142+
* - ``ci_https_proxy``
143+
- .. code:: python
144+
145+
null
146+
- If your CI runner requires an HTTPS proxy, then you can specify it here.
147+
142148
* - ``repo_name``
143149
- .. code:: python
144150

cookiecutter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"repo_name": "python-{{ cookiecutter.project_name|lower|replace(' ','-') }}",
77
"repo_hosting": ["github.com", "gitlab.com", "other domain not listed"],
88
"repo_hosting_domain": "{{ cookiecutter.repo_hosting if cookiecutter.repo_hosting != 'other domain not listed' else '' }}",
9+
"ci_https_proxy": "no",
910
"repo_username": "ionelmc",
1011
"package_name": "{{ cookiecutter.project_name|lower|replace(' ','_')|replace('-','_') }}",
1112
"distribution_name": "{{ cookiecutter.package_name|replace('_','-') }}",

hooks/post_gen_project.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def unlink_if_exists(path):
9797
unlink_if_exists('.travis.yml')
9898
{% endif %}
9999

100+
{%- if 'gitlab' not in cookiecutter.repo_hosting %}
101+
os.unlink('.gitlab-ci.yml')
102+
{% endif %}
103+
100104
{%- if cookiecutter.repo_hosting == 'no' %}
101105
os.unlink('CONTRIBUTING.rst')
102106
{% endif %}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
image: dahanna/python.3.7-git-tox-alpine
2+
3+
{% if cookiecutter.ci_https_proxy != "no" -%}
4+
variables:
5+
# HTTP_PROXY is used to pull docker images.
6+
HTTP_PROXY: {{cookiecutter.ci_https_proxy}}
7+
# HTTPS_PROXY is used to install Python packages such as tox.
8+
HTTPS_PROXY: {{cookiecutter.ci_https_proxy}}
9+
# If we don't include NO_PROXY, we will get "fatal unable to update url base from redirection"
10+
# when trying to fetch the gitlab-ci-token.
11+
NO_PROXY: 127.0.0.1,localhost,.lan,.local,.home,/var/run/docker.sock,{{cookiecutter.repo_hosting_domain}}
12+
# If some of the links in your documentation require a special PEM to verify,
13+
# then sphinx -b linkcheck will fail without that PEM.
14+
# But setting REQUESTS_CA_BUNDLE to that PEM will cause other links to fail,
15+
# because the runner will only accept that PEM, not the defaults.
16+
# Therefore you will usually want to bundle all certificates together with
17+
# cat `python -c "import requests; print(requests.certs.where())"` ~/your.pem > ~/bundled.pem
18+
# REQUESTS_CA_BUNDLE: ci/name-of-your-ca-bundle.pem
19+
{%- endif %}
20+
21+
default:
22+
before_script:
23+
- right_after_pull_docker_image=$(date +%s)
24+
- if [ -z ${SSH_PRIVATE_KEY+ABC} ]; then echo "SSH_PRIVATE_KEY is unset, so assuming you do not need SSH set up.";
25+
else
26+
# All of this will be skipped unless you set SSH_PRIVATE_KEY as a variable at https://{{ cookiecutter.repo_hosting_domain }}/{{ cookiecutter.repo_username }}/{{ cookiecutter.repo_name }}/-/settings/ci_cd
27+
{% raw -%}
28+
- if [ ${#SSH_PRIVATE_KEY} -le 5 ]; then echo "SSH_PRIVATE_KEY looks far too short, something is wrong"; fi
29+
{%- endraw %}
30+
- apk add openssh-client || apt-get install --assume-yes openssh-client
31+
- echo "adding openssh-client took $(( $(date +%s) - right_after_pull_docker_image)) seconds"
32+
33+
# ssh-agent -s starts the ssh-agent and then outputs shell commands to run.
34+
- eval $(ssh-agent -s)
35+
36+
##
37+
## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store.
38+
## We're using tr to fix line endings which makes ed25519 keys work
39+
## without extra base64 encoding.
40+
## We use -d because the version of tr on alpine does not recognize --delete.
41+
## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
42+
##
43+
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
44+
45+
##
46+
## Create the SSH directory and give it the right permissions
47+
##
48+
- mkdir --parents ~/.ssh
49+
- ssh-keyscan -t rsa {{ cookiecutter.repo_hosting_domain }} >> ~/.ssh/known_hosts
50+
- fi
51+
52+
##
53+
## Optionally, if you will be using any Git commands, set the user name and
54+
## and email.
55+
##
56+
#- git config --global user.email "{{ cookiecutter.email }}"
57+
#- git config --global user.name "{{ cookiecutter.full_name }}"
58+
59+
# In general we want to use tox -e docs, but GitLab.com will not deploy Pages
60+
# if the pages build fails.
61+
# The pages build will fail if you use tox -e docs with a link to your GitLab
62+
# Pages documentation that is not yet deployed, because tox -e docs includes
63+
# sphinx-build -b linkcheck. So the pages will never get deployed...
64+
# That's why we deploy pages with no checks here.
65+
# The tests will still run linkcheck on the documentation.
66+
# Since "It may take up to 30 minutes before the site is available after the
67+
# first deployment." (per GitLab), the tests will still fail for a little
68+
# while.
69+
pages:
70+
tags:
71+
- docker
72+
stage: build
73+
# On GitLab, the stages are build->test->deploy.
74+
# If the test stage fails, the deploy stage is skipped.
75+
script:
76+
- pip install -r docs/requirements.txt
77+
- sphinx-build -E -b html docs dist/docs
78+
- mv dist/docs/ public/
79+
artifacts:
80+
paths:
81+
- public
82+
only:
83+
- master
84+
85+
test:
86+
tags:
87+
- docker
88+
stage: test
89+
script:
90+
# apk add any needed packages not included in the image.
91+
# check-manifest, used in tox -e check, requires git,
92+
# so we need to either use an image that includes git or
93+
# apk add git here.
94+
- pip install --upgrade pip
95+
# If using an image that does not include tox, we will
96+
# need to pip install tox here.
97+
# With --sitepackages, we can save time by installing once
98+
# for both regular tests and documentation checks.
99+
- pip install .
100+
- git --version
101+
- python --version
102+
- python2 --version || echo "python2 is not installed."
103+
- virtualenv --version
104+
- pip --version
105+
- tox --version
106+
- uname --all
107+
- lsb_release --all || echo "lsb_release is not supported on this host."
108+
- start_tox=$(date +%s)
109+
# When testing locally, we might not want to set sitepackages=true,
110+
# because the local machine might have all kinds of weird things in the
111+
# environment. But for continuous integration, we do want sitepackages=true,
112+
# because it allows us to use a Docker image with some packages already
113+
# installed to accelerate testing.
114+
- tox --sitepackages
115+
- echo "tox tests took $(( $(date +%s) - start_tox)) seconds"
116+
- echo "Everything after pulling the Docker image took $(( $(date +%s) - right_after_pull_docker_image)) seconds total"
117+

{{cookiecutter.repo_name}}/MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ include README.rst
1616
include pyproject.toml
1717

1818
include tox.ini .travis.yml .appveyor.yml
19+
{% if 'gitlab' in cookiecutter.repo_hosting -%}
20+
include .gitlab-ci.yml
21+
{%- endif %}
1922

2023
global-exclude *.py[cod] __pycache__/* *.so *.dylib

{{cookiecutter.repo_name}}/tox.ini

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,16 @@ commands =
127127
setenv =
128128
SPELLCHECK=1
129129
commands =
130-
sphinx-build -b spelling docs dist/docs
130+
python -m sphinx -b spelling docs dist/docs
131131
skip_install = true
132132
deps =
133133
-r{toxinidir}/docs/requirements.txt
134134
sphinxcontrib-spelling
135135
pyenchant
136136

137+
# Using python -m sphinx instead of sphinx-build allows us to use --sitepackages if we want.
138+
# https://tox.readthedocs.io/en/latest/config.html#conf-sitepackages
139+
137140
[testenv:docs]
138141
usedevelop = true
139142
{%- if cookiecutter.setup_py_uses_setuptools_scm == 'yes' %}
@@ -144,10 +147,10 @@ deps =
144147
-r{toxinidir}/docs/requirements.txt
145148
commands =
146149
{%- if cookiecutter.sphinx_doctest == "yes" %}
147-
sphinx-build {posargs:-E} -b doctest docs dist/docs
150+
python -m sphinx {posargs:-E} -b doctest docs dist/docs
148151
{%- endif %}
149-
sphinx-build {posargs:-E} -b html docs dist/docs
150-
sphinx-build -b linkcheck docs dist/docs
152+
python -m sphinx {posargs:-E} -b html docs dist/docs
153+
python -m sphinx -b linkcheck docs dist/docs
151154
{%- endif %}
152155
{%- if cookiecutter.coveralls == 'yes' %}
153156

0 commit comments

Comments
 (0)