0

I'm attempting to deploy a practice application via AWS CodePipeline. However when I build, I notice the environment variable I added to the buildspec.yml file isn't picked up by my React application.

My buildspec file is below

version: 0.1

env:
  variables:
    REACT_APP_TEST: "If you're seeing this text, the variable worked! Yay! ^o~"

phases:
  install:
    commands:      
      - echo Installing dependencies...
      - npm i
  pre_build:
    commands:
      - echo Nothing to see in prebuld move along! :P
  build:
    commands:
      - echo Build started on `date`
      - npm run build
  post_build:
    commands:
      - echo Build completed on `date`

artifacts:
  files:
    - '**/*'

The variable above is referenced in a simple div using process.env.REACT_APP_TEST According to the docs I should be able to specify environment variables, however the text doesn't show on my application when I deploy it. Am I missing something?

Zeeno
  • 2,671
  • 9
  • 37
  • 60
  • 1
    Make sure you are passing the env variable during npm build command. How you are passing env variable to the react app and how you are using that? – Prakash Sharma Feb 08 '19 at 20:00
  • I was unaware that I needed to pass anything to the build command. Locally I am using a .env file which I assume does this (create-react-app by default handles .env files). Does react (or rather node) not have access to environment variables by default? – Zeeno Feb 08 '19 at 20:04
  • The variable you set here is actually set for the os of that codebuild instance. Your app cannot automatically handle that. You have to pass that variable during build time. Try `REACT_APP_TEST=$REACT_APP_TEST npm run build` in your buildspec file. – Prakash Sharma Feb 08 '19 at 20:32
  • @PrakashSharma That unfortunately did not work. – Zeeno Feb 09 '19 at 10:36

1 Answers1

1

Please use buildspec version of 0.2. There is no reason to use 0.1 version anymore. Just change the "version: 0.1" > "version: 0.2".

The buildspec 0.1 had "environment_variables" > "plaintext" instead of "env" > "variables". However, we strongly recommend you to switch to version 0.2, which is the latest version to get the latest features.

Subin Mathew
  • 2,335
  • 1
  • 16
  • 24