# Access PR target branch when deploying preview build

**URL:** <https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402>\
**Category:** Support\
**Tags:** deploy-previews, environment-vars\
**Created:** [February 12, 2021, 3:01pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402 "2021-02-12T15:01:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kdembler](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/kdembler/32/22041_2.png) [@kdembler](https://answers.netlify.com/u/kdembler)\
**Post date:** [February 12, 2021, 3:01pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/1 "2021-02-12T15:01:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hillary](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hillary/32/21132_2.png) [@hillary](https://answers.netlify.com/u/hillary)\
**Post date:** [February 16, 2021, 7:41pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/2 "2021-02-16T19:41:52Z")

</div>

Hello @kdembler ! 👋

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.

---

<div class="post-metadata">

**Author:** ![kdembler](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/kdembler/32/22041_2.png) [@kdembler](https://answers.netlify.com/u/kdembler)\
**Post date:** [February 16, 2021, 7:59pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/3 "2021-02-16T19:59:57Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Scott](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/scott/32/26136_2.png) [@Scott](https://answers.netlify.com/u/Scott)\
**Post date:** [February 25, 2021, 5:07pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/4 "2021-02-25T17:07:25Z")

</div>

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](https://docs.netlify.com/configure-builds/file-based-configuration/#deploy-contexts) 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!

---

<div class="post-metadata">

**Author:** ![kdembler](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/kdembler/32/22041_2.png) [@kdembler](https://answers.netlify.com/u/kdembler)\
**Post date:** [February 26, 2021, 8:19am UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/5 "2021-02-26T08:19:17Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![Scott](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/scott/32/26136_2.png) [@Scott](https://answers.netlify.com/u/Scott)\
**Post date:** [February 26, 2021, 12:47pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/6 "2021-02-26T12:47:29Z")

</div>

Ah, understood.

Food for thought – a [build plugin](https://docs.netlify.com/configure-builds/build-plugins/create-plugins/#plug-into-events) which you can configure exactly how you want it, which runs before your build? You could take [this blog post](https://www.netlify.com/blog/2020/05/21/localize-your-environment-variables-by-context-with-build-plugins/) as inspiration too (where you’ll also find an existing plugin that does this!)

---

<div class="post-metadata">

**Author:** ![kdembler](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/kdembler/32/22041_2.png) [@kdembler](https://answers.netlify.com/u/kdembler)\
**Post date:** [February 26, 2021, 1:01pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/7 "2021-02-26T13:01:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Scott](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/scott/32/26136_2.png) [@Scott](https://answers.netlify.com/u/Scott)\
**Post date:** [February 26, 2021, 3:38pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/8 "2021-02-26T15:38:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![aminerol](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/aminerol/32/24144_2.png) [@aminerol](https://answers.netlify.com/u/aminerol)\
**Post date:** [April 8, 2021, 3:13pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/9 "2021-04-08T15:13:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kdembler](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/kdembler/32/22041_2.png) [@kdembler](https://answers.netlify.com/u/kdembler)\
**Post date:** [April 8, 2021, 3:27pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/10 "2021-04-08T15:27:04Z")

</div>

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: [https://github.com/Joystream/atlas/tree/master/netlify-plugins/contextual-env](https://github.com/Joystream/atlas/tree/master/netlify-plugins/contextual-env)

---

<div class="post-metadata">

**Author:** ![aminerol](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/aminerol/32/24144_2.png) [@aminerol](https://answers.netlify.com/u/aminerol)\
**Post date:** [April 8, 2021, 3:47pm UTC](https://answers.netlify.com/t/access-pr-target-branch-when-deploying-preview-build/32402/11 "2021-04-08T15:47:22Z")

</div>

Thank you, this plugin will definitely help 🙏
