Skip to content

Using Environment Variables

kbrookes edited this page Oct 19, 2021 · 1 revision

Environment variables primer

(If you're already familiar with environment variables and how they work, skip this section)

If you're using Joomlatools Server, (and/or Pages), it's possible you'll be integrating your site(s) with APIs.

Given that many of the methods for development & deployment discussed previously involve cloning or forking Joomlatools Server from Github, it seems likely that you may also use Github for development purposes.

If you're using both an online repo and are integrating APIs, you're probably going to need to store API keys - and it's generally considered to be extremely bad practice to check these keys into a repo.

That's where environment variables come in.

Environment variables allow you to set variables which are available to the entire application environment. In the past, this might have been achieved using a .env file (usually not in a web accessible directory) and an additional library which loaded those variables into the environment.

With Docker, Fly.io, and other services this is handled via a config file which sits outside any of the Joomlatools Server sites. In Gitpod this is handled via their own interface.

Environment variable example

If you wanted to store an API key as an environment variable, you might create a variable (specific examples shown after this section) called 'SERVICE_API_KEY' and then in your site, this could be called using getenv('CB_TEST_CLIENT').

This would be available throughout your site just like a normal variable, except it only needs to be declared once in the configuration.

Environment variables & Joomlatools Server

Not only are environment variables used to connect to external services, they're also used to configure the environment your sites will run in.

This allows you to control:

  • Whether the app is in production or development (can be used to auto-set staging/live sites)
  • Debugging on/off
  • Cache on/off
  • Preload on/off

There may be other aspects of the environment that can be controlled this way (Johan please expand if necessary)

Setting environment variables in Docker

The docker-compose.yml file in the root of Joomlatools-Server allows you to add variables in the 'environment' section of the file.

Each variable is preceded by a dash and should be on a new line.

Setting environment variables in Fly.io

Joomlatools Server includes a fly.toml file by default, with some common settings. This defines the setup of the Fly.io environment, but will be updated when you run flyctl launch.

Setting environment variables in Gitpod.io

Although Joomlatools Server comes with a gitpod.yml file, additional environment variables may be stripped out by Gitpod.

Fortunately Gitpod has a dedicated variables page where you can set variables that will be available to the whole app.

Best Practice

Although the Joomlatools Server .gitignore file ignores the docker-compose.yml by default, if you are using fly.toml it would be a good idea to add this file to the .gitignore file as well to prevent checking in sensitive API keys.