简体   繁体   English

Create-react-app 托管为 Static Azure 上的应用程序未检测到环境变量

[英]Create-react-app hosted as Static App on Azure doesn't detect env vars

Like the title says, I have a static create-react-app that I'm deploying to Azure Static Apps with Azure DevOps. Like the title says, I have a static create-react-app that I'm deploying to Azure Static Apps with Azure DevOps. The problem is that absolutely none of the.env variables appear to be available on the frontend.问题是在前端似乎绝对没有任何 .env 变量可用。

To be specific, I have an API key named REACT_APP_MAP_API_KEY that I'm injecting using Pipelines on Azure Devops.具体来说,我有一个名为REACT_APP_MAP_API_KEY的 API 密钥,我正在使用 Azure Devops 上的管道注入该密钥。 I've confirmed that the key is loaded in from the variable group properly by logging it.我已通过记录确认密钥已从变量组正确加载。 This is my deployment pipeline YAML file:这是我的部署管道 YAML 文件:

trigger:
- main

pool:
  vmImage: ubuntu-latest

variables:
- group: api-key-group

steps:
  - task: CmdLine@2
    inputs:
      script: 'echo $(REACT_APP_MAP_API_KEY)'
  - checkout: self
    submodules: true
  - task: AzureStaticWebApp@0
    inputs:
      app_location: '/'
      api_location: 'api'
      output_location: ''
    env:
      azure_static_web_apps_api_token: $(deployment_token)
      REACT_APP_MAP_API_KEY: $(REACT_APP_MAP_API_KEY)

The key is echo'd when the pipeline runs.管道运行时会回显密钥。 However, when I try to console.log the variable on the frontend using console.log(process.env.REACT_APP_MAP_API_KEY) I simply get undefined .但是,当我尝试使用console.log(process.env.REACT_APP_MAP_API_KEY)在前端对变量进行 console.log 时,我只是得到undefined

To the best of my knowledge, this is exactly how you're supposed to load an.env variable into a static app frontend on Azure, but it just appears to be flat out not working.据我所知,这正是您应该将 an.env 变量加载到 Azure 上的 static 应用程序前端的方式,但它似乎完全无法正常工作。 Is this a bug on Azure's side?这是 Azure 方面的错误吗?

Credit goes out to @jonrsharpe for finding this thread which links to this workaround which I confirmed does indeed work.感谢@jonrsharpe 找到这个链接到我确认确实有效的解决方法的线程 For the record, it's pretty sad that Microsoft hasn't fixed this issue considering it affects one of their most popular cloud features (Azure Static Apps) for the most popular frontend framework (React).郑重声明,很遗憾微软没有解决这个问题,因为它影响了他们最流行的前端框架 (React) 的最流行的云功能之一(Azure Static 应用程序)。

The workaround does its thing apparently by splitting the deployment into two steps 1) doing the actual build manually ( Bash@3 ), where the environment variables seem to work, then 2) running the preconfigured deploy task ( AzureStaticWebApp@0 ) whose "build" step doesn't do anything at all and then takes the existing /build folder and deploys it.解决方法显然是通过将部署分为两个步骤来完成它的事情:1)手动进行实际构建( Bash@3 ),环境变量似乎可以工作,然后 2)运行预配置的部署任务( AzureStaticWebApp@0 ),其“构建" step 根本不做任何事情,然后获取现有的/build文件夹并部署它。 If this were working correctly, any env variables declared under AzureStaticWebApp@0 would be properly included in build process.如果这工作正常,在AzureStaticWebApp@0下声明的任何环境变量都将正确地包含在构建过程中。

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

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