如何检索gatsby-*。js文件中要使用的gitlab环境变量

发布于 2025-02-06 02:07:53 字数 454 浏览 3 评论 0原文

我在gitlab环境变量上秘密API令牌,我想在项目中安全加载以在 gatsby - .js*文件中使用。我没有或不想使用 .env 文件。根据各种来源,我应该能够像这样使用我的env变量:process.env.token,但它不起作用。我尝试了多个盖茨比插件,我尝试将前缀gatsby _访问Gitlab上的ENV变量,但它仍然不起作用。 我暂时可以做的是在我的 gitlab-ci.yml 文件中检索其值: token:“ $ {gatsby_token}”

我可以在我的构建阶段回荡它们,因此我不知道该值在我的yml文件中是否可在我的yml文件中获得可观,否则只有在管道为pipeline时才能使其变得可观在Gitlab上推出。我的问题是可以将它们传递给我的盖茨比 - *。js文件吗?还是完全解决了问题?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手 2025-02-13 02:07:53

根据各种来源,我应该能够使用我的env变量
就像这样:process.env.token

这不是环境变量应该使用的方式。

如果您不想使用.env文件,您唯一的机会就是在运行命令中设置它们,gatsby开发以及gatsby build

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

从现在开始,some_env_var将暴露在代码中使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文