Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow for multi-version sphinx docs #61

Merged
merged 18 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 146 additions & 3 deletions .github/workflows/docs_build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,57 @@ jobs:
needs: linting
runs-on: ubuntu-latest
steps:
- uses: neuroinformatics-unit/actions/build_sphinx_docs@v2
- name: Checkout
uses: actions/checkout@v4

# Need the tags so that setuptools-scm can form a valid version number
- name: Fetch git tags
run: git fetch origin 'refs/tags/*:refs/tags/*'

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}

- name: Upgrade pip
shell: bash
run: |
# install pip=>20.1 to use "pip cache dir"
python3 -m pip install --upgrade pip

- name: Get pip cache dir
shell: bash
id: pip-cache
run: echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Install dependencies
shell: bash
run: python3 -m pip install -r ./docs/requirements.txt

- name: Check links
shell: bash
run: |
sphinx-build docs/source docs/build -b linkcheck

# needs to have sphinx.ext.githubpages in conf.py extensions list
- name: Building documentation
shell: bash
run: |
sphinx-build docs/source docs/build -b html -W --keep-going

- name: Upload the content for deployment
uses: actions/upload-artifact@v4
with:
name: docs-${{ github.sha }}
path: ./docs/build/

deploy_sphinx_docs:
name: Deploy Sphinx Docs
Expand All @@ -36,6 +86,99 @@ jobs:
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- uses: neuroinformatics-unit/actions/deploy_sphinx_docs@v2
- name: Checkout
uses: actions/checkout@v4

# Fetch the built docs from the "build_sphinx_docs" job
- name: Download HTML documentation artifact
uses: actions/download-artifact@v4
with:
secret_input: ${{ secrets.GITHUB_TOKEN }}
name: docs-${{ github.sha }}
path: docs/build

- name: Checkout the gh-pages branch in a separate folder
uses: actions/checkout@v4
with:
ref: gh-pages
# Checkout to this folder instead of the current one
path: deploy
# Download the entire history
fetch-depth: 0

- name: Push the built HTML to gh-pages
run: |
# Detect if this is a release or from the main branch
echo "Event name: ${{ github.event_name }}"
echo "Ref type: ${{ github.ref_type }}"
if [ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref_type }}" = "tag" ]; then
# Get the tag name without the "refs/tags/" part
version="${GITHUB_REF#refs/*/}"
else
version=dev
fi
echo "Deploying version: $version"
# Make the new commit message. Needs to happen before cd into deploy
# to get the right commit hash.
message="Deploy $version from $(git rev-parse --short HEAD)"
cd deploy
# Need to have this file so that Github doesn't try to run Jekyll
touch .nojekyll
# Delete all the files and replace with our new set
echo -e "\nRemoving old files from previous builds of ${version}:"
rm -rvf ${version}
echo -e "\nCopying HTML files to ${version}:"
cp -Rvf ../docs/build ${version}/

if [[ "${version}" != "dev" ]]; then
# Updated switcher.json file
SWITCHER_CONTENT=$(curl https://neuroblueprint.neuroinformatics.dev/latest/_static/switcher.json)
BASE_URL="$(jq -r '.[1].url|split("/")[0:3]|join("/")' $SWITCHER_FILE)"
NEW_URL="${BASE_URL}/latest/"
# Extract the current latest version entry
FIRST_ENTRY=$(jq '.[0]' <<< "${SWITCHER_CONTENT}")
CURRENT_LATEST_VERSION=$(echo $(jq '.[1].version' <<< "${SWITCHER_CONTENT}") | tr -d '"')

# Remove the "name" field from the current latest entry
UPDATED_CURRENT_LATEST_ENTRY=$(jq --arg url "${BASE_URL}/${CURRENT_LATEST_VERSION}" '.[1] | .url = $url | del(.name)' <<< "$SWITCHER_CONTENT")

# Create the new version entry with the "latest" tag
NEW_ENTRY=$(jq -n --arg version "${version}" --arg url "$NEW_URL" --arg name "$version (latest)" \
'{name: $name, version: $version, url: $url}')

# Combine the entries in the desired order
UPDATED_SWITCHER_CONTENT=$(jq --argjson first_entry "$FIRST_ENTRY" --argjson new_entry "$NEW_ENTRY" --argjson updated_current_latest "$UPDATED_CURRENT_LATEST_ENTRY" '[$first_entry, $new_entry, $updated_current_latest] + .[2:]' <<< "$SWITCHER_CONTENT")

# Write the updated content back to switcher.json
echo "$UPDATED_SWITCHER_CONTENT" > "$SWITCHER_FILE"

echo "switcher.json has been updated successfully."
fi

# If this is a new release, update the link from /latest to it
if [[ "${version}" != "dev" ]]; then
echo -e "\nSetup link from ${version} to 'latest'."
rm -f latest
ln -sf ${version} latest
fi
# Stage the commit
git add -A .
echo -e "\nChanges to be applied:"
git status
# Configure git to be the GitHub Actions account
git config user.email "github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
# If this is a dev build and the last commit was from a dev build
# (detect if "dev" was in the previous commit message), reuse the
# same commit
if [[ "${version}" == "dev" && `git log -1 --format='%s'` == *"dev"* ]]; then
echo -e "\nAmending last commit:"
git commit --amend --reset-author -m "$message"
else
echo -e "\nMaking a new commit:"
git commit -m "$message"
fi
# Make the push quiet just in case there is anything that could leak
# sensitive information.
echo -e "\nPushing changes to gh-pages."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed this is probably better dealt with a token

git push -fq origin gh-pages 2>&1 >/dev/null
echo -e "\nFinished uploading generated files."
14 changes: 9 additions & 5 deletions docs/source/_static/switcher.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[
{
"name": "v0.3.1 (stable)",
"version": "0.3.1",
"url": "https://neuroblueprint.neuroinformatics.dev/"
}
{
"version": "dev",
"url": "https://neuroblueprint.neuroinformatics.dev/dev/"
},
{
"name": "0.3.0 (latest)",
"version": "0.3.0",
"url": "https://neuroblueprint.neuroinformatics.dev/latest/"
}
]
8 changes: 6 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
},
],
"switcher": {
"json_url": "https://neuroblueprint.neuroinformatics.dev/_static/switcher.json",
"json_url": "https://neuroblueprint.neuroinformatics.dev/latest/_static/switcher.json",
"version_match": release,
},
"logo": {
Expand All @@ -143,4 +143,8 @@
}

# link-check cannot check anchors
linkcheck_ignore = ['https://bids-specification.readthedocs.io', 'https://neuroinformatics.zulipchat.com', 'https://www.incf.org']
linkcheck_ignore = [
'https://bids-specification.readthedocs.io',
'https://neuroinformatics.zulipchat.com',
'https://www.incf.org/', # due to SSLCertVerificationError
]
Loading