diff --git a/recipe/common.php b/recipe/common.php index 719414ba..2dc0106a 100644 --- a/recipe/common.php +++ b/recipe/common.php @@ -69,7 +69,21 @@ set('use_atomic_symlink', function () { return commandSupportsOption('mv', '--no-target-directory'); }); -set('env', []); // Run command environment (for example, SYMFONY_ENV=prod) +/** + * Remote environment variables. + * ```php + * set('env', [ + * 'KEY' => 'something', + * ]); + * ``` + * + * It is possible to override it per `run()` call. + * + * ```php + * run('echo $KEY', ['env' => ['KEY' => 'over']] + * ``` + */ +set('env', []); /** * Return current release path. diff --git a/src/Support/helpers.php b/src/Support/helpers.php index 4cd9dcf2..e6f6b931 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -88,15 +88,9 @@ function starts_with(string $string, string $startString) } /** - * Take array of key/value and create string of it. - * * This function used for create environment string. - * - * @param array $array - * - * @return string */ -function array_to_string(array $array): string +function env_strinfigy(array $array): string { return implode(' ', array_map( function ($key, $value) { diff --git a/src/functions.php b/src/functions.php index 7492ff5e..5295baa1 100644 --- a/src/functions.php +++ b/src/functions.php @@ -25,7 +25,7 @@ use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; use function Deployer\Support\array_merge_alternate; -use function Deployer\Support\array_to_string; +use function Deployer\Support\env_strinfigy; use function Deployer\Support\str_contains; /** @@ -297,7 +297,8 @@ function within($path, $callback) * Options: * - `timeout` - Sets the process timeout (max. runtime). The timeout in seconds (default: 300 sec). * - `secret` - Placeholder `%secret%` can be used in command. Placeholder will be replaced with this value and will not appear in any logs. - * - `vars` - Array of placeholders to replace in command: `run('echo %key%', ['vars' => ['key' => 'anything does here']])`; + * - `vars` - Array of placeholders to replace in command: `run('echo %key%', ['vars' => ['key' => 'anything does here']]);` + * - `env` - Array of environment variables: `run('echo $KEY', ['env' => ['key' => 'value']]);` * * Examples: * @@ -330,7 +331,7 @@ function run($command, $options = []) $env = array_merge_alternate(get('env', []), $options['env'] ?? []); if (!empty($env)) { - $env = array_to_string($env); + $env = env_strinfigy($env); $command = "export $env; $command"; } @@ -380,7 +381,7 @@ function runLocally($command, $options = []) $env = array_merge_alternate(get('env', []), $options['env'] ?? []); if (!empty($env)) { - $env = array_to_string($env); + $env = env_strinfigy($env); $command = "export $env; $command"; } diff --git a/test/joy/EnvTest.php b/test/joy/EnvTest.php new file mode 100644 index 00000000..a8baf971 --- /dev/null +++ b/test/joy/EnvTest.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Deployer; + +class EnvTest extends AbstractTest +{ + const RECIPE = __DIR__ . '/recipe/env.php'; + + public function testOnce() + { + $this->init(self::RECIPE); + $this->tester->run(['test', '-f' => self::RECIPE]); + + $display = $this->tester->getDisplay(); + self::assertEquals(0, $this->tester->getStatusCode(), $display); + self::assertStringContainsString('global=global', $display); + self::assertStringContainsString('local=local', $display); + } +} diff --git a/test/joy/recipe/env.php b/test/joy/recipe/env.php new file mode 100644 index 00000000..8c485c47 --- /dev/null +++ b/test/joy/recipe/env.php @@ -0,0 +1,14 @@ + 'global', +]); + +task('test', function () { + info('global=' . run('echo $VAR')); + info('local=' . run('echo $VAR', ['env' => ['VAR' => 'local']])); +});