How to deploy React app from Github to AWS S3 bucket with environment variables set Note: I didn't push .env files in github

huangapple go评论147阅读模式
英文:

How to deploy React app from Github to AWS S3 bucket with environment variables set Note: I didn't push .env files in github

问题

请仔细阅读并提供一些可能的方法。

背景:

  • 我使用'create-react-app'创建了一个React应用程序
  • 我创建了3个不同环境的.env文件,分别是.env.development、.env.staging和.env.production。
  • 出于安全原因,我们不应该将这些.env文件推送到公共的GitHub仓库
  • 我已经采取了必要的步骤,使用.gitignore文件

现在,我想直接从GitHub部署我的代码到AWS S3存储桶,并设置这些环境变量。
如何操作?

注意:我没有将我的.env文件推送到GitHub。因此,我的GitHub仓库不包含这些.env文件。

英文:

Hi React experts(especially with CI/CD knowledge),

Please read it carefully and provide some possible way.

> Background:
>
> - I have created a react app using 'create-react-app'
> - command  I have created 3 .env files for different environments like .env.development,.env.staging and .env.production.
> - For security reasons, we should not push those .env files to public Github repo
> - I have taken the necessary steps with the .gitignore file

Now I want to deploy my code from Github directly to AWS S3 bucket with those env variables set.
How does one go about doing this?

Note: I have not pushed my .env files in Github.bSo, my Github repo doesn't contain those .env files.

答案1

得分: 3

根据这里。我们无法在S3中设置环境变量。S3只是一个静态对象存储,不是动态内容服务器。

根据React文档,在执行npm run build后,运行时的所有环境变量都设置为最终构建(在构建文件夹内)。在构建React应用程序时,.env文件中的所有秘密变量都在运行时环境中,并在完成npm run build后将这些环境变量(秘密变量)添加到构建文件夹(最终应用程序)中。

因为S3不是运行时环境,所以我们需要构建一个React应用程序,并在创建存储桶后将build文件夹上传到S3存储桶,或者我们可以使用GitHub操作提供这些环境变量。

英文:

According to here. We can't set environment variables in S3. S3 is just a static object store, not a dynamic content server.

According to react documentation all the environment variables in the runtime are set to final build (inside build folder) after executing npm run build. when building React app all the secret variables in the .env file are in the runtime environment and after finishing npm run build those environment variables(secret variables) are added to the build folder (final app).

Because S3 is not a runtime environment we need to build a react app and upload the build folder to the S3 bucket after creating a bucket or we can provide that environment variables using GitHub action.

huangapple
  • 本文由 发表于 2020年1月7日 02:49:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定