Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Updated environment variable docs #63

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions docs/deployments/environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Environment Variables
# Environment Variables

Environment variables are a set of named values that can affect the way running processes will behave on a deployment. An environment variable can be used to store secret values that are not safe to be stored in the codebase. You may wish to use
different values for different environments, such as development, preview, and production to connect to different databases or API endpoints.
different values for different environments, such as preview, and production to connect to different databases or API endpoints.

## Creating environment variables

Expand All @@ -17,18 +17,41 @@ Enter the name and value of your variable. All values are encrypted, and can onl

It is also possible to specify what environments the variable should be available in.

<Warning>
After creating or deleting an environment variable, you will need to redeploy your project for the changes to take effect.
</Warning>

## Accessing environment variables

Within your Dart code, you can access environment variables using the `Platform.environment` map.
Within your Dart code, you can access environment variables using the `Platform.environment` map. Any package that uses this map can access Globe environment variables as well.

```dart
String? value = Platform.environment['MY_VARIABLE'];
```

### Using "dotenv" package

If you prefer to use a `.env` file for local development, you can use the [dotenv package](https://pub.dev/packages/dotenv) to load values from the `.env` file locally and use environment variables when deployed.

When your app is deployed to Globe, any static assets are ignored, so you need to pass `includePlatformEnvironment: true` to the `DotEnv` constructor to include platform environment variables that you previously defined on Globe dashboard (Project > Settings > Environment variables).

```dart
final env = DotEnv(includePlatformEnvironment: true)..load();
String? value = env['MY_VARIABLE'];
```

## System environment variables

The following environment variables are available on all deployments:
Your deployed Dart application has access to the Globe-specific environment variables which wouldn't be typically available while you develop/test your app locally. These are available on all deployments:

| Name | Description |
| ------ | ----------------------------------------------- |
| `PORT` | The port that the application should listen on. |
| `GLOBE` | Variable for checking if running in Globe. Will be set to '1' |
| `HOSTNAME` | The hostname of your deployed application in Globe. |
| `CRON_NAME` | Name of the cron job provided in the `globe.yaml` file. |
| `CRON_SCHEDULE` | The cron schedule code. For example, `* * * * *` |
| `CRON_ID` | Unique identifier for the defined cron job. |
| `CRON_EVENT_ID` | Unique identifier for the current running cron job. |

* Cron environment variables are only available when a cron job is running.
Loading