-
Notifications
You must be signed in to change notification settings - Fork 5
Using Environment Variables
(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.
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.
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)
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.
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.
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.
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.