[英]Azure static web app environment variable
I am trying to publish Gatsbyjs by Azure Static web app.我正在尝试通过 Azure Static web 应用程序发布 Gatsbyjs。 I have a plugin (gatsby-source-contentful).我有一个插件(gatsby-source-contentful)。
I need to pass variables like:我需要传递变量,例如:
{
resolve: `gatsby-source-contentful`,
options: {
spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
},
},
Error:错误:
Running 'npm run build'...
> gatsby-starter-default@0.1.0 build /github/workspace
> gatsby build
success open and validate gatsby-configs - 0.021s
error Invalid plugin options for "gatsby-source-contentful":
- "accessToken" is required
- "spaceId" is required
not finished load plugins - 0.905s
Where can I pass this?我在哪里可以通过这个?
Thanks.谢谢。
They are called environment variables .它们被称为环境变量。 They are intended to store sensitive data such as tokens, identifiers, etc, and they shouldn't be pushed in your repository, so you should ignore them (in your .gitignore
file).它们旨在存储敏感数据,例如令牌、标识符等,不应将它们推送到您的存储库中,因此您应该忽略它们(在您的.gitignore
文件中)。
By default, Gatsby creates 2 environments without noticing you, one for each compilation method:默认情况下,Gatsby 会在不通知您的情况下创建 2 个环境,每种编译方法一个:
gatsby develop
: uses .env.development
gatsby develop
:使用.env.development
gatsby build
: uses .env.production
gatsby build
:使用.env.production
Note: you can change this behavior if needed to add your own environments using NODE_ENV
custom commands.注意:如果需要使用NODE_ENV
自定义命令添加您自己的环境,您可以更改此行为。
So, to pass your data to your gatsby-config.js
you just need to create two files ( .env.development
and .env.production
) at the root of your project.因此,要将数据传递给gatsby-config.js
,您只需在项目的根目录下创建两个文件( .env.development
和.env.production
)。 Then, add the following snippet at the top of your gatsby-config.js
:然后,在gatsby-config.js
的顶部添加以下代码段:
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})
Note: dotenv
is already a dependency of Gatsby so you don't need to install it again注意: dotenv
已经是 Gatsby 的依赖,所以不需要重新安装
This will tell Gatsby where to take the environment variables.这将告诉 Gatsby 在哪里获取环境变量。
You just remain to populate both environment files.您只需要填充两个环境文件即可。 Look for the credentials in Contentful and add them in the files using the sane naming than you've set in your gatsby-config.js
:在 Contentful 中查找凭据,并使用比您在gatsby-config.js
中设置的合理命名将它们添加到文件中:
CONTENTFUL_SPACE_ID=123456789
CONTENTFUL_ACCESS_TOKEN=123456789
Keep also in mind that when dealing with Azure, Netlify, AWS, or similar CI/CD tools, you'll need to provide to the server the same environment files to avoid a code-breaking when pushing the changes.还要记住,在处理 Azure、Netlify、AWS 或类似的 CI/CD 工具时,您需要向服务器提供相同的环境文件,以避免在推送更改时出现代码破坏。
For Azure Static Web Apps there is two ways to set environment variables one for front-end and one for back-end scenarios.对于 Azure Static Web 应用程序有两种设置环境变量的方法,一种用于前端场景,一种用于后端场景。
Since you are using Gatsby, I guess its safe to assume you are building your front-end.由于您使用的是 Gatsby,我想假设您正在构建前端是安全的。 For that you will need to add the environment variables on your build configuration (azure-static-web-apps-.yml).为此,您需要在构建配置 (azure-static-web-apps-.yml) 中添加环境变量。
Like so:像这样:
env: # Add environment variables here
CONTENTFUL_SPACE_ID: <your-id>
Here is the link for that in documenation .这是文档中的链接。
Not to be confused with this one which is used for defining backend environment variables.不要与用于定义后端环境变量的这个混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.