Home > Net >  How to retrieve Gitlab environments variables to be used in gatsby-*.js files
How to retrieve Gitlab environments variables to be used in gatsby-*.js files

Time:06-09

I have on gitlab environment variables secret api tokens, that I want to securely load in my project to be used in the gatsby-.js* files. I do not have or want to use .env files. According to various sources, I should be able to use my env variables just like this : process.env.TOKEN, but it does not work. I tried multiple Gatsby plugins, I tried having the prefix GATSBY_ to my env variables on gitlab, yet it still doesn't work. What I was able to do for now is to retrieve their values in my gitlab-ci.yml file like this : TOKEN: "${GATSBY_TOKEN}"

where I can echo them in my build stage, So I don't know if the value is accesible in my yml file locally or it only becomes accesible when the pipeline is launched on gitlab. My question is it possible to pass them down to my gatsby-*.js files ? Or is there another solution entirely to the problem ?

CodePudding user response:

According to various sources, I should be able to use my env variables just like this : process.env.TOKEN

That is not how environment variables are supposed to work.

If you don't want to use .env files your only chance is setting them in the running commands, gatsby develop as well as gatsby build:

  "scripts": {
    "start": "SOME_ENV_VAR=someValue gatsby develop",
   }

From now on, SOME_ENV_VAR will be exposed to be used in your code.

  • Related