Using Global Environment Variables

Intro

Environment variables are variables which values change depending on the selected environment - development, staging, production. These are useful as they are defined globally per target and then they can be used in server-side components as a dynamic data. Use cases are - database connections, mailer settings, API Keys, URLs etc. which may be different for your different environments.

In our example we will show you how to setup environment variables for your database connections.

Define Environment Variables

First we need to define the Environment Variables.

Open the Server Connect panel:

Click Globals:

And expand Input:

Here you will see $_ENV - this is where we define our environment variables:

Right click $_ENV and add a new variable:

We call it DB_HOST as this will be the variable used for the database connection host:

The same way we add a few more variables - DB_PORT, DB_USER, DB_PASSWORD - their names pretty much explain what will they be used for:

Save the Globals:

And we are done defining our variables.

Environment Variables Values

The next step is to add values for the environment variables per target.

Development Environment

While on the Development target, click the Server Connect settings button:

Open the Environment tab:

And you will see the environment variables which we defined in the Globals. Click the value for the first one and enter the database host for your development target:

Do the same for the rest of the variables:

Click Save and you are done setting up the variables for your Development environment.

Production Environment

We open the Target menu and select Production:

Then we click the Server Connect settings button:

And open the Environment tab:

Here we enter the values needed for the Production Environment:

Click Save and we are done. Do the same for all the environments you’ve set up.

Using Environment Variables for Database Connections

Now open your database connection defined in Server Connect:

Instead of entering the connection options per target in this dialog, save it and deploy the changes, we will just use the environment variables here.

Click the dynamic data picker for the Database Host:

In the Data Picker, select the DB_HOST environment variable:

And click Select:

Do the same for the rest of the Database Connection options:

Save your database connection and you are done! That’s how useful the environment variables are.

10 Likes

7 posts were split to a new topic: Environment Variables questions

Christmas all over again :tada: :confetti_ball:. Wappler you did it again :champagne:. I have been waiting for this for years lol. :clap:

4 Likes

Yes, this IS brilliant. I somehow missed this update. Bravo :clap: