From d935c940411550fed914bb35d57f9ed89c65e952 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Tue, 25 Feb 2014 10:16:58 +0000 Subject: [PATCH] Lots of clean up: Fixed remote git build to only checkout a commit if there is one, cleaned up logging to remove the logger cruft from the visible log and shortened paths within the log by removing the build directory, added a build-level exception handler to catch things like failed clones and so on. --- PHPCI/Builder.php | 3 ++- PHPCI/Command/RunCommand.php | 28 ++++++++++++++++++---------- PHPCI/Logging/BuildDBLogHandler.php | 5 ++++- PHPCI/Model/Build.php | 2 ++ PHPCI/Model/Build/RemoteGitBuild.php | 7 ++++++- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 738f7bf3..e1fa56c5 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -268,8 +268,9 @@ class Builder implements LoggerAwareInterface { $buildId = 'project' . $this->build->getProject()->getId( ) . '-build' . $this->build->getId(); - $this->ciDir = dirname(__FILE__) . '/../'; + $this->ciDir = dirname(dirname(__FILE__) . '/../') . '/'; $this->buildPath = $this->ciDir . 'build/' . $buildId . '/'; + $this->build->currentBuildPath = $this->buildPath; $this->interpolator->setupInterpolationVars( $this->build, diff --git a/PHPCI/Command/RunCommand.php b/PHPCI/Command/RunCommand.php index 140a971c..318f9247 100644 --- a/PHPCI/Command/RunCommand.php +++ b/PHPCI/Command/RunCommand.php @@ -22,6 +22,7 @@ use Symfony\Component\Console\Output\OutputInterface; use b8\Store\Factory; use PHPCI\Builder; use PHPCI\BuildFactory; +use PHPCI\Model\Build; /** * Run console command - Runs any pending builds. @@ -68,7 +69,7 @@ class RunCommand extends Command // For verbose mode we want to output all informational and above // messages to the symphony output interface. - if ($input->hasOption('verbose')) { + if ($input->getOption('verbose')) { $this->logger->pushHandler( new OutputLogHandler($this->output, Logger::INFO) ); @@ -88,17 +89,24 @@ class RunCommand extends Command $build = BuildFactory::getBuild($build); - // Logging relevant to this build should be stored - // against the build itself. - $buildDbLog = new BuildDBLogHandler($build, Logger::INFO); - $this->logger->pushHandler($buildDbLog); + try { + // Logging relevant to this build should be stored + // against the build itself. + $buildDbLog = new BuildDBLogHandler($build, Logger::INFO); + $this->logger->pushHandler($buildDbLog); - $builder = new Builder($build, $this->logger); - $builder->execute(); + $builder = new Builder($build, $this->logger); + $builder->execute(); + + // After execution we no longer want to record the information + // back to this specific build so the handler should be removed. + $this->logger->popHandler($buildDbLog); + } catch (\Exception $ex) { + $build->setStatus(Build::STATUS_FAILED); + $build->setLog($build->getLog() . PHP_EOL . PHP_EOL . $ex->getMessage()); + $store->save($build); + } - // After execution we no longer want to record the information - // back to this specific build so the handler should be removed. - $this->logger->popHandler($buildDbLog); } $this->logger->addInfo("Finished processing builds"); diff --git a/PHPCI/Logging/BuildDBLogHandler.php b/PHPCI/Logging/BuildDBLogHandler.php index 60cf48d3..91196dc3 100644 --- a/PHPCI/Logging/BuildDBLogHandler.php +++ b/PHPCI/Logging/BuildDBLogHandler.php @@ -29,7 +29,10 @@ class BuildDBLogHandler extends AbstractProcessingHandler protected function write(array $record) { - $this->logValue .= (string)$record['formatted']; + $message = (string)$record['message']; + $message = str_replace($this->build->currentBuildPath, '/', $message); + + $this->logValue .= $message . PHP_EOL; $this->build->setLog($this->logValue); } } \ No newline at end of file diff --git a/PHPCI/Model/Build.php b/PHPCI/Model/Build.php index bc814560..f17fcc74 100644 --- a/PHPCI/Model/Build.php +++ b/PHPCI/Model/Build.php @@ -26,6 +26,8 @@ class Build extends BuildBase const STATUS_SUCCESS = 2; const STATUS_FAILED = 3; + public $currentBuildPath = null; + /** * Get link to commit from another source (i.e. Github) */ diff --git a/PHPCI/Model/Build/RemoteGitBuild.php b/PHPCI/Model/Build/RemoteGitBuild.php index 4be161bd..35aa8c90 100644 --- a/PHPCI/Model/Build/RemoteGitBuild.php +++ b/PHPCI/Model/Build/RemoteGitBuild.php @@ -89,7 +89,12 @@ class RemoteGitBuild extends Build // 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()); + + $commit = $this->getCommitId(); + + if (!empty($commit) && $commit != 'Manual') { + $builder->executeCommand('cd "%s" && git checkout %s', $cloneTo, $this->getCommitId()); + } // Remove the key file: unlink($keyFile);