diff --git a/.github/pages/index.html b/.github/pages/index.html deleted file mode 100644 index c495f39f2..000000000 --- a/.github/pages/index.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Redirecting to main branch - - - - - - diff --git a/.github/pages/make_switcher.py b/.github/pages/make_switcher.py deleted file mode 100755 index c06813afa..000000000 --- a/.github/pages/make_switcher.py +++ /dev/null @@ -1,96 +0,0 @@ -"""Make switcher.json to allow docs to switch between different versions.""" - -import json -import logging -from argparse import ArgumentParser -from pathlib import Path -from subprocess import CalledProcessError, check_output - - -def report_output(stdout: bytes, label: str) -> list[str]: - """Print and return something received frm stdout.""" - ret = stdout.decode().strip().split("\n") - print(f"{label}: {ret}") - return ret - - -def get_branch_contents(ref: str) -> list[str]: - """Get the list of directories in a branch.""" - stdout = check_output(["git", "ls-tree", "-d", "--name-only", ref]) - return report_output(stdout, "Branch contents") - - -def get_sorted_tags_list() -> list[str]: - """Get a list of sorted tags in descending order from the repository.""" - stdout = check_output(["git", "tag", "-l", "--sort=-v:refname"]) - return report_output(stdout, "Tags list") - - -def get_versions(ref: str, add: str | None) -> list[str]: - """Generate the file containing the list of all GitHub Pages builds.""" - # Get the directories (i.e. builds) from the GitHub Pages branch - try: - builds = set(get_branch_contents(ref)) - except CalledProcessError: - builds = set() - logging.warning(f"Cannot get {ref} contents") - - # Add and remove from the list of builds - if add: - builds.add(add) - - # Get a sorted list of tags - tags = get_sorted_tags_list() - - # Make the sorted versions list from main branches and tags - versions: list[str] = [] - for version in ["master", "main"] + tags: - if version in builds: - versions.append(version) - builds.remove(version) - - # Add in anything that is left to the bottom - versions += sorted(builds) - 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 from gh-pages directories" - ) - parser.add_argument( - "--add", - help="Add this directory to the list of existing directories", - ) - 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("origin/gh-pages", args.add) - write_json(args.output, args.repository, versions) - - -if __name__ == "__main__": - main() diff --git a/.github/workflows/_docs.yml b/.github/workflows/_docs.yml deleted file mode 100644 index acc52652b..000000000 --- a/.github/workflows/_docs.yml +++ /dev/null @@ -1,55 +0,0 @@ -on: - workflow_call: - - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Avoid git conflicts when tag and branch pushed at same time - if: github.ref_type == 'tag' - run: sleep 60 - - - name: Checkout - uses: actions/checkout@v7 - with: - # Need this to get version number from last tag - fetch-depth: 0 - - - name: Install system packages - run: sudo apt-get install graphviz - - - name: Install uv - uses: astral-sh/setup-uv@v10.3.2 - - - name: Build docs - run: uv run --locked tox -e docs - - - name: Remove environment.pickle - run: rm build/html/.doctrees/environment.pickle - - - name: Upload built docs artifact - uses: actions/upload-artifact@v7 - with: - name: docs - path: build - - - name: Sanitize ref name for docs version - run: echo "DOCS_VERSION=${GITHUB_REF_NAME//[^A-Za-z0-9._-]/_}" >> $GITHUB_ENV - - - name: Move to versioned directory - run: mv build/html .github/pages/$DOCS_VERSION - - - name: Write switcher.json - run: python .github/pages/make_switcher.py --add $DOCS_VERSION ${{ github.repository }} .github/pages/switcher.json - - - name: Publish Docs to gh-pages - if: github.ref_type == 'tag' || github.ref_name == 'main' - # We pin to the SHA, not the tag, for security reasons. - # https://docs.github.com/en/actions/learn-github-actions/security-hardening-for-github-actions#using-third-party-actions - uses: peaceiris/actions-gh-pages@84c30a85c19949d7eee79c4ff27748b70285e453 # v4.1.0 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - publish_dir: .github/pages - keep_files: true diff --git a/.github/workflows/_release.yml b/.github/workflows/_release.yml deleted file mode 100644 index 59b929293..000000000 --- a/.github/workflows/_release.yml +++ /dev/null @@ -1,32 +0,0 @@ -on: - workflow_call: - -jobs: - artifacts: - runs-on: ubuntu-latest - - steps: - - name: Download artifacts - uses: actions/download-artifact@v8 - with: - merge-multiple: true - - - name: Zip up docs - run: | - set -vxeuo pipefail - if [ -d html ]; then - mv html $GITHUB_REF_NAME - zip -r docs.zip $GITHUB_REF_NAME - rm -rf $GITHUB_REF_NAME - fi - - - name: Create GitHub Release - # We pin to the SHA, not the tag, for security reasons. - # https://docs.github.com/en/actions/learn-github-actions/security-hardening-for-github-actions#using-third-party-actions - uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3.0.3 - with: - prerelease: ${{ contains(github.ref_name, 'a') || contains(github.ref_name, 'b') || contains(github.ref_name, 'rc') }} - files: "*" - generate_release_notes: true - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2ccb56911..abb1178fa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,12 +1,10 @@ name: CI on: - push: - branches: - - main - tags: - - '*' pull_request: + push: + branches: [main] + tags: ["*"] # '*' never matches '/', so e.g. release/x tags don't build jobs: lint: @@ -19,10 +17,12 @@ jobs: with: runs-on: ubuntu-latest - docs: - uses: ./.github/workflows/_docs.yml - permissions: - contents: write + docs: # Call the docs building workflow directly + uses: DiamondLightSource/myst-version-switcher-plugin/.github/workflows/docs.yml@v0.28.0 + with: + # Some of the docs need graphviz, so install it as well as running sphinx + build-command: sudo apt-get install -y graphviz && uv run --locked tox -e docs + html-dir: build/html example: needs: test @@ -33,7 +33,7 @@ jobs: release: needs: [test, docs] - if: github.ref_type == 'tag' - uses: ./.github/workflows/_release.yml + if: github.ref_type == 'tag' # tag pushes only + uses: DiamondLightSource/myst-version-switcher-plugin/.github/workflows/release.yml@v0.28.0 permissions: - contents: write + contents: write # create the Release + attach assets diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 000000000..73645bda3 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,37 @@ +name: Publish + +on: + # matches ci.yml's `name:`, NOT its filename + workflow_run: + workflows: [CI] + types: [completed] + + # fork-PR previews and manual re-deploys + workflow_dispatch: + inputs: + pr: + description: "Fork PR to approve + preview (empty = re-deploy)" + required: false + default: "" + +jobs: + publish: + # Run if the workflow has been manually dispatched or it was a + # successful CI run that did not come from a fork. + # Skip merge queue runs as they deploy the same commit as the PR commit + if: >- + (github.event_name == 'workflow_dispatch' || + (github.event.workflow_run.conclusion == 'success' && + github.event.workflow_run.event != 'merge_group' && + github.event.workflow_run.head_repository.full_name == github.repository)) + uses: DiamondLightSource/myst-version-switcher-plugin/.github/workflows/publish-gh-pages.yml@v0.28.0 + with: + pr: ${{ inputs.pr }} + max-releases: "30" + max-prs: "20" + permissions: + contents: read + actions: read # gather cross-run docs artifacts + pages: write + id-token: write + statuses: write # record the fork-preview approval / preview link diff --git a/copier.yml b/copier.yml index 1f074dd2e..f0272dacd 100644 --- a/copier.yml +++ b/copier.yml @@ -163,3 +163,6 @@ _migrations: - echo This update will require you to login to pypi.org and make changes before you can make a new release to PyPI. If you do not have time to do this now, press CTRL+C to abort this update. after: - echo Visit https://diamondlightsource.github.io/python-copier-template/main/how-to/pypi.html to find out how to set up PyPI trusted publishing + - version: 6.0.0 + command: echo "If your repo was using the gh-pages branch to publish docs, follow https://diamondlightsource.github.io/myst-version-switcher-plugin/main/how-to/migrate-from-gh-pages/ to publish with GitHub Actions instead." + when: "{{ _stage == 'after' and docs_type == 'sphinx' }}" diff --git a/docs/conf.py b/docs/conf.py index f78540b5b..832dab5aa 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -3,11 +3,9 @@ # This file only contains a selection of the most common options. For a full # list see the documentation: # https://www.sphinx-doc.org/en/master/usage/configuration.html - +import os import sys from importlib.metadata import version -from pathlib import Path -from subprocess import check_output import requests @@ -19,14 +17,7 @@ # The full version, including alpha/beta/rc tags. release = version(project) -# The short X.Y version. -if "+" in release: - # Not on a tag, use branch name - root = Path(__file__).absolute().parent.parent - git_branch = check_output("git branch --show-current".split(), cwd=root) - version = git_branch.decode().strip() -else: - version = release +version = os.environ.get("VERSION_NAME", "local") extensions = [ # For graphviz diagrams @@ -110,9 +101,9 @@ if not switcher_exists: print( "*** Can't read version switcher, is GitHub pages enabled? \n" - " Once Docs CI job has successfully run once, set the " - "Github pages source branch to be 'gh-pages' at:\n" - f" https://github.com/{github_user}/{github_repo}/settings/pages", + " Set the Github pages source to 'GitHub Actions' at:\n" + f" https://github.com/{github_user}/{github_repo}/settings/pages\n" + " then CI will publish the docs on its next successful run.", file=sys.stderr, ) @@ -135,15 +126,15 @@ "json_url": switcher_json, "version_match": version, }, - "check_switcher": True, - "navbar_end": ["theme-switcher", "icon-links", "version-switcher"], + "check_switcher": False, + "navbar_end": ["theme-switcher", "navbar-icon-links", "version-switcher"], } # A dictionary of values to pass into the template engine’s context for all pages html_context = { "github_user": github_user, "github_repo": github_repo, - "github_version": version, + "github_version": "main", "doc_path": "docs", } diff --git a/docs/how-to/build-docs.md b/docs/how-to/build-docs.md index 2351d0e51..655724e3a 100644 --- a/docs/how-to/build-docs.md +++ b/docs/how-to/build-docs.md @@ -41,8 +41,12 @@ $ tox -e docs-autobuild -- --watch tests (building-docs-in-ci)= ## Building docs in CI -After a successful run of CI +For a successful docs build, the source for the 'Build and Deployment' of the GitHub Pages should be set to GitHub Actions (not 'Deploy from a branch'). This should be changed first, then CI will publish. Settings > Pages -![Setup GitHub Pages](../images/gh-pages-setup.png) +![Setup GitHub Pages](../images/github-pages-actions-setup.png) + +If your repo was using the `gh-pages` branch to publish docs, follow the [migration instructions](https://diamondlightsource.github.io/myst-version-switcher-plugin/main/how-to/migrate-from-gh-pages) to now use Github Actions. + +Docs are built using the [myst-version-switcher-plugin](https://github.com/DiamondLightSource/myst-version-switcher-plugin). diff --git a/docs/images/gh-pages-setup.png b/docs/images/gh-pages-setup.png deleted file mode 100644 index db083b7e7..000000000 Binary files a/docs/images/gh-pages-setup.png and /dev/null differ diff --git a/docs/images/github-pages-actions-setup.png b/docs/images/github-pages-actions-setup.png new file mode 100644 index 000000000..5727de282 Binary files /dev/null and b/docs/images/github-pages-actions-setup.png differ diff --git a/pyproject.toml b/pyproject.toml index 8fdd854ef..6bd993913 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,9 @@ build-backend = "setuptools.build_meta" [project] name = "python-copier-template" +description = "Diamond's opinionated copier template for pure Python projects." dynamic = ["version"] +readme = "README.md" requires-python = ">=3.11" [dependency-groups] diff --git a/template/renovate.json.jinja b/template/renovate.json.jinja index 5e52f7a6b..34d9457a3 100644 --- a/template/renovate.json.jinja +++ b/template/renovate.json.jinja @@ -22,15 +22,14 @@ "actions/checkout", "astral-sh/setup-uv", "actions/upload-artifact", - "actions/download-artifact", - "softprops/action-gh-release", + "DiamondLightSource/myst-version-switcher-plugin", "codecov/codecov-action"{% if docker %}, "docker/setup-buildx-action", "docker/login-action", "docker/build-push-action", "docker/metadata-action"{% endif %}{% if pypi %}, - "pypa/gh-action-pypi-publish"{% endif %}{% if sphinx %}, - "peaceiris/actions-gh-pages"{% endif %} + "pypa/gh-action-pypi-publish", + "actions/download-artifact"{% endif %} ], "matchManagers": [ "github-actions" diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/pages" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/pages" deleted file mode 120000 index 765622ebd..000000000 --- "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/pages" +++ /dev/null @@ -1 +0,0 @@ -../../.github/pages \ No newline at end of file diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/_release.yml" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/_release.yml" deleted file mode 120000 index d5d5c173f..000000000 --- "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/_release.yml" +++ /dev/null @@ -1 +0,0 @@ -../../../.github/workflows/_release.yml \ No newline at end of file diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/ci.yml.jinja" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/ci.yml.jinja" index 49133a9e1..df048b81f 100644 --- "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/ci.yml.jinja" +++ "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/ci.yml.jinja" @@ -8,7 +8,7 @@ on: - '*' pull_request: merge_group: - + jobs: lint: @@ -38,10 +38,12 @@ jobs: packages: write {% endraw %}{% endif %}{% if sphinx %} docs: - uses: ./.github/workflows/_docs.yml - permissions: - contents: write -{% endif %} + uses: DiamondLightSource/myst-version-switcher-plugin/.github/workflows/docs.yml@v0.28.0 + with: + # Some of the docs need graphviz, so install it as well as running sphinx + build-command: sudo apt-get install -y graphviz && uv run --locked tox -e docs + html-dir: build/html + {% endif %} dist: uses: ./.github/workflows/_dist.yml {% if pypi %} @@ -55,6 +57,6 @@ jobs: release: needs: [dist, test{% if sphinx %}, docs{% endif %}] if: github.ref_type == 'tag' - uses: ./.github/workflows/_release.yml + uses: DiamondLightSource/myst-version-switcher-plugin/.github/workflows/release.yml@v0.28.0 permissions: contents: write diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if sphinx %}_docs.yml{% endif %}" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if sphinx %}_docs.yml{% endif %}" deleted file mode 120000 index 6f47d301b..000000000 --- "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if sphinx %}_docs.yml{% endif %}" +++ /dev/null @@ -1 +0,0 @@ -../../../.github/workflows/_docs.yml \ No newline at end of file diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if sphinx %}publish.yml{% endif %}" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if sphinx %}publish.yml{% endif %}" new file mode 100644 index 000000000..73645bda3 --- /dev/null +++ "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if sphinx %}publish.yml{% endif %}" @@ -0,0 +1,37 @@ +name: Publish + +on: + # matches ci.yml's `name:`, NOT its filename + workflow_run: + workflows: [CI] + types: [completed] + + # fork-PR previews and manual re-deploys + workflow_dispatch: + inputs: + pr: + description: "Fork PR to approve + preview (empty = re-deploy)" + required: false + default: "" + +jobs: + publish: + # Run if the workflow has been manually dispatched or it was a + # successful CI run that did not come from a fork. + # Skip merge queue runs as they deploy the same commit as the PR commit + if: >- + (github.event_name == 'workflow_dispatch' || + (github.event.workflow_run.conclusion == 'success' && + github.event.workflow_run.event != 'merge_group' && + github.event.workflow_run.head_repository.full_name == github.repository)) + uses: DiamondLightSource/myst-version-switcher-plugin/.github/workflows/publish-gh-pages.yml@v0.28.0 + with: + pr: ${{ inputs.pr }} + max-releases: "30" + max-prs: "20" + permissions: + contents: read + actions: read # gather cross-run docs artifacts + pages: write + id-token: write + statuses: write # record the fork-preview approval / preview link diff --git a/template/{% if sphinx %}docs{% endif %}/conf.py.jinja b/template/{% if sphinx %}docs{% endif %}/conf.py.jinja index 92ea6b5f1..23e81ea7c 100644 --- a/template/{% if sphinx %}docs{% endif %}/conf.py.jinja +++ b/template/{% if sphinx %}docs{% endif %}/conf.py.jinja @@ -5,9 +5,8 @@ list see the documentation: https://www.sphinx-doc.org/en/master/usage/configuration.html """ +import os import sys -from pathlib import Path -from subprocess import check_output import requests @@ -21,14 +20,7 @@ project = "{{ repo_name }}" # The full version, including alpha/beta/rc tags. release = {{ package_name }}.__version__ -# The short X.Y version. -if "+" in release: - # Not on a tag, use branch name - root = Path(__file__).absolute().parent.parent - git_branch = check_output("git branch --show-current".split(), cwd=root) - version = git_branch.decode().strip() -else: - version = release +version = os.environ.get("VERSION_NAME", "local") extensions = [ # Use this for generating API docs @@ -135,9 +127,9 @@ switcher_exists = requests.get(switcher_json).ok if not switcher_exists: print( "*** Can't read version switcher, is GitHub pages enabled? \n" - " Once Docs CI job has successfully run once, set the " - "Github pages source branch to be 'gh-pages' at:\n" - f" https://github.com/{github_user}/{github_repo}/settings/pages", + " Set the GitHub Pages source to 'GitHub Actions' at:\n" + f" https://github.com/{github_user}/{github_repo}/settings/pages\n" + " then CI will publish the docs on its next successful run.", file=sys.stderr, ) @@ -168,7 +160,7 @@ html_theme_options = { "version_match": version, }, "check_switcher": False, - "navbar_end": ["theme-switcher", "icon-links", "version-switcher"], + "navbar_end": ["theme-switcher", "navbar-icon-links", "version-switcher"], "navigation_with_keys": False, } @@ -176,7 +168,7 @@ html_theme_options = { html_context = { "github_user": github_user, "github_repo": github_repo, - "github_version": version, + "github_version": "main", "doc_path": "docs", } diff --git a/tests/test_example.py b/tests/test_example.py index 98b487a00..9434978bb 100644 --- a/tests/test_example.py +++ b/tests/test_example.py @@ -367,6 +367,11 @@ def test_renovate_actions_match_what_is_shipped(override: dict, tmp_path: Path): for workflow_file in (tmp_path / ".github" / "workflows").glob("*.yml"): workflow = yaml.safe_load(workflow_file.read_text()) for job in workflow.get("jobs", {}).values(): + # Call reusable workflows with a job-level 'uses'. Skip local ones + job_uses = job.get("uses") + if job_uses and not job_uses.startswith("./"): + repo = "/".join(job_uses.split("@")[0].split("/")[:2]) + used_github_actions.add(repo) for step in job.get("steps", []): action = step.get("uses") if action: