feat: add post-create support for env key in app.json

Closes #4498
This commit is contained in:
Jose Diaz-Gonzalez
2026-01-07 03:21:04 -05:00
parent ad00918891
commit 34c336c668
14 changed files with 653 additions and 4 deletions

View File

@@ -44,6 +44,103 @@
- `schedule`: (string, required)
- `concurrency_policy`: (string, optional, default: `allow`, options: `allow`, `forbid`, `replace`)
## Env
```json
{
"env": {
"SIMPLE_VAR": "default_value",
"SECRET_KEY": {
"description": "A secret key for signing tokens",
"generator": "secret"
},
"DATABASE_URL": {
"description": "PostgreSQL connection string",
"required": true
},
"OPTIONAL_VAR": {
"description": "An optional configuration value",
"value": "default",
"required": false
},
"SYNC_VAR": {
"description": "A variable that updates on every deploy",
"value": "synced_value",
"sync": true
}
}
}
```
(object, optional) A key-value object for environment variable configuration. Keys are the variable names. Values can be either a string (used as the default value) or an object with the following properties:
- `description`: (string, optional) Human-readable explanation of the variable's purpose
- `value`: (string, optional) Default value for the variable
- `required`: (boolean, optional, default: `true`) Whether the variable must have a value
- `generator`: (string, optional) Function to generate the value. Currently only `"secret"` is supported, which generates a 64-character cryptographically secure hex string
- `sync`: (boolean, optional, default: `false`) If `true`, the value will be set on every deploy, overwriting any existing value
### Behavior
Environment variables from `app.json` are processed during the first deploy, before the predeploy script runs. The behavior depends on the variable configuration:
1. **Variables with `value` or simple string**: The default value is set if the variable doesn't already exist
2. **Variables with `generator: "secret"`**: A random 64-character hex string is generated if the variable doesn't exist
3. **Required variables without a value or generator**: If a TTY is available, the user is prompted for a value. Otherwise, the deploy fails with an error
4. **Optional variables without a value**: Skipped silently if no TTY is available
On subsequent deploys:
- Variables are NOT re-set unless `sync: true` is specified
- Variables with `sync: true` are always set to their configured value, overwriting any manual changes
- Variables that already have values are not modified
### Examples
**Simple default value:**
```json
{
"env": {
"WEB_CONCURRENCY": "5"
}
}
```
**Generated secret (recommended for API keys, tokens, etc.):**
```json
{
"env": {
"SECRET_KEY_BASE": {
"description": "Base secret for session encryption",
"generator": "secret"
}
}
}
```
**Required variable that must be provided:**
```json
{
"env": {
"DATABASE_URL": {
"description": "PostgreSQL connection URL",
"required": true
}
}
}
```
**Variable that stays in sync with app.json:**
```json
{
"env": {
"FEATURE_FLAGS": {
"value": "new_ui,dark_mode",
"sync": true
}
}
}
```
## Formation
```json

View File

@@ -71,6 +71,35 @@ dokku config:export --format shell node-js-app
# ENV='prod' COMPILE_ASSETS='1'
```
## Setting Environment Variables via app.json
Environment variables can also be declared in an `app.json` file in your repository root. This is useful for setting default values, generating secrets, or requiring certain variables to be set before deployment.
```json
{
"env": {
"SIMPLE_VAR": "default_value",
"SECRET_KEY": {
"description": "A secret key for signing tokens",
"generator": "secret"
},
"DATABASE_URL": {
"description": "PostgreSQL connection string",
"required": true
}
}
}
```
Environment variables from `app.json` are processed during the first deploy, before the predeploy script runs. Variables can be configured as:
- **Simple string values**: Set as defaults if not already configured
- **Generated secrets**: Use `"generator": "secret"` to auto-generate a 64-character hex string
- **Required variables**: Use `"required": true` to prompt for or require a value
- **Synced variables**: Use `"sync": true` to update the value on every deploy
For full details on the `env` schema and behavior, see the [app.json documentation](/docs/appendices/file-formats/app-json.md#env).
## Special Config Variables
The following config variables have special meanings and can be set in a variety of ways. Unless specified via global app config, the values may not be passed into applications. Usage of these values within applications should be considered unsafe, as they are an internal configuration values that may be moved to the internal properties system in the future.

View File

@@ -545,6 +545,21 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `config-set`
- Description: Sets one or more config values for an app without restarting (when --no-restart flag is specified)
- Invoked by: app-json plugin
- Arguments: `[--no-restart] $APP $KEY1=VALUE1 [$KEY2=VALUE2 ...]`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `core-post-deploy`
> To avoid issues with community plugins, this plugin trigger should be used _only_ for core plugins. Please avoid using this trigger in your own plugins.