简体   繁体   English

如何为在应用服务上使用 azure devops 管道部署的 React 应用设置 env

[英]How to set .env for react app deployed with azure devops pipeline on app service

I developed a pipeline with CI/CD on azure Devops for deploying a React app on Azure web app service.我在 azure Devops 上开发了一个带有 CI/CD 的管道,用于在 Azure web 应用程序服务上部署 React 应用程序。 Locally I'm using a.env file and this file is on.gitignore.我在本地使用的是一个 .env 文件,这个文件是 on.gitignore。 I want to know how can i set the.env for reading it on production.我想知道如何设置 the.env 以便在生产环境中阅读它。

You can check the documentation below:您可以查看以下文档:

https://create-react-app.dev/docs/adding-custom-environment-variables/#adding-development-environment-variables-in-env https://create-react-app.dev/docs/adding-custom-environment-variables/#adding-development-environment-variables-in-env

.env files should be checked into source control (with the exclusion of .env*.local ). .env文件应该被检入源代码管理(不包括.env*.local )。

If you don't want to check in the .env files to DevOps, you could add the variables in the pipeline variables:如果您不想将.env文件签入 DevOps,您可以在管道变量中添加变量:

在此处输入图片说明

In addition, you can refer to the following case for more suggestions:另外,更多建议可以参考以下案例:

How to use environment variables in React app hosted in Azure 如何在 Azure 中托管的 React 应用程序中使用环境变量

Many of the proposed solutions related to this issue may not work but I solved it the following way.许多与此问题相关的建议解决方案可能不起作用,但我通过以下方式解决了它。 However, first let me explain why other solutions may not (should not) work (please correct me if I am wrong)但是,首先让我解释一下为什么其他解决方案可能不会(不应该)起作用(如果我错了请纠正我)

  • Adding pipeline variables (even though they are environment variables) should not work since a react app is run on the client side and there is no server side code that can inject environment variables to the react app.添加管道变量(即使它们是环境变量)应该不起作用,因为反应应用程序在客户端运行并且没有服务器端代码可以将环境变量注入反应应用程序。
  • Installing environment variable task on the classic pipeline should not work for the same reason.出于同样的原因,在经典管道上安装环境变量任务不应该起作用。
  • Adding to Application Settings in azure app service should not work for the same reason.出于同样的原因,添加到 azure 应用程序服务中的应用程序设置应该不起作用。
  • Having.env or.env.development or.env.production file in a git repo should not be a good practice as it may compromise api keys and other sensitive information.在 git 存储库中拥有 .env 或 .env.development 或 .env.production 文件不应该是一个好的做法,因为它可能会危及 api 密钥和其他敏感信息。

So here is my solution -所以这是我的解决方案 -

Step1: Add all those.env files to azure devops library as secure files.第一步:将所有这些.env 文件作为安全文件添加到 azure devops 库中。 You can download these secure files in the build machine using a DownloadSecureFile@1 pipeline task (yml).您可以使用DownloadSecureFile@1管道任务 (yml) 在构建机器中下载这些安全文件。 This way we are making sure the correct.env file is provided in the build machine before the task yarn build --mode development in the pipeline.通过这种方式,我们确保在管道中的任务yarn build --mode development之前在构建机器中提供了正确的 env 文件。

在此处输入图像描述

Step2: Add the following task in your azure yml pipeline in appropriate place.第 2 步:在您的 azure yml 管道中适当的位置添加以下任务。 I have created a github repohttps://github.com/mail4hafij/react-yarn-azure-pipeline if you want to see a complete example.如果您想查看完整示例,我已经创建了一个 github 存储库https://github.com/mail4hafij/react-yarn-azure-pipeline

  # Download secure file from azure library
  - task: DownloadSecureFile@1
    inputs:
      secureFile: '.env.development'

  # Copy the .env file
  - task: CopyFiles@2
    inputs:
      sourceFolder: '$(Agent.TempDirectory)'
      contents: '**/*.env.development'
      targetFolder: '$(YOUR_DEFINED_PROJECT_ROOT_FOLDER_VARIABLE)'
      cleanTargetFolder: false

Keep note, secure files can't be edited but you can always re-upload.请注意,无法编辑安全文件,但您可以随时重新上传。

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

相关问题 Azure DevOps - React App - 在发布管道中设置环境变量 - Azure DevOps - React App - Set Environment Variables in Release Pipeline 如何从 azure 发布管道查询当前部署到应用程序服务的 docker 图像? - How can I query which docker image is currently deployed to an app service from an azure release pipeline? 如何从 Azure 管道中的 Azure 应用服务获取价值 - How to get value from Azure App Service from Azure pipeline Azure Devops 管道返回:'无法将 web package 部署到应用服务。 内部服务器错误(代码:500)' - Azure Devops Pipeline returning: 'Failed to deploy web package to App Service. Internal Server Error (CODE: 500)' 部署在 azure web 应用程序上时找不到 React 应用程序上的字体 - Font on React app not found when deployed on azure web app 你如何指示 Azure DevOps 将你的 output 放在目标应用服务上的特定位置? - How do you direct Azure DevOps to place your output in a specific location on the target App Service? Azure Devops如何部署static angular app到azure blob存储 - Azure Devops how to deploy static angular app to azure blob storage 在 Azure 应用服务中设置暂存环境 - Set up staging environments in Azure App Service 设置 Azure devops 使用 REST API 发布管道变量 - Set Azure devops Release pipeline variable using REST API 如何从github部署到Azure应用服务 - How to Deploy from github to Azure App Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM