diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 34c934b3..5a26be8d 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -246,6 +246,14 @@ class Builder $buildId = 'project' . $this->build->getProject()->getId() . '-build' . $this->build->getId(); $this->ciDir = realpath(dirname(__FILE__) . '/../') . '/'; $this->buildPath = $this->ciDir . 'build/' . $buildId . '/'; + + // Setup environment vars that will be accessible during exec() + putenv("PHPCI=1"); + putenv("PHPCI_COMMIT=".$commitId); + putenv("PHPCI_PROJECT=".$this->build->getProject()->getId()); + putenv("PHPCI_BUILD=".$this->build->getId()); + putenv("PHPCI_PROJECT_TITLE=".$this->build->getProject()->getTitle()); + putenv("PHPCI_BUILD_PATH=".$this->buildPath); // Create a working copy of the project: if (!$this->build->createWorkingCopy($this, $this->buildPath)) { diff --git a/PHPCI/Plugin/Env.php b/PHPCI/Plugin/Env.php new file mode 100644 index 00000000..1de016fb --- /dev/null +++ b/PHPCI/Plugin/Env.php @@ -0,0 +1,51 @@ + +* @package PHPCI +* @subpackage Plugins +*/ +class Env implements \PHPCI\Plugin +{ + protected $phpci; + protected $env_vars; + + public function __construct(\PHPCI\Builder $phpci, array $options = array()) + { + $this->phpci = $phpci; + $this->env_vars = $options; + } + + /** + * Adds the specified environment variables to the builder environment + */ + public function execute() + { + $success = true; + foreach ($this->env_vars as $key => $value) { + if (is_numeric($key)) { + // This allows the developer to specify env vars like " - FOO=bar" or " - FOO: bar" + $env_var = is_array($value)? key($value).'='.current($value): $value; + } else { + // This allows the standard syntax: "FOO: bar" + $env_var = "$key=$value"; + } + + if (!putenv($env_var)) { + $success = false; + $this->phpci->logFailure("Unable to set environment variable"); + } + } + return $success; + } +}