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 2 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
9 changes: 9 additions & 0 deletions docs/user/reference/environment-variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ 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 URL for the version it's currently building.
humitos marked this conversation as resolved.
Show resolved Hide resolved
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, the default domain will be exposed (e.g. ``.readthedocs.io``)
humitos marked this conversation as resolved.
Show resolved Hide resolved

:Example: ``https://docs.example.com/en/latest/``
:Example: ``https://docs.readthedocs.io/ja/stable/``

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