From dd30d8111a44d60ed759b0ed17ed6b68d377867f Mon Sep 17 00:00:00 2001 From: Benedikt Date: Fri, 20 Dec 2024 16:56:12 +0000 Subject: [PATCH] Adding version switcher to CI --- {docs => .github/pages}/index.html | 0 .github/pages/switcher.py | 41 ++++++++++++++++++++++++++++ .github/workflows/_build_docs.yaml | 6 ---- .github/workflows/_github_pages.yaml | 2 +- .github/workflows/_switcher.yaml | 32 ++++++++++++++++++++++ .github/workflows/docs.yaml | 8 +++++- 6 files changed, 81 insertions(+), 8 deletions(-) rename {docs => .github/pages}/index.html (100%) create mode 100644 .github/pages/switcher.py create mode 100644 .github/workflows/_switcher.yaml diff --git a/docs/index.html b/.github/pages/index.html similarity index 100% rename from docs/index.html rename to .github/pages/index.html diff --git a/.github/pages/switcher.py b/.github/pages/switcher.py new file mode 100644 index 00000000..bf8fb264 --- /dev/null +++ b/.github/pages/switcher.py @@ -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() \ No newline at end of file diff --git a/.github/workflows/_build_docs.yaml b/.github/workflows/_build_docs.yaml index 66781969..f6a32df3 100644 --- a/.github/workflows/_build_docs.yaml +++ b/.github/workflows/_build_docs.yaml @@ -50,9 +50,3 @@ jobs: with: name: docs-${{ inputs.tag }} path: artifacts - - - name: Upload index.html as Artifact - uses: actions/upload-artifact@v4.4.3 - with: - name: docs-index-html - path: docs/index.html diff --git a/.github/workflows/_github_pages.yaml b/.github/workflows/_github_pages.yaml index c754d8e5..5b71d165 100644 --- a/.github/workflows/_github_pages.yaml +++ b/.github/workflows/_github_pages.yaml @@ -14,7 +14,7 @@ jobs: - name: Setup Pages uses: actions/configure-pages@v5.0.0 - - name: Download Latest Docs Artifact + - name: Download All Docs Artifact uses: actions/download-artifact@v4.1.8 with: pattern: docs-* diff --git a/.github/workflows/_switcher.yaml b/.github/workflows/_switcher.yaml new file mode 100644 index 00000000..44b3fcbc --- /dev/null +++ b/.github/workflows/_switcher.yaml @@ -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/upload-artifact@v4.4.3 + with: + name: docs-index-html + path: .github/pages/index.html + + - name: Download All Docs Artifact + uses: actions/download-artifact@v4.1.8 + 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/upload-artifact@v4.4.3 + with: + name: docs-switcher-json + path: .github/pages/switcher.json + \ No newline at end of file diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml index 3aa6818e..7cc2ea58 100644 --- a/.github/workflows/docs.yaml +++ b/.github/workflows/docs.yaml @@ -5,6 +5,8 @@ on: branches: - master - dev + release: + type: [published] jobs: legacy_docs: @@ -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