Skip to content

Commit

Permalink
Adding version switcher to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
daurer committed Dec 20, 2024
1 parent d1be009 commit dd30d81
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
File renamed without changes.
41 changes: 41 additions & 0 deletions .github/pages/switcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Create/modify switcher.json to allow docs to switch between different versions."""

import json, os
from argparse import ArgumentParser
from pathlib import Path


def get_versions(root: str) -> list[str]:
"""Generate a list of versions."""
versions = sorted([ f.name for f in os.scandir(root) if f.is_dir() ])
print(f"Sorted versions: {versions}")
return versions


def write_json(path: Path, repository: str, versions: list[str]):
"""Write the JSON switcher to path."""
org, repo_name = repository.split("/")
struct = [
{"version": version, "url": f"https://{org}.github.io/{repo_name}/{version}/"}
for version in versions
]
text = json.dumps(struct, indent=2)
print(f"JSON switcher:\n{text}")
path.write_text(text, encoding="utf-8")


def main(args=None):
"""Parse args and write switcher."""
parser = ArgumentParser(description="Make a versions.json file")
parser.add_argument("root", type=Path, help="Path to root directory with all versions of docs")
parser.add_argument("repository", help="The GitHub org and repository name: ORG/REPO")
parser.add_argument("output", type=Path, help="Path of write switcher.json to")
args = parser.parse_args(args)

# Write the versions file
versions = get_versions(args.root)
write_json(args.output, args.repository, versions)


if __name__ == "__main__":
main()
6 changes: 0 additions & 6 deletions .github/workflows/_build_docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,3 @@ jobs:
with:
name: docs-${{ inputs.tag }}
path: artifacts

- name: Upload index.html as Artifact
uses: actions/[email protected]
with:
name: docs-index-html
path: docs/index.html
2 changes: 1 addition & 1 deletion .github/workflows/_github_pages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup Pages
uses: actions/[email protected]

- name: Download Latest Docs Artifact
- name: Download All Docs Artifact
uses: actions/[email protected]
with:
pattern: docs-*
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/_switcher.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on:
workflow_call:

jobs:
version_switcher:
runs-on: ubuntu-latest
steps:
- name: Checkout PtyPy Code
uses: actions/checkout@v4

- name: Upload index.html as Artifact
uses: actions/[email protected]
with:
name: docs-index-html
path: .github/pages/index.html

- name: Download All Docs Artifact
uses: actions/[email protected]
with:
pattern: docs-*
merge-multiple: true
path: ./doc_versions

- name: Create Switcher File
run: python .github/pages/switcher.py ./doc_versions ${{ github.repository }} .github/pages/switcher.json

- name: Upload switcher.json as Artifact
uses: actions/[email protected]
with:
name: docs-switcher-json
path: .github/pages/switcher.json

8 changes: 7 additions & 1 deletion .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches:
- master
- dev
release:
type: [published]

jobs:
legacy_docs:
Expand All @@ -17,8 +19,12 @@ jobs:
with:
tag: ${{ github.ref_name }}

github_pages:
switcher:
uses: ./.github/workflows/_switcher.yaml
needs:
- legacy_docs
- new_docs

github_pages:
needs: switcher
uses: ./.github/workflows/_github_pages.yaml

0 comments on commit dd30d81

Please sign in to comment.