* @package PHPCI * @subpackage Core */ class RemoteGitBuild extends Build { /** * Get the URL to be used to clone this remote repository. */ protected function getCloneUrl() { return $this->getProject()->getReference(); } /** * Create a working copy by cloning, copying, or similar. */ public function createWorkingCopy(Builder $builder, $buildPath) { $yamlParser = new YamlParser(); $key = trim($this->getProject()->getGitKey()); if (!empty($key)) { $success = $this->cloneBySsh($builder, $buildPath); } else { $success = $this->cloneByHttp($builder, $buildPath); } if (!$success) { $builder->logFailure('Failed to clone remote git repository.'); return false; } if (!is_file($buildPath . 'phpci.yml')) { $builder->logFailure('Project does not contain a phpci.yml file.'); return false; } $yamlFile = file_get_contents($buildPath . 'phpci.yml'); $builder->setConfigArray($yamlParser->parse($yamlFile)); return true; } /** * Use an HTTP-based git clone. */ protected function cloneByHttp(Builder $builder, $cloneTo) { return $builder->executeCommand('git clone -b %s %s "%s"', $this->getBranch(), $this->getCloneUrl(), $cloneTo); } /** * Use an SSH-based git clone. */ protected function cloneBySsh(Builder $builder, $cloneTo) { // Copy the project's keyfile to disk: $keyPath = realpath($cloneTo); if ($keyPath === false) { $keyPath = dirname($cloneTo); } $keyFile = $keyPath . '.key'; file_put_contents($keyFile, $this->getProject()->getGitKey()); chmod($keyFile, 0600); // Use the key file to do an SSH clone: $cmd = 'eval `ssh-agent -s` && ssh-add "%s" && git clone -b %s %s "%s" && ssh-agent -k'; $success = $builder->executeCommand($cmd, $keyFile, $this->getBranch(), $this->getCloneUrl(), $cloneTo); $builder->executeCommand('cd "%s" && git checkout %s', $cloneTo, $this->getCommitId()); // Remove the key file: unlink($keyFile); return $success; } }