Skip to content

Extension for Ansible redirects #2418

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

Merged
merged 17 commits into from
Jun 3, 2025
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
8 changes: 8 additions & 0 deletions .github/workflows/build-package-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ name: Build and deploy docs
- '11'
- '10'
- '9'
generate-redirects:
description: Generate page redirects
type: boolean
default: true
deploy:
description: Deploy the build
type: boolean
required: true
default: false
deployment-environment:
description: Deployment environment
type: choice
Expand All @@ -54,6 +59,9 @@ jobs:
repository-name: ${{ github.event.inputs.repository-name || 'ansible-documentation' }}
repository-branch: ${{ github.event.inputs.repository-branch || 'devel' }}
ansible-package-version: ${{ github.event.inputs.ansible-package-version || 'devel' }}
generate-redirects: >-
${{ github.event_name == 'workflow_dispatch'
&& github.event.inputs.generate-redirects == 'true' }}
secrets:
DOCS_BOT_TOKEN: ${{ secrets.DOCS_BOT_TOKEN }}

Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/reusable-build-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ name: Build docs
description: Ansible community package version
required: false
default: devel
generate-redirects:
description: Generate page redirects
type: boolean
default: true
secrets:
DOCS_BOT_TOKEN:
required: true
Expand Down Expand Up @@ -69,7 +73,9 @@ jobs:

- name: Build the Ansible community package docs
run: >-
make webdocs ANSIBLE_VERSION="${{
make webdocs ${{
inputs.generate-redirects && 'EXTRA_TAGS="-t redirects"' || ''
}} ANSIBLE_VERSION="${{
env.PACKAGE_VERSION != 'devel' && env.PACKAGE_VERSION || ''
}}"
working-directory: build-directory/docs/docsite
Expand Down
1 change: 1 addition & 0 deletions docs/docsite/.templates/redirect_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><meta name="robots" content="noindex"><noscript><meta http-equiv="refresh" content="0; url=${to_uri}" /></noscript><script>var target = "${to_uri}";if (window.location.hash) {window.location.replace(target + window.location.hash);} else {window.location.replace(target);}</script></head></html>
3 changes: 2 additions & 1 deletion docs/docsite/Makefile.sphinx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#

# You can set these variables from the command line.
EXTRA_TAGS ?=
DOCS_VARIANTS ?=
SPHINXCONFDIR = rst
LANGOPTS ?=
SPHINXOPTS ?= -j $(CPUS) -n -w rst_warnings -c "$(SPHINXCONFDIR)" $(DOCS_VARIANTS) $(LANGOPTS)
SPHINXOPTS ?= -j $(CPUS) -n -w rst_warnings -c "$(SPHINXCONFDIR)" $(DOCS_VARIANTS) $(EXTRA_TAGS) $(LANGOPTS)
SPHINXBUILD = sphinx-build
SPHINXPROJ = sdfsdf
SOURCEDIR = rst
Expand Down
451 changes: 451 additions & 0 deletions docs/docsite/declarative-configs/ansible_redirects.toml

Large diffs are not rendered by default.

27 changes: 26 additions & 1 deletion docs/docsite/rst/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

import sys
import os
import tomllib
from pathlib import Path

from sphinx.application import Sphinx

DOCS_ROOT_DIR = Path(__file__).parent.resolve()

# If your extensions are in another directory, add it here. If the directory
# is relative to the documentation root, use os.path.abspath to make it
Expand Down Expand Up @@ -376,7 +382,7 @@
autoclass_content = 'both'

# Note: Our strategy for intersphinx mappings is to have the upstream build location as the
# canonical source.
# canonical source.
intersphinx_mapping = {
'python': ('https://docs.python.org/2/', None),
'python3': ('https://docs.python.org/3/', None),
Expand All @@ -390,3 +396,22 @@
]
linkcheck_workers = 25
# linkcheck_anchors = False

# Generate redirects for pages when building on Read The Docs
def setup(app: Sphinx) -> dict[str, bool | str]:

if 'redirects' in app.tags:

redirects_config_path = DOCS_ROOT_DIR.parent / "declarative-configs" / "ansible_redirects.toml"
redirects = tomllib.loads(redirects_config_path.read_text())
redirect_template = DOCS_ROOT_DIR.parent / ".templates" / "redirect_template.html"

app.config.redirects = redirects
app.config.redirect_html_template_file = redirect_template
app.setup_extension('sphinx_reredirects') # redirect pages that have been restructured or removed

return {
"parallel_read_safe": True,
"parallel_write_safe": True,
"version": app.config.release,
}
1 change: 1 addition & 0 deletions tests/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sphinx
sphinx-intl # translation utility used by docs/docsite/Makefile
sphinx-notfound-page # extension used for the custom 404 page (cowsay)
sphinx-ansible-theme # extension used for the custom docs theme
sphinx-reredirects # extension to create redirects for moved pages
sphinx-rtd-theme
rstcheck
yamllint
Expand Down
3 changes: 3 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ sphinx==7.2.5
# sphinx-copybutton
# sphinx-intl
# sphinx-notfound-page
# sphinx-reredirects
# sphinx-rtd-theme
# sphinxcontrib-jquery
sphinx-ansible-theme==0.10.3
Expand All @@ -174,6 +175,8 @@ sphinx-intl==2.3.1
# via -r tests/requirements.in
sphinx-notfound-page==1.1.0
# via -r tests/requirements.in
sphinx-reredirects==0.1.6
# via -r tests/requirements.in
sphinx-rtd-theme==3.0.2
# via
# -c tests/constraints.in
Expand Down