Can't access Build environment variables

In the /settings/deploys#environment for my project I am settings env variables as follows:

It worked only for one deployment. All the subsequent deployments showed values for GATSBY_URL vars as $URL rather than the website url.

What am I missing here?

You cannot “stack” variables like that unless you do some very special parsing. We don’t interpolate for you, so as you see, you’ll get the string literal rather than the interpretation of an interpretation. Kinda like if you set $vara="$varb" - it’s a string :slight_smile:

You could try something like this:

  1. create a shell script for your build
  2. in the shell script, use your normal build command but first set up the environment.

I haven’t tested this but something like this may work:

#!/bin/sh
export GATSBY_URL=$URL GATSBY_DEPLOY_PRIME_URL=$DEPLOY_PRIME_URL GATSBY_DEPLOY_URL=$DEPLOY_URL
npm run build

Effectively, since in our UI those are strings, you need to programatically dereference or interpolate your variables. What I suggested may or may not work but something like that could :slight_smile:

All I need is to access the site url (be it a preview or my custom domain) during the build stage so I can provide an absolute path for the og:image instead of /img/og-image.jpg @fool

OK, then use $DEPLOY_PRIME_URL or $DEPLOY_URL or $URL directly (note that you need to do something to use them as described here:

…or do the trick I mentioned to set the GATSBY_* variables which you don’t have to “use” as described in that article :slight_smile:

Hi,

I’ve the same request/problem thant Sandor :smiley:

I tried your solution in a “one line command”, but it doesn’t work. Here is my code:

export REACT_APP_URL=$DEPLOY_PRIME_URL && yarn build

in my compiled build, REACT_APP_URL is empty. Do I miss something?

Not sure what is happening there, @Nicolas! What happens if you use this build command instead?

export REACT_APP_URL=$DEPLOY_PRIME_URL && env && yarn build

This will show what the environment is before yarn runs - you should see $REACT_APP_URL set in the output from env. IF that is the case, you’ll want to troubleshoot why yarn doesn’t “pass that on” to the build - I did a similar test and saw my test variable set before the build. I don’t have a sample react site to test, but whatever happens inside of yarn/react isn’t really Netlify specific, as long as we have the variable set before the build our system is working as intended and in the way that other systems should work too.

Hello and happy new year :slight_smile:

The problem is that we build/deploy locally, so during the “build”, the command doesn’t know the “future URL” of the deploy command … It makes sense :smiley:

I suppose that this “problem” is fixed if I just ask netlify build/deploy for me

Yup - bit of a chicken and egg problem around API deploys that need to know the ID:

  • you need to create a deploy
  • to get the ID,
  • and you need to know the checksums of all your source files to create a deploy,

…so there is no time when you know the ID and can still change the files. This is quite different in our build environment of course.

Perhaps you’d be able to take a hybrid approach:

  1. Build 99% of your app in your CI.
  2. Commit resultant build to git.
  3. we get notified by git and start a “build” (that isn’t a build)
  4. during that “build”, have us “massage” the files to add the deploy ID, with a build command like this:

sed -i s/PLACEHOLDER/${DEPLOY_PRIME_URL}/g file1.html && sed -i s/PLACEHOLDER/${DEPLOY_PRIME_URL}/g file2.js && ls -l index.html

What does that do?

  1. Search and replaces your placeholder with the “real” per-url deploy
  2. makes sure there is an index.html (so we don’t accidentally deploy an empty site if something goes wrong during the copy/massage)
  3. publishes effectively what you built, with that substring interpolated.

Not super direct, but may help you achieve your goals.

1 Like

Thank you :slight_smile:

1 Like