In Yii2, is it possible to use a .env file as JSON for an Azure Pipeline configuration?
In Yii2, is it possible to use a .env file as JSON for an Azure Pipeline configuration?
Yes, you can put JSON as a string in a Yii2 .env file and decode it in PHP using json_decode().
For Azure Pipelines, either read it from .env or pass it as a pipeline variable and decode it the same way.
Yes, in Yii you can use environment configuration from a JSON file instead of a traditional .env file, especially for an Microsoft Azure Pipeline setup.
A common approach is:
$config = json_decode(file_get_contents(__DIR__ . '/config/env.json'), true);defined('YII_DEBUG') or define('YII_DEBUG', $config['YII_DEBUG']);defined('YII_ENV') or define('YII_ENV', $config['YII_ENV']);
Example env.json:
{ "YII_DEBUG": true, "YII_ENV": "dev", "DB_HOST": "localhost", "DB_NAME": "yii2db"}
In Azure Pipelines, you can create this file during build/release:
- script: | echo '{ "YII_DEBUG": true, "YII_ENV": "prod" }' > config/env.json
This works fine as long as your application loads the JSON before using the values.