The default domains don’t support multi-level subdomains, e.g. foo.service-name.onrender.com. That would also require the code to know what to do with multi-level subdomains. Additionally, a wildcard custom domain can only point to one service.
I’ve not tried it personally (I’ll add it to my list of experiments to try), but maybe you could have an initialDeployHook script (docs) that utilises the Render REST API to add a custom domain with a domain structure you want, e.g. foo-preview-xxx.example.com. However, to have this completely automated, it would also need your DNS provider to have an API to be able to create the corresponding CNAME record.
get 'photos', to: 'photos#index', constraints: { subdomain: 'admin' }
So you are saying, you can’t define subdomains in the render blueprint for pull request previews, but you’d instead have to use Render’s REST api once the service is live (but not valid yet) to then fix the DNS stuff? is it this api? Add custom domain
What if in a Pull request preview edited the blueprint with the custom domain for that pull request preview itself (that it pre registered with some DNS provider api), would that be recommended? or do you have to go the Render REST api route post-deploy?
Domain declarations in the render.yaml will only be used to update the primary service, not Preview Environments.
Any custom domain setup would need to be done after, I tested the initialDeployHook pointing at a bash script like:
SRV=$(echo $RENDER_INSTANCE_ID | cut -d '-' -f 1,2)
if [["$IS_PULL_REQUEST" == "true"]]; then
JSON='{"name":"%s"}\n'
DOMAIN_JSON=$(printf "$JSON" "$RENDER_SERVICE_NAME.domain.com")
curl --request POST \
--url https://api.render.com/v1/services/$SRV/custom-domains \
--header "Accept: application/json" \
--header "Authorization: Bearer $API_KEY" \
--header "Content-Type: application/json" \
--data "$DOMAIN_JSON"
# hopefully this can be done with your DNS provider API
echo "Now create $RENDER_SERVICE_NAME.domain.com CNAME record pointing at $RENDER_SERVICE_NAME.onrender.com..."
fi
And it created a custom domain on the Preview Environment.
RENDER_INSTANCE_ID, IS_PULL_REQUEST, RENDER_SERVICE_NAME are all provided by Render. You’d just need to get your API_KEY in there securely.
Do you think it would work if, instead of doing this:
# hopefully this can be done with your DNS provider API
echo "Now create $RENDER_SERVICE_NAME.domain.com CNAME record pointing at $RENDER_SERVICE_NAME.onrender.com..."
fi
Instead of that, we create a wildcard A record so that it can utilize subdomains (not just CNAME ones) so a url like app.ourdomain.com would work?
If you’re still referring to Preview Environments, then using the anycast IP as an A-record on the wildcard would allow custom domains to resolve, but each Preview Environment service would still need a unique custom domain.
SSL is enforced, so adding these unique custom domains would need to wait for the certificate to be issued, which shouldn’t take too long, and should continue to work through the wildcard setup you mentioned.
It may be easiest to try out what you have in mind here and let us know if you come across any issues.