How to set up environment variable in CI/CD?

Hello, I have an architecture where my backend is hosted APIs are hosted in heroku and I am using Netlify for CDN for React application.

I am trying to set up merge request review deployment, so we can review the apps online. The react app would have to call out to the backend API URL that changes for different review branches. I need to set an environment variable REACT_APP_API_URL to something like during my CI/CD setup but since Netlify review branches get deployed automatically, it seems impossible to set this variable. I tried to do some deployment manually during CI/CD using the netlify CLI, but it looks like there is no support for environment variable injection (at least yet??).

How are people handling scenarios like this when you need to set dynamic environment variables?

Hmm, I’m not 100% sure this is what you need, but does this seem relevant to your question?

I have the same use case. We’re looking at Heroku Review Apps, which spin up new apps per branch or PR. Heroku provides a way to set the API URL in a standard way, based on branch or PR name. It would be great if there were a way to dynamically set our Netlify environment variable to this value. I wonder if there is a way to specify a pattern like http://mysite-<branch_name> as an environment variable? I can’t imagine we’re the only ones trying to do this.

I looked at the linked post, it doesn’t have anything helpful in it related to this unfortunately, and the use case seems a bit different.

Hi, I’m not sure what you need but the format for a branch deploy is branchname– You can access this using the deploy_prime_url env var as mentioned here: Build environment variables | Netlify Docs. Let me know if that works.

Thanks Dennis. What I need to be able to do is pass in/override an environment variable at deploy time. Our Netlify application is part of a larger app. The backend comes up first, and provides me with a URL that I need to pass into the Netlify app somehow. The steps are being orchestrated outside of Netlify, and look like this:

  1. PR to our backend repo is opened and results in a deployment of our backend service
  2. An API URL is obtained from previous deployment
  3. Netlify app is deployed and needs the API URL from #2

The idea is we want to dynamically spin up new/separate environments for different PR’s/branches.
My thought was to pass the environment variable through the CLI as stated by the OP, but it doesn’t look like that is possible. Maybe there’s a better way to approach this?

Yea, I don’t think passing an env var through the cli would work. Perhaps you can create an endpoint that your build can ping to get the value that you need which you can then use during the rest of your build process. Does that sound like it would work for you?