2

I'm trying to set a environment variable when I push on a certain branch on my repo (the 'next' branch), the goal is that I want this variable to be defined only in this branch.

I've read the netlify documentation and I have no idea what I'm doing wrong:

https://docs.netlify.com/configure-builds/file-based-configuration/

So I've used the following code in my netlify.toml file:

[context.next.environment]
  SITE_BRANCH = "next"

I also tried [context.next]

My variable never appears in process.env

Does anyone have any idea what I'm doing wrong ? Thanks

EDIT

This is my nuxt.config.js file:

export default {
  publicRuntimeConfig: {
    SITE_BRANCH: 'process.env.SITE_BRANCH'
  }
}

This is my .env file:

SITE_BRANCH=next
kissu
  • 40,416
  • 14
  • 65
  • 133
JulienChamp
  • 113
  • 1
  • 9
  • Not sure myself about the thing here. Your env variable is named `SITE_BRANCH`? Then, it should be in `process.env.SITE_BRANCH` right? Be careful because `console.log`'ing just `process.env` will give you an empty object normally. On top of that, [this is how](https://stackoverflow.com/a/67705541/8816585) you usually use env variables in Nuxt. – kissu Apr 13 '22 at 08:43
  • I tried `console.log`ing `process.env.SITE_BRANCH` and it returned undefined. – JulienChamp Apr 13 '22 at 08:51
  • Where? Could you please edit your question with some code? Also, is `SITE_BRANCH` the name of your env variable here? – kissu Apr 13 '22 at 08:52
  • Could you be sure that you can achieve a proper working flow with a simple static `.env` file at first? Before going into the dynamic netlify one. – kissu Apr 13 '22 at 08:52
  • I also use `publicRuntimeConfig` and `privateRuntimeConfig` in my nuxt.config for other variables. But I didn't try it for this one since it was depending on the branch name, I thought it was netlify that had the information. Not sure if what I'm saying is clear. – JulienChamp Apr 13 '22 at 08:55
  • At the end, it will still be the same: passing down the variable to your app so you can expect it to work in the same way (if it's coming from a file or from Netlify, it should still be similar). I still do recommend trying with a static `.env` file to be sure that this is working. – kissu Apr 13 '22 at 09:12
  • Okay, so I've tried with a `.env` file and it's working, I get the variable by using `this.$config.SITE_BRANCH`. – JulienChamp Apr 13 '22 at 09:27

1 Answers1

1

Looks like the syntax is more like this (indentation is important)

[context."feat/my-cool-branch"]
  SITE_BRANCH = "next"

This part of the documentation goes more into details.

kissu
  • 40,416
  • 14
  • 65
  • 133
  • 1
    It seems to be working with this code: `[context."next".environment] SITE_BRANCH = "next"` and of course the publicRuntimeConfig – JulienChamp Apr 13 '22 at 10:05