Knowing the deploy URL of previews on build

I am trying to build previews using GoHugo. In its configuration, the tool has a parameter baseURL that defines the path to the installation (including the domain root).

The thing is… it needs to include the domain name so that links for instance in the RSS template have the full URL to the site.

Now, trying to deploy a test version (for instance via netlify --deploy --build (or via the build hooks via GitHub) works fine, but the resulting website has broken asset links. The best sample of this is the missing stylesheet. Because the URL Hugo uses is the website’s URL and the content security policy does not allow cross-origin requests for the assets.

(Sorry for the extensive introduction, we now come to the point of my question:)

I found the env-variable DEPLOY_URL that is said to contain the deployed URL of the current build. It doesn’t contain the proper URL though…

echo ${DEPLOY_URL}
hugo \
  --verbose \
  --baseURL ${DEPLOY_URL}

Results in the output of for the DEPLOY_URL.

I understand that it doesn’t make much sense to initiate a custom subdomain before the system is 100% sure it has a deployable website, but I am also in need of the final URL when the SSG is building the site.

Is there any way to retrieve the final deployment URL on build (or am I maybe even doing something very wrong and it would work done right)?

I have three possible workarounds, but all three are “hacks”:

  1. naming the baseURL parameter "", which will lead to all absolute links being relative to the domain root. The problem here is, that many templates and features require the full URL (think of images in RSS feed contents)
  2. use the empty baseURL from 1 and add a second parameter that has the full URL, then to rework all templates that require the full URL to use that new parameter. This will put some extra strain on internal templates (they all need to be re-defined) and basically introduces a second point of possible failure.
  3. a post-build plugin that replaces all occurrences of with the THEN hopefully available DEPLOY_URL

Maybe someone out there already has a solution for my dilemma?

hi there,

i’m not the deepest expert for deploy previews, but if i understand this correctly:

you should be able to know the URL if you also know the information about the pull request. quoting:

This URL has the prefix deploy-preview followed by the identifier number of the pull request or merge request. For example, a Deploy Preview for pull/merge request #42 will deploy to

is this helpful in your case?

Thank you for your response, but sadly not. I am using the Netlify CLI to deploy “every now and then” and get away from automatic deploys via Github PRs, tags and pushes. If I would do these deploys via PRs that should be the proper way to create the URL.

The more I think about it the more my third “hack” might be a usable solution. Replacing strings after build. I wonder if I am doing the call to DEPLOY_URL wrong, but then I would expect that variable to be empty, and here it doe the thingy. The docs are also not too verbose about WHEN exactly that variable is available with the final URL. It reads as if it’s available any time.

yeah, that makes sense. I am going to try and see if we can get someone to clarify when the variable is available, my hunch is probably after the build is complete (so during the deploy process). I will see if i can get that confirmed.

I’m fairly certain that the DEPLOY_URL variable is available for the full duration of the build when it’s run on Netlify. But since you’re running your build locally and deploying by using the CLI command netlify deploy --build, I think that’s why there isn’t a deploy hash yet to put into a subdomain.

However, since you’re working in the CLI, you could also set the subdomain yourself, using the --alias flag. For example, you could change your build command like so:

hugo \
  --verbose \
  --baseURL ''

and then use this command to build/deploy from the CLI:

netlify deploy --build --alias 'my-preview'

If you make multiple deploys with the same alias, Netlify will serve the most recent deploy with that alias at the alias URL. (This matches the behavior of Netlify-built Deploy Previews and branch deploys.) If you wanted to use a unique URL for each deploy, you could change the alias name in both locations each time you deploy. Or you could get fancy and do it programmatically with a number generator or timestamp. Personally, I actually like having a consistent URL for the most recent non-production deploy.

Hope that helps!


Hmmm, and it also occurs to me that if you do use --alias, the local build might actually properly generate the DEPLOY_URL variable to match. (But I haven’t tested.)

Thank you @verythorough and @perry. I will try it out later today and get back to you about it!

This works perfectly :slight_smile: I am using my git commit hash now and all CSS is happy :slight_smile:

1 Like

Thanks so much for coming back and letting us know. Happy building! :rocket: