sphinxext-opengraph icon indicating copy to clipboard operation
sphinxext-opengraph copied to clipboard

Auto-generate site URL if one is not given using CI/CD environment variables

Open choldgraf opened this issue 2 years ago • 1 comments

The ogp_site_url field is the only one strictly required by this extension, but I find that folks often forget to set it nonetheless. On many CI/CD systems, there are often environment variables that could be used as a fallback if site_url isn't present. This might be helpful for folks that forget or that simply don't want to have to worry about manually specifying the URL.

For example:

GitHub or ReadTheDocs examples

GitHub Actions have default environment variables for all actions. ReadTheDocs also has default environment variables.

Assuming that a person building Sphinx with GitHub Actions is using GitHub Pages (and same w/ ReadTheDocs) you could create a site URL using these variables. For example:

import os
env = os.environ

if "GITHUB_ACTION" in env:
	site_url = f"https://{env['GITHUB_REPOSITORY_OWNER']}.github.io/{env['GITHUB_REPOSITORY']}"
elif "READTHEDOCS" in env:
    site_url = f"https://{env['READTHEDOCS_PROJECT']}.readthedocs.io/{env['READTHEDOCS_LANGUAGE']}/{env['READTHEDOCS_VERSION_NAME']}"

choldgraf avatar Dec 31 '22 14:12 choldgraf

The site url is already autopopulated on ReadTheDocs so that case is taken care of. I'm weary of automatically setting the site url in Github Actions. If a user uses Github Actions to build their docs and deploys the built docs/artifacts elsewhere, the url will silently be wrong.

If there's a way on Github Actions to detect if the build is being deployed to Github Pages, then I'd be open to adding the auto site url there too.

TheTripleV avatar Jan 03 '23 02:01 TheTripleV