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: expose VCS-related environment variables #10168

Merged
merged 16 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
37 changes: 37 additions & 0 deletions docs/user/reference/environment-variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,43 @@ 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/``

.. envvar:: READTHEDOCS_REPOSITORY_URL

URL from where the source code to generate the documentation is clonned.
humitos marked this conversation as resolved.
Show resolved Hide resolved
It could be HTTPS, SSH or any other URL scheme supported by Git.
This is the same URL defined under :guilabel:`Admin` > :guilabel:`Settings` > :guilabel:`Repository URL`.
humitos marked this conversation as resolved.
Show resolved Hide resolved

:Example: ``https://github.com/readthedocs/readthedocs.org``
:Example: ``[email protected]:readthedocs/readthedocs.org.git``

.. envvar:: READTHEDOCS_REPOSITORY_IDENTIFIER
Copy link
Member

@ericholscher ericholscher Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a repository id? Or a version ID?

This is probably the name I'm the most unclear on whether it's correct.

Again though, perfect naming is hard, and the examples tell most of the story.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like READTHEDOCS_GIT_IDENTIFIER better. In fact I like everything better with git :)

READTHEDOCS_GIT_CLONE_URL
READTHEDOCS_GIT_COMMIT_HASH
READTHEDOCS_GIT_IDENTIFIER

it's easier to understand. I think the old VCS semi-supported backends can just have to deal with that. If we want to really support something, we might as well re-introduce "VCS" or have stuff like READTHEDOCS_HG_CLONE_URL... but that's not relevant now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, if we wanted it to be generic, we could use VCS, but I agree GIT is probably better, and forward looking 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'm going to change the names to use GIT 👍🏼


Git identifier checked out from the repository URL.
humitos marked this conversation as resolved.
Show resolved Hide resolved
It could be a branch or tag name.
humitos marked this conversation as resolved.
Show resolved Hide resolved
humitos marked this conversation as resolved.
Show resolved Hide resolved

:Example: ``v1.x``
:Example: ``bugfix/docs-typo``
:Example: ``feature/signup``
:Example: ``update-readme``

.. envvar:: READTHEDOCS_REPOSITORY_IDENTIFIER_HASH
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love how long this name is, but it seems reasonably explicit.

I think IDENTIFIER and HASH are kind of the same thing, but I'm not sure if git has a canonical name for this. I couldn't quickly find it in the docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added HASH here to be explicit about a commit in particular. We are using IDENTIFIER for the branch/tag name as well. I didn't find a better name for this 😞


Git commit hash identifier checked out from the repository URL.
humitos marked this conversation as resolved.
Show resolved Hide resolved

:Example: ``1f94e04b7f596c309b7efab4e7630ed78e85a1f1``

.. 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
9 changes: 9 additions & 0 deletions readthedocs/doc_builder/director.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ def get_rtd_env_vars(self):
"READTHEDOCS_OUTPUT": os.path.join(
self.data.project.checkout_path(self.data.version.slug), "_readthedocs/"
),
"READTHEDOCS_REPOSITORY_URL": self.data.project.repo,
"READTHEDOCS_REPOSITORY_IDENTIFIER": self.data.version.identifier,
"READTHEDOCS_REPOSITORY_IDENTIFIER_HASH": self.data.build["commit"],
}
return env

Expand Down Expand Up @@ -615,6 +618,12 @@ def get_build_env_vars(self):
}
)

env.update(
{
"READTHEDOCS_CANONICAL_URL": self.data.version.canonical_url,
}
)

# 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
8 changes: 8 additions & 0 deletions readthedocs/projects/tests/test_build_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ def test_get_env_vars(self, load_yaml_config, build_environment, config, externa
"READTHEDOCS_OUTPUT": os.path.join(
self.project.checkout_path(self.version.slug), "_readthedocs/"
),
"READTHEDOCS_REPOSITORY_URL": self.project.repo,
"READTHEDOCS_REPOSITORY_IDENTIFIER": self.version.identifier,
"READTHEDOCS_REPOSITORY_IDENTIFIER_HASH": self.build.commit,
}

self._trigger_update_docs_task()
Expand All @@ -301,6 +304,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