Set site.url from env var (netlify deploy preview)

I am new to jekyll, but I have built a site that is live, hosted on netlify.

Now I would like to use netlify deploy previews so I can test some changes without modifying the main public site. Such deploy previews are published at an url which I cannot predict. The url is available in an environment variable.

I can’t figure out how to get this url into my build. It needs to be in a _config.yml file, but I don’t know it in advance. I would like to override the site var in _config.yml on the jekyll build command line, but that doesn’t seem to be possible.

Advice, please?

how much do you need the actual site url for? I think I only use it for the canonical url in the metadata, every where else I just have my urls start with a / and the relative link.

Sorry, not an answer to your question, I know.

Looking at your site it looks like most of the urls are relative, so when the site is at 1234.mysite.com vs https://mysite.com what is actually not working?

I used the https://phlow.github.io/feeling-responsive/ template. Without the actual url, lots of styling doesn’t work.

in your head where you have:
<link rel="canonical" href="/about">

This is where I think the actual site url is important - so I would add it there.

what is a link to the preview site? I think it is something else.

I think I have it figured out. This build command seems to have worked:

printf "site: %s" "$DEPLOY_URL" > _config_netlify.yml; jekyll build --config _config.yml,_config_netlify.yml

See my deploy preview at https://5e40749f8f39b10008bd3868--angry-tereshkova-959566.netlify.com

I have not yet verified that this produces a correct deployment of master branch to main url.

ah - yeah that url I think would break the relative-ness of the urls. [not sure about that now]

it looks like you are able to have a different build command for the preview site? if so I think you are on the right track, writing the preview url to the dev config file.

actually I still don’t see what good the site url is doing, none of the urls for css or js have that in them, they are all relative.

just a moment; i will set the build command back to “jekyll build” and push another deploy preview.

No, you are correct. Here is a new deploy preview made with just “jekyll build”. When I did this the other day, the styling of nav was all broken.

in your config files I think that if you DO NOT specify the url or baseurl it will probably work fine. If you do specify it the preview site may or may not break - I think it would be trying to pull the css from the prod site which may or may not work.

If you have any more trouble post a link to your repo if you can.

thanks for your help @rdyar.

I’m totally thrilled with how easy and powerful this technology is. I moved that site from a very creaky old (expensively hosted) wordpress site in two days.

SSG FTW!
I used to use a CMS and static is so much easier to me.

Also, anyone reading this thread, not using the url or baseurl is not always good advice, probably best if you do use them unless there is a reason not to?