How to get base branch name dynamically(during build) in deploy.preview

How to get base branch name dynamically in deploy.preview.

On pull_request/megre how to get the base branch name(means for which branch I am going to merge if pull successful)

process.env.BRANCH => pull/606/head (I do not want this)
process.env.HEAD => JIRA-ID_XXX (I do not want this)

Build environment variables | Netlify Docs (I do not found any)

Same question earlier but not answered till now

Hi @ankitpandeypagefreez

Check out the Deploy URLs and metadata which states

DEPLOY_PRIME_URL: URL representing the primary URL for an individual deploy, or a group of them, like branch deploys and Deploy Previews; for example, https://feature-branch--petsof.netlify.app or https://deploy-preview-1--petsof.netlify.app.

Sorry,
I want something else.
i.e.
My build in package.json is based on the branches like dev1, dev2, main, staging

npm run build:$BRANCH

This $BRANCH(or any otherway) could get the base branch to run by build.

I checked the metadata but Netlify did not providing.

As far as I am aware, $BRANCH is not available to the package.json.

You would possibly need to write your own script (e.g. a bash script) that could check the branch and run the appropriate build command.

Thanks for reply.

But how to get the base branch when doing pull request in [context.deploy-preview] in netlify.toml file? (I need this)

It would be great if Netlify provides one more env variable called BASE

You would most likely have to rely on Git commands to get a branch name. Not sure if it’s the right one in context of a PR, but maybe git status --branch shows the correct one?

@hrishikesh
It won’t work.
This is not the context when making PR from github.com

Meanwhile I got a solution:

https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request

curl
-H “Accept: application/vnd.github+json”
-H “Authorization: Bearer ”
https://api.github.com/repos/OWNER/REPO/pulls/PULL_NUMBER

In JSON response there is a “base” key
base.ref => Base Branch Name

Not sure If Netlify Team could provide a new environment variable called BASE_BRANCH in future.

@fool @fool-unprivileged
Do you have answer of my question?

It would be great if you help me.

How to get base branch name dynamically(during build) in deploy.preview(for a pull request) in netlify.toml?

Hi @ankitpandeypagefreez

It seems the approach we suggested earlier doesn’t work and we’re already brainstorming what could be done in the short term.

We’ll probably have to open a feature request but we’ll let you know soon!

hi @ankitpandeypagefreez !

I don’t know more than Gualter :slight_smile: But I think you should be able to use the git command line utility to get that information, though I do not know how exactly. But, once we start your build, we do have a full clone of the repo, so commands accessing the local copy of the clone - like git status - just work normally. Perhaps you can find a way to use those to show the info you need? We’re not super-experts on command line git, so you may have to do the research yourself to find the details

I came here with the same problem.

Using the HEAD env var is working for me (any variation of git status does not, as Netlify creates a new temporary branch with the build ID as the name).

1 Like

Thanks for coming back and sharing, @bep!