Access PR target branch when deploying preview build

Hey!

I’ve been looking extensively but haven’t found anything that’d satisfy my requirements. My app uses env variables defined in Netlify to use correct backend service instance. We also run two versions - production (from master branch) and staging (from development branch). Variables for both of those version are defined in Netlify with PRODUCTION_ and STAGING_ prefixes respectively. When doing production or branch deploys, picking the correct env variable isn’t an issue - I can use the CONTEXT variable supplied by Netlify to know which variables to use.

The problem I’m facing is this - how can I use the correct variables in the same fashion but for deploy previews for Pull Requests? When a PR is targeting the master branch, I’d like to use the PRODUCTION_ variables, otherwise the STAGING_ ones. Is there some way to know what branch the PR is targeting? Neither HEAD or BRANCH seem to be what I’m looking for.

Would greatly appreciate any help in that regard!

Cheers,
Klaudiusz

Hello @kdembler ! :wave:

Welcome to the Netlify Community! This is a great question, and we appreciate you posting it.

Before diving in, I was wondering if you could explain what your use case is for deploy previews for Pull Requests? If we understand a bit more about what you are aiming to accomplish, we will be able to provide better information.

Sure!

My team is building a frontend app and as stated above, we want to be able to run two versions of that app: production and staging, deployed from respective branches and with their own versions of backend services. When a preview for a PR is deployed I want to be able to see how the app with the latest changes works. However, if the PR is targeting the master branch, the preview cannot be deployed with STAGING_ variables - these would point to a latest backend which may not be yet supported by the production version and vice versa for PRs targeting the development branch. I’d want those previews to use the correct set of env variables to make the previews work as the production/staging will work after merging.

Hope that helps, let me know if you have any more questions :slight_smile:

Hey, thanks for your patience! Just wanted to ask and see if you’d found something that worked for you?

Although we do have a deploy-preview deploy context available, the env vars are applied across all deploy previews.

Instead, you’ll want to define branch-specific deploy contexts where “all deploys from this specific branch will inherit these settings”, e.g.

[context.production]
  (production env vars go here)
[context.staging]
  (staging env vars go here)

Hope this helps!

1 Like

Hey Pie, thanks for your reply!

Unfortunately, I haven’t been able to find anything that solved my usecase.

Also, like I mentioned in the original message, I’m aware of branch-specific deploy contexts and already use them but unfortunately they don’t help much in this specific case. I’m talking specifically about PR deploy previews. Those will always have a deploy-preview deploy context, so they don’t give me the information I’m interested in - what context is the PR targeting. When deploying a preview from a PR, I’d like to know whether that PR, when merged, will go into master (then I need to use production vars) or into development (then I need to use staging vars). Seems I need some more info than deploy context for that but also seems there isn’t really a way to do this :frowning:

Ah, understood.

Food for thought – a build plugin which you can configure exactly how you want it, which runs before your build? You could take this blog post as inspiration too (where you’ll also find an existing plugin that does this!)

Right, that thought crossed my mind as well. I guess the issue is that this info about PR target branch isn’t exposed in any way, so the build plugin would probably need to call GitHub API to figure out what’s a target branch and then act based on that

Might be worth creating a feature request post here in Netlify – it’ll help us to assess how sought-after this is. I will double-check with the team in case there’s something I’m missing.

If it’s any consolation, for now, Octokit is quite well documented!

Hey @kdembler, would like to know if you find any solution for this use case, would really appreciate any kind of help

Hey @aminerol! Unfortunately, there isn’t any built-in solution for this so I went with the suggestion in this thread and created a plugin that calls the Github API. If you’re interested, you can find the code here: atlas/netlify-plugins/contextual-env at master · Joystream/atlas · GitHub

1 Like

Thank you, this plugin will definitely help :pray: