Exemple #1
0
 public function build($force = false)
 {
     //
     // Since some SCM operations might take a while, put the project on
     // BUILDING status right away
     //
     $status = $this->getStatus();
     $this->setStatus(self::STATUS_BUILDING);
     $this->_save(true);
     // We want the building status to update imediatelly
     $this->setStatus($status);
     $params = array();
     $scm = new ScmConnector($this->getScmConnectorType(), $this->getScmLocalWorkingCopy(), $this->getScmRemoteRepository(), $this->getScmUsername(), $this->getScmPassword(), $this->getScmEnvVars());
     if ($this->getStatus() == self::STATUS_ERROR && !$force) {
         $this->touchDateCheckedForChanges();
         SystemEvent::raise(SystemEvent::INFO, "Project is in error state, to build you need to force. [PROJECTID={$this->getId()}]", __METHOD__);
         return false;
     }
     if ($this->getStatus() == self::STATUS_BUILDING) {
         SystemEvent::raise(SystemEvent::INFO, "Project is currently building, or is queued for building. [PROJECTID={$this->getId()}]", __METHOD__);
         //$this->setStatus(self::STATUS_ERROR);
         return false;
     }
     if ($this->getStatus() != self::STATUS_MODIFIED) {
         //
         // Checkout required?
         //
         $this->touchDateCheckedForChanges();
         if ($this->getStatus() == self::STATUS_UNINITIALIZED || !file_exists($this->getScmLocalWorkingCopy())) {
             if (!$scm->checkout()) {
                 SystemEvent::raise(SystemEvent::INFO, "Couldn't checkout sources. [PROJECTID={$this->getId()}]", __METHOD__);
                 $this->setStatus(self::STATUS_UNINITIALIZED);
                 return false;
             }
         } else {
             if ($this->getStatus() == self::STATUS_UNBUILT) {
                 $force = true;
             }
             if (!$scm->isModified()) {
                 SystemEvent::raise(SystemEvent::INFO, "No modifications detected. [PROJECTID={$this->getId()}]", __METHOD__);
                 if (!$force) {
                     //$this->setStatus(self::STATUS_OK);
                     return false;
                 }
             }
         }
     }
     $this->setStatus(self::STATUS_MODIFIED);
     $rev = null;
     // Keep this for now, add it to the project build later.
     if (!$scm->update($rev)) {
         SystemEvent::raise(SystemEvent::INFO, "Couldn't update local sources. [PROJECTID={$this->getId()}]", __METHOD__);
         if (!$force) {
             $this->setStatus(self::STATUS_ERROR);
             return false;
         }
     }
     //
     // Clean up sources dir and fresh populate it with the new SCM sources
     //
     if (!Framework_Filesystem::emptyDir($this->getSourcesDir()) || !$scm->export($this->getSourcesDir())) {
         SystemEvent::raise(SystemEvent::INFO, "Couldn't refresh the sources dir. [PROJECTID={$this->getId()}]", __METHOD__);
         $this->setStatus(self::STATUS_ERROR);
         return false;
     }
     // We're now building
     $this->setStatus(self::STATUS_BUILDING);
     //$this->_save(true); // We want the building status to update imediatelly
     //
     // Scm stuff done, setup a new build for the project
     //
     $build = new Project_Build($this);
     $build->setScmRevision($rev);
     $this->triggerNotification(NotificationSettings::BUILD_STARTED);
     if (!$build->init()) {
         $this->setStatus(self::STATUS_FAILED);
         SystemEvent::raise(SystemEvent::INFO, "Integration build failed. [PROJECTID={$this->getId()}]", __METHOD__);
         $this->triggerNotification(NotificationSettings::BUILD_FAILED);
         return false;
     }
     $this->setStatus(self::STATUS_OK);
     $this->incrementStatsNumBuilds();
     $build->setLabel($this->getReleaseLabel());
     // make sure the project's release counter was incremented
     if ($this->getOptionReleasePackage()) {
         $build->generateReleasePackage();
     }
     SystemEvent::raise(SystemEvent::INFO, "Integration build successful. [PROJECTID={$this->getId()}]", __METHOD__);
     $this->triggerNotification(NotificationSettings::BUILD_SUCCESS);
     return true;
 }