简体   繁体   English

Azure static web 应用程序环境变量

[英]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.

相关问题 用于容器的 Azure Web 应用程序未设置环境变量 - Azure Web App for Containers not setting Environment Variables Azure Static Web 应用 NodeJS 环境变量 - Azure Static Web App NodeJS env variables Azure ZC6E190B284633C48E39E55049DA3CCE8 中的主机 static HTML - Host static HTML in Azure Web App Service 识别是否部署在移动应用程序或 Web 应用程序上的环境变量? - Environment variable to recognise if deployed on Mobile App or Web App? 环境变量不接受天蓝色 - Environment variable not accepting for azure Static Web App Deploy with Azure 使用特定节点版本 - Static Web App Deploy with Azure using specific Node version 我无法从我的 REACT 应用程序的 azure 应用程序服务配置中读取环境变量 - I am having trouble of reading environment variable from azure app service configuration for my REACT app 我无法从我的 REACT 应用程序中读取 azure 应用程序服务配置的环境变量 - I am unable to read environment variable from azure app service configuration from my REACT app 从 Azure Static Web 应用程序调用应用程序服务 API 时获得未经授权 (401) 尽管已链接 - Getting unauthorized (401) when calling App Service API from Azure Static Web App despite being Linked Azure Web APP上的MongoDB - MongoDB on Azure Web APP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM