Deploy React App using AWS Code Pipeline with build variables

Viewed 516

I am looking to deploy a create-react-app as a static site to AWS S3. The build is being orchestrated with CodePipeline.

Our codebase has REACT_APP_**** variables that are injected during the build. How do I inject different variables for our QA and Prod stage of the pipeline. These variables are pointing to different configurations for backend and auth systems.

As part of the pipeline CodeBuild only runs one time so can't figure out how to inject different variable for QA and Prod.

1 Answers

In your buildspec file, you can use build list to define builds which run in parallel for QA and Prod with different environment variables.

batch:
  build-list:
    - identifier: qa_build
      env:
        variables:
          REACT_APP_VAR1: qa_val_1
          REACT_APP_VAR2: qa_val_2
          REACT_APP_VAR3: qa_val_3
    - identifier: prod_build
      env:
        variables:
          REACT_APP_VAR1: prod_val_1
          REACT_APP_VAR2: prod_val_2
          REACT_APP_VAR3: prod_val_3

Since each build will have its own separate artifacts, you will need to configure your pipeline to handle this. This documentation page provides an example of such a configuration.

Related