简体   繁体   English

用于 UAT 和 Live 的 React 中的运行时环境变量,用于单个构建 devops

[英]Runtime Environment Variables in React for UAT and Live for a single build devops

Sorry if you think this has been asked before but there doesn't seem like a good solution anywhere.抱歉,如果您认为以前有人问过这个问题,但似乎在任何地方都没有一个好的解决方案。

I have a build pipeline that packages up my react app into a single artifact.我有一个构建管道,可以将我的 react 应用程序打包成一个工件。 The release pipeline pushes that artifact to different Azure storage accounts for each environment (Dev, UAT, Live).发布管道将该工件推送到每个环境(Dev、UAT、Live)的不同 Azure 存储帐户。

Surely there is a way to use DevOps variables to configure variables in my package per environment.当然,有一种方法可以使用 DevOps 变量在我的 package 每个环境中配置变量。

Other solutions:其他解决方案:

  1. One build per environment - I don't want to do this because I would need to create a branch for each environment, a pipeline for each, the env configs for each, and a release pipeline for each.每个环境一个构建 - 我不想这样做,因为我需要为每个环境创建一个分支,为每个环境创建一个管道,为每个环境创建一个配置,并为每个环境创建一个发布管道。 This means a change to 1 environment takes 3 times as long.这意味着更改 1 个环境需要 3 倍的时间。 Also, the time to build these environments trebles.此外,构建这些环境的时间增加了三倍。

  2. Using a JSON file and swapping this out on deployment.使用 JSON 文件并在部署时将其换出。 - This didn't work because webpack imported the JSON file into the build so whilst I transformed the config.json files. - 这不起作用,因为 webpack 将 JSON 文件导入到构建中,因此我转换了config.json文件。 It was too late.已经太迟了。 This seems similar to using env.development and env.live and would mean 3 builds这似乎类似于使用env.developmentenv.live并且意味着 3 个构建

  3. Pull environment out of the request URL and call an endpoint - seems like my only option but definitely has flaws.从请求 URL 中提取环境并调用端点 - 似乎是我唯一的选择,但肯定有缺陷。

This isn't an issue in .NET (or Java I believe, .NET is my background) and was solved years ago with web.config and appSettings.这在 .NET (或 Java 我相信,.NET 是我的背景)中不是问题,并且几年前通过 Z2567A940EC9705EB17AC2 解决了

Please let me know if you have solved this and how?请让我知道您是否已解决此问题以及如何解决? Thanks for your help谢谢你的帮助

Runtime Environment Variables in React for UAT and Live for a single build devops用于 UAT 和 Live 的 React 中的运行时环境变量,用于单个构建 devops

We could use the task Replace Tokens to use DevOps variables to configure variables in the package per environment:我们可以使用任务Replace Tokens来使用 DevOps 变量在每个环境的 package 中配置变量:

在此处输入图像描述

The format of variable in .json file is #{TestVar}# . .json文件中的变量格式为#{TestVar}#

And define the key's values on the Variables based on the stages:并根据阶段在变量上定义键的值:

在此处输入图像描述

Hope this helps.希望这可以帮助。

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

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