Migrate from Travis-CI and Readthedocs to Github actions

python
github
Published

September 1, 2020

For a number of years I have been concerned about the duplication of work having to maintain continuous integration environments both on Travis-CI to run the unit tests and on Readthedocs to build and host the documentation.

The solution I am proposing is to use Github actions to replace both systems. I chose Github actions instead of Travis-CI because they are easier to configure, have better log-viewing interface and surprisingly enough they are better integrated with Github!

The biggest hurdle was to find a way to reproduce the capability of readthedocs to host multiple versions of the documentation together, fortunately the sphinx-multiversion gives a very similar functionality.

Next I’ll be sharing an example configuration for Github actions that integrates:

# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
name: Python package
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7]
steps:
- uses: actions/checkout@v2
with:
submodules: true
fetch-depth: 0
persist-credentials: false
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install libcfitsio
run: sudo apt-get install libcfitsio-dev
- name: Install pip dependencies
run: |
python -m pip install -r requirements.txt -r test-requirements.txt
- name: Build
run: python setup.py build_ext -i && python setup.py install
env:
C_INCLUDE_PATH: /usr/include/cfitsio/
- name: Test
run: |
pytest
- name: Build docs
if: ${{ matrix.python-version==3.6 }}
run: |
python -m pip install -r docs/requirements.txt && sphinx-multiversion docs html
- name: Install SSH Client 🔑
if: ${{ matrix.python-version==3.6 }}
uses: webfactory/ssh-agent@v0.2.0
with:
ssh-private-key: ${{ secrets.DEPLOY_KEY }}
- name: Deploy 🚀
if: ${{ matrix.python-version==3.6 && contains(github.ref, 'master') }}
uses: JamesIves/github-pages-deploy-action@3.5.7
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: master
FOLDER: html
REPOSITORY_NAME: MYORG/MYREPO
SSH: true

Requirements

  • Create a docs/requirements.txt file which includes sphinx-multiversion, I directly use https://github.com/Holzhaus/sphinx-multiversion/archive/master.zip

  • Follow the sphinx-multiversion documentation to configure docs/conf.py, for example my configuration is:

      ```

    Sphinx multiversion configuration

    extensions += [“sphinx_multiversion”]

templates_path = [ “_templates”, ] #html_sidebars = {’**’: [ # “versioning.html”, # ]}

Whitelist pattern for tags (set to None to ignore all tags) smv_tag_whitelist = r’^.*

Whitelist pattern for branches (set to None to ignore all branches) smv_branch_whitelist = r’^master$’

Whitelist pattern for remotes (set to None to use local branches only) smv_remote_whitelist = None

Pattern for released versions smv_released_pattern = r’^.tags.$’

Format for versioned output directories inside the build directory smv_outputdir_format = ‘{ref.name}’

Determines whether remote or local git branches/tags are preferred if their output dirs conflict smv_prefer_remote_refs = False

    ```
  • If necessary, customize the templates, for example I added localtoc.html and page.html to mine, see this gist
  • Decide where you want to host the docs, I created a dedicated repository (same name of the python package replacing _ by -), created a Github deploy key on it, and added it as DEPLOY_KEY within the secrets of the Github repository so that it is available in the Github action (GITHUB_TOKEN instead is automatically created).