Change config file depending on current environment/branch


#1

Hi! I have a project with a file “config.json” that contains connection string to the database, port and other settings to run the application. The project is hosted on github so that I store different values in that file in my development and production branches. Let’s say after testing I would like to merge all changes from development branch to production branch without moving dev config file to prod, so the question would be:

Is it possible to move those settings in environment variables in Circle CI so that "config.json" will be changed with the corresponding values depending on what branch triggered the build?

Example:

Doing a commit in “dev” branch would trigger a build that replaces variables in “config.json” file with development values and “master” branch – production values.

config.json could look like this:

{
  "db": {
    "name": "$DB_NAME",
    "username": "$DB_USERNAME",
    "password": "$DB_PASSWORD"
  },
  "server": {
    "port": "$APP_PORT"
  }
}

It must be a very usual problem - setup different config values for environments, how do you solve it with circle ci?

Appreciate any answer! :+1:


#2

Yes, if you look at the “Spin Up Environment” step in a job that has already run, you will see what env vars you get for free. I think you want CIRCLE_BRANCH. You can then use that to select a JSON file and copy it into place, using a separate Bash script, or a multi-line run section in your config.yml.