PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 19, 2022

[FIXED] How to retrieve Gitlab environments variables to be used in gatsby-*.js files

 August 19, 2022     api, environment-variables, gatsby, gitlab, reactjs     No comments   

Issue

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 ?


Solution

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.



Answered By - Ferran Buireu
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing