简体   繁体   English

反应应用程序的运行时环境变量

[英]Runtime environment variable for react app

We have three environments Dev , Staging and Prod .我们有三个环境DevStagingProd Create-react-app build is quite suitable for all the three environments but the only things change during the build is the environment variable for API endpoint. Create-react-app 构建非常适合所有三种环境,但构建期间唯一改变的是 API 端点的环境变量。

We are using the Jenkins pipeline for the build process and putting the output in S3 Bucket , the only problem with that process is that we have to make a separate build for every environment although everything is same except that API environment variable.我们在构建过程中使用 Jenkins 管道并将输出放在S3 Bucket ,该过程的唯一问题是我们必须为每个环境进行单独的构建,尽管除了 API 环境变量之外一切都相同。 It takes a significant amount of time.这需要大量时间。

So I possible solution is to move from compiled environment variable to runtime environment.所以我可能的解决方案是从编译环境变量移动到运行时环境。 I came across this .我遇到了这个 But I don't know how to do it in practical being newbie to Jenkins and AWS但作为JenkinsAWS新手,我不知道如何在实践中做到这一点

Our folder structure looks like this我们的文件夹结构是这样的

Project
|---mocks
|---static
|---index.html

index.html索引.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">

    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React APP</title>
  </head>
  <body>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

So, I am writing the way we have fixed this.所以,我正在写我们解决这个问题的方式。 In Jenkinfile file which is placed inside our project repo.在放置在我们项目存储库中的Jenkinfile文件中。 We have made following changes for every environment.我们对每个环境进行了以下更改。

Before every stage we simply re-writing the env.js with respective file.在每个阶段之前,我们简单地用相应的文件env.js

 stage('Push to Dev') {
       ...
       steps {
         .....
           sh 'cp ./config/dev.js ./build/env.js'
            .....
       }
    }

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

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