4

How can I setup a CI/CD workflow with gitlab (or GitHub Actions) that generates my own Read the Docs site and is hosted for free using gitlab pages?

Is there a fork-ready example repo on gitlab or github that I can use to self-generate and self-host my own Read the Docs site?

Michael Altfield
  • 2,083
  • 23
  • 39

3 Answers3

10

You can host a sphinx-powered site (optionally using the Read the Docs theme) on GitHub Pages using GitHub Actions to wrap sphinx-build and push your html static assets to your GitHub Pages source, such as the gh-pages branch..

You need to define a GitHub Actions workflow to execute a build script.

Here's an example workflow that will execute buildDocs.sh every time there's a push to master

name: docs_pages_workflow
 
# execute this workflow automatically when a we push to master
on:
  push:
    branches: [ master ]
 
jobs:
 
  build_docs_job:
    runs-on: ubuntu-latest
    container: debian:buster-slim
 
    steps:
 
    - name: Prereqs
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        apt-get update
        apt-get install -y git
        git clone --depth 1 "https://token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" .
      shell: bash
 
    - name: Execute script to build our documentation and update pages
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: "docs/buildDocs.sh"
      shell: bash

And here's an example buildDocs.sh script that's executed by the workflow above:

#!/bin/bash
################################################################################
# File:    buildDocs.sh
# Purpose: Script that builds our documentation using sphinx and updates GitHub
#          Pages. This script is executed by:
#            .github/workflows/docs_pages_workflow.yml
#
# Authors: Michael Altfield <michael@michaelaltfield.net>
# Created: 2020-07-17
# Updated: 2020-07-17
# Version: 0.1
################################################################################
 
###################
# INSTALL DEPENDS #
###################
 
apt-get update
apt-get -y install git rsync python3-sphinx python3-sphinx-rtd-theme
 
#####################
# DECLARE VARIABLES #
#####################
 
pwd
ls -lah
export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)
 
##############
# BUILD DOCS #
##############
 
# build our documentation with sphinx (see docs/conf.py)
# * https://www.sphinx-doc.org/en/master/usage/quickstart.html#running-the-build
make -C docs clean
make -C docs html
 
#######################
# Update GitHub Pages #
#######################
 
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
 
docroot=`mktemp -d`
rsync -av "docs/_build/html/" "${docroot}/"
 
pushd "${docroot}"
 
# don't bother maintaining history; just generate fresh
git init
git remote add deploy "https://token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git checkout -b gh-pages
 
# add .nojekyll to the root so that github won't 404 on content added to dirs
# that start with an underscore (_), such as our "_content" dir..
touch .nojekyll
 
# Add README
cat > README.md <<EOF
# GitHub Pages Cache
 
Nothing to see here. The contents of this branch are essentially a cache that's not intended to be viewed on github.com.
 
 
If you're looking to update our documentation, check the relevant development branch's 'docs/' dir.
 
For more information on how this documentation is built using Sphinx, Read the Docs, and GitHub Actions/Pages, see:
 
 * https://tech.michaelaltfield.net/2020/07/18/sphinx-rtd-github-pages-1
EOF
 
# copy the resulting html pages built from sphinx above to our new git repo
git add .
 
# commit all the new files
msg="Updating Docs for commit ${GITHUB_SHA} made on `date -d"@${SOURCE_DATE_EPOCH}" --iso-8601=seconds` from ${GITHUB_REF} by ${GITHUB_ACTOR}"
git commit -am "${msg}"
 
# overwrite the contents of the gh-pages branch on our github.com repo
git push deploy gh-pages --force
 
popd # return to main repo sandbox root

I wrote an article that describes how to run your own Read the Docs site on GitHub Pages that describes the above files in more detail.

Michael Altfield
  • 2,083
  • 23
  • 39
3

I adapted @Michael Altfield's solution into a single GitHub Action:

name: docs_pages_workflow 

on: 
  push:
    branches: [ main ]   

jobs:

  build_docs_job:
    runs-on: ubuntu-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

  steps:
    - name: Checkout
      uses: actions/checkout@v2.3.4

    - name: Set up Python
      uses: actions/setup-python@v2.2.1
      with:
        python-version: 3.9

    - name: Install dependencies
      run: |
        python -m pip install -U sphinx
        python -m pip install sphinx-rtd-theme
    
    - name: make the sphinx docs
      run: |
        make -C docs clean
        make -C docs html
              
    - name: Init new repo in dist folder and commit
      run: |
        cd docs/build/html/
        git init
        touch .nojekyll
        git add -A
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git commit -m 'deploy'

    - name: Force push to destination branch
      uses: ad-m/github-push-action@v0.5.0
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: gh-pages
        force: true
        directory: ./docs/build/html

Note, my Makefile builds to build not _build directory. The last line with directory: is saying to push from the .docs/build/html directory where we just created the new Git repo. This avoids his rsync and pushd commands. Otherwise the logic follows @Michael Altfield's solution.

Eli Holmes
  • 656
  • 7
  • 10
1

I wrote sphinx-notes/pages actions to do this. Here is an example of workflow:

name: Deploy Sphinx documentation to Pages

on:
  push:
    branches: [master] # branch to trigger deployment

jobs:
  pages:
    runs-on: ubuntu-20.04
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    permissions:
      pages: write
      id-token: write
    steps:
    - id: deployment
      uses: sphinx-notes/pages@v3
SilverRainZ
  • 156
  • 5