Jose A
Jose A

Reputation: 11077

Is it possible to set environment variables per branch in the Amplify.yml file (AWS Amplify)?

I'm currently using AWS Amplify to manage my front-end. I've been manually injecting the environment variables throughout the console.

While I have seen that (at least in this case), the environment variables are correctly protected as mentioned in the AWS docs. I wanted to know if it was possible to set in the amplify.yml file variables per branch that do not necessarily need protection.

Something like this:

version: 0.1
env:
  variables:
    myvarOne:
      branch: master
        value: ad
      branch: dev
        value otherval
frontend:
  phases:
    preBuild:
      commands:
        - yarn install
        - yarn lint
        - yarn test
    build:
      commands:
        - yarn build build
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

Upvotes: 5

Views: 4827

Answers (3)

I'm working with the built in variable overriding in Amplify. Seems like the overridden variable is not applied when I having multiple branches connected.(In my case 2 branches- one for development and other for production)

Upvotes: 0

papapalapa
papapalapa

Reputation: 89

I might be pretty late with the answer, but as of the time of writing, it seems like there exists an out-of-the-box solution to your case.

According to the documentation almost verbatim, it asks you to do the following:

  1. Sign in to the AWS Management Console here.
  2. Navigate to App Settings > Environment variables > Manage variables.
  3. In the Manage variables section, under Variable, enter your key. For Value, enter your value.
  4. Choose Actions and then choose Add variable override.. You can override the environment variable for the key based on branches.
  5. You now have a set of environment variables specific to your branch.

This GIF better illustrates steps 4-5.

I don't have enough stackoverflow reputation to add an image, so please refer to the 5th point in the documentation where it describes a way to Add variable override for specific branches. https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html

Upvotes: 8

dfranca
dfranca

Reputation: 5322

So far, it seems there is no ideal solution for your problem However, it is possible to do some workaround to have something like that working

You cannot have per branch environment variables, but you can have per branch commands

So, you can define different variables for different branches and run the appropriate command as you wish

version: 0.1
env:
  variables:
    myvarOne:
      value_master: val
      value_dev: otherval
frontend:
  phases:
    preBuild:
      commands:
        - if [ "${AWS_BRANCH}" = "master" ]; then export VALUE=${value_master}; fi
        - if [ "${AWS_BRANCH}" = "dev" ]; then export VALUE=${value_dev}; fi
        - yarn install
        - yarn lint
        - yarn test
    build:
      commands:
        - yarn build build
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

Upvotes: 6

Related Questions