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

Build: export READTHEDOCS_CANONICAL_URL variable #10166

Merged
merged 8 commits into from
Mar 22, 2023
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
2 changes: 1 addition & 1 deletion docs/user/canonical-urls.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The canonical URL takes the following into account:

* The default version of your project (usually "latest" or "stable").
* The canonical :doc:`custom domain </custom-domains>` if you have one,
otherwise the default :ref:`subdomain <default_subdomain>` will be used.
otherwise the default :ref:`subdomain <default-subdomain>` will be used.

For example, if you have a project named ``example-docs``
with a custom domain ``https://docs.example.com``,
Expand Down
2 changes: 1 addition & 1 deletion docs/user/custom-domains.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ You can serve your documentation project from your own domain,
for instance ``docs.example.com``.
This is great for maintaining a consistent brand for your product and its documentation.

.. _default_subdomain:
.. _default-subdomain:

.. rubric:: Default subdomains

Expand Down
12 changes: 12 additions & 0 deletions docs/user/reference/environment-variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ All :doc:`build processes </builds>` have the following environment variables au

:Example: ``/home/docs/checkouts/readthedocs.org/user_builds/project/envs/version``

.. envvar:: READTHEDOCS_CANONICAL_URL

Canonical base URL for the version that is built.
If the project has configured a :doc:`custom domain </custom-domains>` (e.g. ``docs.example.com``) it will be used in the resulting canonical URL.
Otherwise, your project's :ref:`default subdomain <default-subdomain>` will be used.

The path for the language and version is appended to the domain, so the final canonical base URLs can look like the following examples:

:Example: ``https://docs.example.com/en/latest/``
:Example: ``https://docs.readthedocs.io/ja/stable/``
:Example: ``https://example--17.org.readthedocs.build/fr/17/``

humitos marked this conversation as resolved.
Show resolved Hide resolved
.. seealso::

:doc:`/environment-variables`
Expand Down
10 changes: 10 additions & 0 deletions readthedocs/api/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework import serializers

from readthedocs.api.v2.utils import normalize_build_command
from readthedocs.builds.constants import EXTERNAL
from readthedocs.builds.models import Build, BuildCommandResult, Version
from readthedocs.oauth.models import RemoteOrganization, RemoteRepository
from readthedocs.projects.models import Domain, Project
Expand Down Expand Up @@ -124,11 +125,20 @@ class VersionAdminSerializer(VersionSerializer):
"""Version serializer that returns admin project data."""

project = ProjectAdminSerializer()
canonical_url = serializers.SerializerMethodField()
build_data = serializers.JSONField(required=False, write_only=True)

def get_canonical_url(self, obj):
return obj.project.get_docs_url(
lang_slug=obj.project.language,
version_slug=obj.slug,
external=obj.type == EXTERNAL,
)

class Meta(VersionSerializer.Meta):
fields = VersionSerializer.Meta.fields + [
"build_data",
"canonical_url",
]


Expand Down
3 changes: 2 additions & 1 deletion readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,8 @@ class Meta:
proxy = True

def __init__(self, *args, **kwargs):
self.project = APIProject(**kwargs.pop('project', {}))
self.project = APIProject(**kwargs.pop("project", {}))
self.canonical_url = kwargs.pop("canonical_url", None)
# These fields only exist on the API return, not on the model, so we'll
# remove them to avoid throwing exceptions due to unexpected fields
for key in ['resource_uri', 'absolute_url', 'downloads']:
Expand Down
6 changes: 6 additions & 0 deletions readthedocs/doc_builder/director.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,12 @@ def get_build_env_vars(self):
}
)

env.update(
{
"READTHEDOCS_CANONICAL_URL": self.data.version.canonical_url,
humitos marked this conversation as resolved.
Show resolved Hide resolved
}
)

# Update environment from Project's specific environment variables,
# avoiding to expose private environment variables
# if the version is external (i.e. a PR build).
Expand Down
5 changes: 5 additions & 0 deletions readthedocs/projects/tests/test_build_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ def test_get_env_vars(self, load_yaml_config, build_environment, config, externa
# Local and Circle are different values.
# We only check it's present, but not its value.
READTHEDOCS_VIRTUALENV_PATH=mock.ANY,
READTHEDOCS_CANONICAL_URL=self.project.get_docs_url(
lang_slug=self.project.language,
version_slug=self.version.slug,
external=external,
),
)
if not external:
expected_build_env_vars["PRIVATE_TOKEN"] = "a1b2c3"
Expand Down
1 change: 1 addition & 0 deletions readthedocs/rtd_tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,7 @@ def test_get_version_by_id(self):
"built": False,
"id": 18,
"active": True,
"canonical_url": "http://readthedocs.org/docs/pip/en/0.8/",
"project": {
"analytics_code": None,
"analytics_disabled": False,
Expand Down