diff --git a/Mage/Task/AbstractTask.php b/Mage/Task/AbstractTask.php index 5edd4fa..d4bcf6c 100644 --- a/Mage/Task/AbstractTask.php +++ b/Mage/Task/AbstractTask.php @@ -222,6 +222,7 @@ abstract class AbstractTask */ final protected function runCommand($command, &$output = null) { + $command = ltrim($this->getEnvVarsString() . ' ' . $command); if ($this->getStage() == self::STAGE_DEPLOY || $this->getStage() == self::STAGE_POST_RELEASE) { return $this->runCommandRemote($command, $output); } else { @@ -305,4 +306,56 @@ abstract class AbstractTask } return $result; } + + /** + * Returns the array of environment variables + * Returned array contains both system variables and variables set in config + * WARNING: To access system's variables you need to set proper value in your php.ini at variables_order key + * @see http://php.net/manual/en/ini.core.php#ini.variables-order + * + * @return array + */ + protected function getEnvVariables() + { + $configVars = array_merge( + $this->getConfig()->general('env', []), + $this->getConfig()->environmentConfig('env', []), + $this->getConfig()->getParameter('env', []), + [ + 'variables' => $this->getConfig()->getParameter('env.variables', []) + ] + ); + + if (isset($configVars['variables'])) { + $configVars = $configVars['variables']; + } + + $envVariables = array_merge( + $_ENV, + $configVars + ); + + return $envVariables; + } + + /** + * Returns ready to inject environment string + * The string is build from env vars array in schema: + * key1=value1 key2=value3 ... + * + * @return string + */ + protected function getEnvVarsString() + { + $envVarsArray = $this->getEnvVariables(); + $envVars = array_map( + function ($key, $value) { + return "$key=$value"; + }, + array_keys($envVarsArray), + $this->getEnvVariables() + ); + + return join(' ', $envVars); + } } diff --git a/docs/example-config/.mage/config/environment/production.yml b/docs/example-config/.mage/config/environment/production.yml index 97a3fcc..1af997f 100644 --- a/docs/example-config/.mage/config/environment/production.yml +++ b/docs/example-config/.mage/config/environment/production.yml @@ -22,3 +22,6 @@ tasks: - sampleTask - sampleTaskRollbackAware verbose_logging: true +env: + variables: + symfony_env: prod diff --git a/docs/example-config/.mage/config/general.yml b/docs/example-config/.mage/config/general.yml index c3a9b6d..3accc0e 100644 --- a/docs/example-config/.mage/config/general.yml +++ b/docs/example-config/.mage/config/general.yml @@ -7,3 +7,7 @@ verbose_logging: false scm: type: git url: git://github.com/andres-montanez/Zend-Framework-Twig-example-app.git +env: + variables: + node_path: "/bin/node" + symfony_env: prod