【问题标题】:React Application to have a staging environment and deploying with Docker on AWS Elastic BeanStalkReact Application 拥有一个暂存环境并在 AWS Elastic BeanStalk 上使用 Docker 进行部署
【发布时间】:2020-05-02 14:22:46
【问题描述】:

我正在开发一个由 create-react-app 创建的反应应用程序。我在 AWS Elastic BeanStalk 上有两个环境,暂存和生产。我正在通过 Docker 部署我的应用程序。我正在使用 CircleCI 进行自动化部署。

我的问题是我想在构建反应应用程序时更改端点 url。

我正在使用 cross-env 设置变量 REACT_APP_API_HOST,但我必须运行命令 build:staging 来构建它。

我不知道如何使用这个 docker。

Docker 文件

FROM node:12

WORKDIR /app
COPY . /app

RUN npm install
RUN npm run build

RUN npm install -g serve

EXPOSE 3000

ENTRYPOINT ["serve", "-l", "3000", "-s", "build", "-d"]

还有 package.json 文件的脚本部分

"scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject",
        "build:staging": "cross-env REACT_APP_API_HOST=staging react-scripts build",
        "build:prod": "cross-env REACT_APP_API_HOST=production react-scripts build"
    },

【问题讨论】:

    标签: javascript reactjs amazon-web-services docker circleci


    【解决方案1】:

    所以经过长时间的研发,我和朋友找到的答案是在将其部署到 Elastic Beanstalk 之前运行一个中间脚本。

    buildDocker.sh

    #!/bin/bash
    branch_name=$(git symbolic-ref --short -q HEAD)
    
    if [ "$branch_name" == "develop" ]; then
        result_sed=$(sed 's/RUN npm run build/RUN npm run build:staging/g' Dockerfile)
        echo "$result_sed" > Dockerfile
    elif [ "$branch_name" == "master" ]; then
        result_sed=$(sed 's/RUN npm run build/RUN npm run build:prod/g' Dockerfile)
        echo "$result_sed" > Dockerfile
    fi
    

    【讨论】:

    • 你为什么要这样做?只需为您的暂存设置创建一个不同的环境,然后让 CodePipeline 与您的 Bitbucket/Github 集成以监听更改
    猜你喜欢
    • 2015-10-23
    • 2015-04-03
    • 2016-07-18
    • 2016-06-19
    • 2015-08-05
    • 1970-01-01
    • 2017-01-29
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多