无法将不同的 .env 文件分配给不同的环境

问题描述 投票:0回答:0

我继承了一个安装了 dotenv 的 React 项目。共有三个文件 .env、.env.staging 和 .env.production。看起来 .env.staging 文件似乎没有正常工作。不清楚

.env 文件:

NODE_ENV=development
HTTPS=false
REACT_APP_DOMAIN=**mylocalapi**

.env.staging:

NODE_ENV=staging
HTTPS=true
REACT_APP_DOMAIN=https://**stagingwebsite**

.env.production:

NODE_ENV=production
HTTPS=true
REACT_APP_DOMAIN=https://**productionwebsite**

package.json 文件中的内容如下:

 "scripts": {
    "start": "react-scripts  start",
    "build": "react-scripts build",
    "build:staging": "env-cmd -f .env.staging npm run build",
    "build:production": "env-cmd -f .env.production npm run build",
    "test": "react-scripts test",
    "analyze": "source-map-explorer 'build/static/js/*.js'",
    "eject": "react-scripts eject"
  },

几个小时以来,我一直在兜圈子,试图找出如何告诉每个环境使用哪个 .env 文件。很多答案都说它是 NODE_ENV 变量......但这是在文件内部?!这是一个陷阱 22.

我已经尝试在运行它的服务器上物理设置一个名为“NODE_ENV”的环境变量,但这似乎不起作用。奇怪的是,登台网站正在获取 .env.production 文件(不是其他两个),所以 something 触发了这个。我知道它使用 .env.production 文件,因为它指向该文件中的后端应用程序 URL。

我将登台网站和生产网站都作为 Azure 静态 Web 应用程序运行。

我在网上看到一些关于创建不同构建文件夹的建议,使用 .env 文件中名为 OUTPUT_DIR 的变量。然后我尝试运行“npm run build:staging”,但构建仍然进入标准的“build”文件夹。

是否应该在我们的 .yml 文件中进行一些调整?这是暂存站点的 .yml 文件:

name: Azure Static Web Apps CI/CD

env:
  CI: ""

on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened, closed]
    branches:
      - main

jobs:
  build_and_deploy_job:
    if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
    runs-on: ubuntu-latest
    name: Build and Deploy Job
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - name: Set NODE_ENV
        run: echo "NODE_ENV=staging" >> $GITHUB_ENV
      - name: Build And Deploy
        id: builddeploy
        uses: Azure/static-web-apps-deploy@v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.xxx}}
          repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
          action: "upload"
          ###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
          # For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
          app_location: "/" # App source code path
          api_location: "api" # Api source code path - optional
          output_location: "build" # Built app content directory - optional
          ###### End of Repository/Build Configurations ######

  close_pull_request_job:
    if: github.event_name == 'pull_request' && github.event.action == 'closed'
    runs-on: ubuntu-latest
    name: Close Pull Request Job
    steps:
      - name: Close Pull Request
        id: closepullrequest
        uses: Azure/static-web-apps-deploy@v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.xxx}}
          action: "close"

我可以传递一些东西来强制它进行构建:暂存吗?

我觉得我在这里遗漏了一些非常基本的东西,但我看不到疏忽。有人请帮忙吗?

reactjs environment dotenv
© www.soinside.com 2019 - 2024. All rights reserved.