php-censor/PHPCI/Model/Build/RemoteGitBuild.php

121 lines
3.3 KiB
PHP
Raw Normal View History

<?php
/**
2013-05-16 03:16:56 +02:00
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2013, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link http://www.phptesting.org/
*/
namespace PHPCI\Model\Build;
use PHPCI\Model\Build;
use PHPCI\Builder;
use Symfony\Component\Yaml\Parser as YamlParser;
/**
* Remote Git Build Model
* @author Dan Cryer <dan@block8.co.uk>
* @package PHPCI
* @subpackage Core
*/
2013-06-19 17:47:25 +02:00
class RemoteGitBuild extends Build
{
/**
* Get the URL to be used to clone this remote repository.
*/
2013-06-19 17:47:25 +02:00
protected function getCloneUrl()
{
return $this->getProject()->getReference();
}
/**
* Create a working copy by cloning, copying, or similar.
*/
public function createWorkingCopy(Builder $builder, $buildPath)
{
$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;
}
2014-03-15 06:18:12 +01:00
$build_config = $this->getProject()->getBuildConfig();
if (!$build_config)
{
if (!is_file($buildPath . '/phpci.yml')) {
$builder->logFailure('Project does not contain a phpci.yml file.');
return false;
}
$build_config = file_get_contents($buildPath . '/phpci.yml');
}
2014-03-15 06:18:12 +01:00
$yamlParser = new YamlParser();
$builder->setConfigArray($yamlParser->parse($build_config));
return true;
}
/**
* Use an HTTP-based git clone.
*/
2013-10-10 02:01:06 +02:00
protected function cloneByHttp(Builder $builder, $cloneTo)
{
$success = $builder->executeCommand('git clone -b %s %s "%s"', $this->getBranch(), $this->getCloneUrl(), $cloneTo);
if (!empty($commit) && $commit != 'Manual') {
2014-03-12 18:37:57 +01:00
$cmd = 'cd "%s" && git checkout %s';
if (IS_WIN) {
$cmd = 'cd /d "%s" && git checkout %s';
}
$builder->executeCommand($cmd, $cloneTo, $this->getCommitId());
}
return $success;
}
/**
* Use an SSH-based git clone.
*/
2013-10-10 02:01:06 +02:00
protected function cloneBySsh(Builder $builder, $cloneTo)
{
// Copy the project's keyfile to disk:
2013-10-10 02:01:06 +02:00
$keyPath = realpath($cloneTo);
2013-07-30 03:55:29 +02:00
if ($keyPath === false) {
2013-10-10 02:01:06 +02:00
$keyPath = dirname($cloneTo);
2013-07-30 03:55:29 +02:00
}
$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';
2013-10-10 02:01:06 +02:00
$success = $builder->executeCommand($cmd, $keyFile, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
$commit = $this->getCommitId();
if (!empty($commit) && $commit != 'Manual') {
2014-03-12 18:37:57 +01:00
$cmd = 'cd "%s" && git checkout %s';
if (IS_WIN) {
$cmd = 'cd /d "%s" && git checkout %s';
}
$builder->executeCommand($cmd, $cloneTo, $this->getCommitId());
}
// Remove the key file:
unlink($keyFile);
return $success;
}
}