2013-05-16 00:47:37 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-05-12 18:26:17 +02:00
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link https://www.phptesting.org/
|
|
|
|
*/
|
2013-05-16 00:47:37 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Model\Build;
|
2013-05-16 16:40:40 +02:00
|
|
|
|
|
|
|
use PHPCI\Model\Build;
|
|
|
|
use PHPCI\Builder;
|
2013-05-16 00:47:37 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-16 03:30:48 +02:00
|
|
|
* 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
|
2013-05-16 00:47:37 +02:00
|
|
|
{
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Create a working copy by cloning, copying, or similar.
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
public function createWorkingCopy(Builder $builder, $buildPath)
|
|
|
|
{
|
2014-05-13 17:15:33 +02:00
|
|
|
$key = trim($this->getProject()->getSshPrivateKey());
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
if (!empty($key)) {
|
|
|
|
$success = $this->cloneBySsh($builder, $buildPath);
|
|
|
|
} else {
|
|
|
|
$success = $this->cloneByHttp($builder, $buildPath);
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
if (!$success) {
|
|
|
|
$builder->logFailure('Failed to clone remote git repository.');
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2014-03-15 06:22:59 +01:00
|
|
|
return $this->handleConfig($builder, $buildPath);
|
2013-05-16 16:40:40 +02:00
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Use an HTTP-based git clone.
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function cloneByHttp(Builder $builder, $cloneTo)
|
2013-05-16 16:40:40 +02:00
|
|
|
{
|
2015-03-25 11:10:47 +01:00
|
|
|
$cmd = 'git clone --recursive ';
|
2014-05-05 22:03:44 +02:00
|
|
|
|
|
|
|
$depth = $builder->getConfig('clone_depth');
|
|
|
|
|
|
|
|
if (!is_null($depth)) {
|
|
|
|
$cmd .= ' --depth ' . intval($depth) . ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$cmd .= ' -b %s %s "%s"';
|
2014-05-01 17:53:29 +02:00
|
|
|
$success = $builder->executeCommand($cmd, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
|
2014-03-12 17:04:56 +01:00
|
|
|
|
2014-05-12 16:18:42 +02:00
|
|
|
if ($success) {
|
|
|
|
$success = $this->postCloneSetup($builder, $cloneTo);
|
2014-03-12 17:04:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 21:34:31 +01:00
|
|
|
return $success;
|
2013-05-16 16:40:40 +02:00
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Use an SSH-based git clone.
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function cloneBySsh(Builder $builder, $cloneTo)
|
2013-05-16 16:40:40 +02:00
|
|
|
{
|
2014-04-16 18:38:19 +02:00
|
|
|
$keyFile = $this->writeSshKey($cloneTo);
|
2013-07-30 03:55:29 +02:00
|
|
|
|
2014-04-16 18:38:19 +02:00
|
|
|
if (!IS_WIN) {
|
|
|
|
$gitSshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
|
2013-07-30 03:55:29 +02:00
|
|
|
}
|
|
|
|
|
2014-04-16 18:38:19 +02:00
|
|
|
// Do the git clone:
|
2015-03-25 11:10:47 +01:00
|
|
|
$cmd = 'git clone --recursive ';
|
2014-05-05 22:03:44 +02:00
|
|
|
|
|
|
|
$depth = $builder->getConfig('clone_depth');
|
|
|
|
|
|
|
|
if (!is_null($depth)) {
|
|
|
|
$cmd .= ' --depth ' . intval($depth) . ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$cmd .= ' -b %s %s "%s"';
|
2013-07-30 03:55:29 +02:00
|
|
|
|
2014-04-16 18:38:19 +02:00
|
|
|
if (!IS_WIN) {
|
|
|
|
$cmd = 'export GIT_SSH="'.$gitSshWrapper.'" && ' . $cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = $builder->executeCommand($cmd, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
|
2014-02-25 11:16:58 +01:00
|
|
|
|
2014-05-12 16:18:42 +02:00
|
|
|
if ($success) {
|
|
|
|
$success = $this->postCloneSetup($builder, $cloneTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the key file and git wrapper:
|
|
|
|
unlink($keyFile);
|
2014-07-03 22:54:01 +02:00
|
|
|
if (!IS_WIN) {
|
|
|
|
unlink($gitSshWrapper);
|
|
|
|
}
|
2014-05-12 16:18:42 +02:00
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle any post-clone tasks, like switching branches.
|
|
|
|
* @param Builder $builder
|
|
|
|
* @param $cloneTo
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-12 16:18:42 +02:00
|
|
|
protected function postCloneSetup(Builder $builder, $cloneTo)
|
|
|
|
{
|
|
|
|
$success = true;
|
2014-02-25 11:16:58 +01:00
|
|
|
$commit = $this->getCommitId();
|
|
|
|
|
2015-04-21 14:51:55 +02:00
|
|
|
$chdir = IS_WIN ? 'cd /d "%s"' : 'cd "%s"';
|
2014-02-24 21:34:31 +01:00
|
|
|
|
2015-04-21 14:51:55 +02:00
|
|
|
if (!empty($commit) && $commit != 'Manual') {
|
|
|
|
$cmd = $chdir . ' && git checkout %s --quiet';
|
|
|
|
$success = $builder->executeCommand($cmd, $cloneTo, $commit);
|
|
|
|
}
|
2014-07-10 15:27:15 +02:00
|
|
|
|
2015-04-21 14:51:55 +02:00
|
|
|
// Always update the commit hash with the actual HEAD hash
|
|
|
|
if ($builder->executeCommand($chdir . ' && git rev-parse HEAD', $cloneTo)) {
|
|
|
|
$this->setCommitId(trim($builder->getLastOutput()));
|
2014-05-12 16:18:42 +02:00
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
return $success;
|
|
|
|
}
|
2014-04-16 18:38:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an SSH key file on disk for this build.
|
|
|
|
* @param $cloneTo
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function writeSshKey($cloneTo)
|
|
|
|
{
|
|
|
|
$keyPath = dirname($cloneTo . '/temp');
|
|
|
|
$keyFile = $keyPath . '.key';
|
|
|
|
|
|
|
|
// Write the contents of this project's git key to the file:
|
2014-05-13 17:15:33 +02:00
|
|
|
file_put_contents($keyFile, $this->getProject()->getSshPrivateKey());
|
2014-04-16 18:38:19 +02:00
|
|
|
chmod($keyFile, 0600);
|
|
|
|
|
|
|
|
// Return the filename:
|
|
|
|
return $keyFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an SSH wrapper script for Git to use, to disable host key checking, etc.
|
|
|
|
* @param $cloneTo
|
|
|
|
* @param $keyFile
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function writeSshWrapper($cloneTo, $keyFile)
|
|
|
|
{
|
|
|
|
$path = dirname($cloneTo . '/temp');
|
|
|
|
$wrapperFile = $path . '.sh';
|
|
|
|
|
2014-05-01 17:53:29 +02:00
|
|
|
$sshFlags = '-o CheckHostIP=no -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o PasswordAuthentication=no';
|
|
|
|
|
2014-04-16 18:38:19 +02:00
|
|
|
// Write out the wrapper script for this build:
|
|
|
|
$script = <<<OUT
|
|
|
|
#!/bin/sh
|
2014-05-01 17:53:29 +02:00
|
|
|
ssh {$sshFlags} -o IdentityFile={$keyFile} $*
|
2014-04-16 18:38:19 +02:00
|
|
|
|
|
|
|
OUT;
|
|
|
|
|
|
|
|
file_put_contents($wrapperFile, $script);
|
|
|
|
shell_exec('chmod +x "'.$wrapperFile.'"');
|
|
|
|
|
|
|
|
return $wrapperFile;
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
}
|