5 Building Environments

Building Environments

Now that we have a setup with working variables, it's time to tackle different requirements for each environment. For instance, the database connection string should vary between the development and production environments.

In our example we want to replace the $example:Website.Url variable with different values for each environment.

  • For dev, use http://localhost:5000
  • For prod, use https://mywebsite.com

We can define these environments in the .confixrc file. Without any specified environments, prod is implicitly added and is taken as the default. To address our needs, we'll list dev and prod environments in the .confixrc.

.confixrc
{
  "component": { ...  },
  "project": {
    "environments": [
      {
        "name": "dev"
      },
      {
        "name": "prod"
      }
    ],
    "configurationFiles": [ ... ],
    "componentProviders": [ ... ],
    "variableProviders": [
      {
        "name": "example",
        "type": "local",
        "path": "$project:/variables.json"
      }
    ]
  }
}

One challenge is that we have only one variables.json file, but we need separate files for each environment. This is a common issue for all variable providers. For example, if using the azure-keyvault provider, you'd likely want different keyvaults for each environment.

Confix configuration has a concept of environmentOverride to allow for settings to changes in each environment. In this scenario, we can set up dev and prod overrides for the example variable provider, specifically changing the path property.

.confixrc
{
  "component": { ...  },
  "project": {
    "environments": [
      {
        "name": "dev"
      },
      {
        "name": "prod"
      }
    ],
    "configurationFiles": [ ... ],
    "componentProviders": [ ... ],
    "variableProviders": [
      {
        "name": "example",
        "type": "local",
        "environmentOverride": {
          "dev": {
            "path": "$project:/variables.dev.json"
          },
          "prod": {
            "path": "$project:/variables.prod.json"
          },
        }
      }
    ]
  }
}

By default, the first listed environment is active unless specified otherwise. To change this, use the --environment flag when running confix.

So, we need to rename the variables.json file to variables.dev.json to match the dev environment.

confix variable set --environment prod
 Running in scope Project
 Configuration loaded
 Active Environment is prod
? Variable name: $example:Website.Url
? Variable value: ********
! The local variable file was not found at the expected location. Created empty file at: '/Users/ex/GettingStarted/src/Website/variables.prod.json'
 Variable $example:Website.Url set successfully.

With these adjustments, we now have a setup with different environments. To build a specific environment, simply use the --environment flag with confix build.

The project structure now looks like this:

Continue to the next section to learn about how you deploy your application.