简体   繁体   English

在 GitHub 页面上部署的 Gatsby 站点中使用环境变量

[英]Using environment variables in Gatsby site deployed on GitHub pages

Description描述

I am trying to use environment variables in my Gatsby site deployed on GitHub pages, but it is only working locally and not on the live site.我正在尝试在部署在 GitHub 页面上的 Gatsby 站点中使用环境变量,但它只能在本地运行,而不能在实时站点上运行。 Apparently this is because Gatsby requires a .env.production file to access the variables from.显然这是因为 Gatsby 需要一个.env.production文件来访问变量。 How do I create this using GitHub workflows and maintain the secrecy of my variable so it is not exposed publicly?我如何使用 GitHub 工作流程创建它并维护我的变量的机密性,使其不会公开?

I have read through this related question but can't figure out how to translate it into code relevant to my situation.我已经通读了这个相关的问题,但无法弄清楚如何将其转换为与我的情况相关的代码。 How to use environment variables in Github Page? 如何在 Github Page 中使用环境变量?

Steps to reproduce重现步骤

I added a secret variable in my github repository settings, created a workflow file so that it could be accessed as an environment variable, added the dotenv requirement with a path configuration in my gatsby-config.js file, and then deployed my site using npm run deploy.我在我的 github 存储库设置中添加了一个秘密变量,创建了一个工作流文件,以便它可以作为环境变量访问,在我的 gatsby-config.js 文件中添加了带有路径配置的 dotenv 要求,然后使用 npm 部署了我的站点运行部署。

The environment variable is accessible locally from a .env.development file, but is not accessible on the deployed site.环境变量可以从.env.development文件本地访问,但不能在部署的站点上访问。 Below is the github repo I have been trying to figure this out on.下面是我一直试图解决这个问题的 github repo。 The link is to my latest commit on the dev branch so it is easier to find where I have been trying to get the workflow with environment variables up and running.该链接指向我在 dev 分支上的最新提交,因此可以更轻松地找到我一直在尝试使用环境变量启动和运行工作流的位置。

https://github.com/spk2dc/spk2dc.github.io/tree/62d6078dffba4feb1ec3fd85c952e10eb0787017 https://github.com/spk2dc/spk2dc.github.io/tree/62d6078dffba4feb1ec3fd85c952e10eb0787017

Expected result预期结果

Environment variables accessible to live Gatsby website deployed on GitHub pages and secrecy of variable maintained.部署在 GitHub 页面上的 Gatsby 实时网站可访问环境变量,并维护变量的保密性。

Environment环境

System:  
OS: Linux 5.4 Ubuntu 18.04.5 LTS (Bionic Beaver)  
CPU: (2) x64 Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz  
Shell: 4.4.20 - /bin/bash  
Binaries:  
Node: 14.6.0 - /usr/local/bin/node  
Yarn: 1.22.5 - /usr/bin/yarn  
npm: 6.14.8 - /usr/local/bin/npm  
Languages:  
Python: 2.7.17 - /usr/bin/python  
Browsers:  
Chrome: 85.0.4183.121  
npmPackages:  
gatsby: ^2.4.2 => 2.24.50  
gatsby-image: ^2.0.41 => 2.4.16  
gatsby-plugin-feed: ^2.2.0 => 2.5.11  
gatsby-plugin-google-analytics: ^2.0.19 => 2.3.13  
gatsby-plugin-manifest: ^2.4.21 => 2.4.24  
gatsby-plugin-netlify: ^2.0.17 => 2.3.13  
gatsby-plugin-offline: ^2.1.0 => 2.2.10  
gatsby-plugin-postcss: ^2.0.7 => 2.3.11  
gatsby-plugin-purgecss: ^3.1.1 => 3.1.1  
gatsby-plugin-react-helmet: ^3.0.12 => 3.3.10  
gatsby-plugin-sharp: ^2.6.24 => 2.6.28  
gatsby-remark-copy-linked-files: ^2.0.12 => 2.3.12  
gatsby-remark-images: ^2.0.6 => 2.0.6  
gatsby-remark-prismjs: ^3.2.9 => 3.5.11  
gatsby-remark-responsive-iframe: ^2.1.1 => 2.4.12  
gatsby-remark-smartypants: ^2.0.9 => 2.3.10  
gatsby-source-filesystem: ^2.0.33 => 2.3.25  
gatsby-transformer-remark: ^2.3.12 => 2.8.29  
gatsby-transformer-sharp: ^2.5.12 => 2.5.13  
npmGlobalPackages:  
gatsby-cli: 2.12.87  

You don't need to push your .env file in a production environment, you just need to add the secret key to your repository in the Secrets menu option:您不需要在生产环境中推送您的.env文件,您只需要在 Secrets 菜单选项中将密钥添加到您的存储库中:

在此处输入图片说明

Then, you just need to link your Gatsby application to those environment variables by calling them in your code like process.env.REACT_APP_APIKey .然后,您只需要通过在代码中调用它们来将 Gatsby 应用程序链接到这些环境变量,例如process.env.REACT_APP_APIKey

Of course, I would suggest adding a .env.development and test your variables locally under gatsby build process.当然,我建议添加一个.env.development并在gatsby build过程下本地测试您的变量。

In addition, keep in mind that you will need to prefix with GATSBY_ all those variables that will need to be accessible by the browser as the Environment variables documentation explains.此外,请记住,如环境变量文档所解释的那样,您需要使用GATSBY_所有那些需要由浏览器访问的变量的前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM