Exemplo n.º 1
0
 /**
  * 发起上线
  *
  * @throws \Exception
  */
 public function actionStartDeploy()
 {
     $taskId = \Yii::$app->request->post('taskId');
     if (!$taskId) {
         $this->renderJson([], -1, yii::t('walle', 'deployment id is empty'));
     }
     $this->task = Task::findOne($taskId);
     if (!$this->task) {
         throw new \Exception(yii::t('walle', 'deployment id not exists'));
     }
     if ($this->task->user_id != $this->uid) {
         throw new \Exception(yii::t('w', 'you are not master of project'));
     }
     // 任务失败或者审核通过时可发起上线
     if (!in_array($this->task->status, [Task::STATUS_PASS, Task::STATUS_FAILED])) {
         throw new \Exception(yii::t('walle', 'deployment only done for once'));
     }
     // 清除历史记录
     Record::deleteAll(['task_id' => $this->task->id]);
     // 项目配置
     $this->conf = Project::getConf($this->task->project_id);
     $this->walleTask = new WalleTask($this->conf);
     $this->walleFolder = new Folder($this->conf);
     try {
         if ($this->task->action == Task::ACTION_ONLINE) {
             $this->_makeVersion();
             $this->_initWorkspace();
             $this->_preDeploy();
             $this->_revisionUpdate();
             $this->_postDeploy();
             $this->_transmission();
             $this->_updateRemoteServers($this->task->link_id, $this->conf->post_release_delay);
             $this->_cleanRemoteReleaseVersion();
             $this->_cleanUpLocal($this->task->link_id);
         } else {
             $this->_rollback($this->task->ex_link_id);
         }
         /** 至此已经发布版本到线上了,需要做一些记录工作 */
         // 记录此次上线的版本(软链号)和上线之前的版本
         ///对于回滚的任务不记录线上版本
         if ($this->task->action == Task::ACTION_ONLINE) {
             $this->task->ex_link_id = $this->conf->version;
         }
         // 第一次上线的任务不能回滚、回滚的任务不能再回滚
         if ($this->task->action == Task::ACTION_ROLLBACK || $this->task->id == 1) {
             $this->task->enable_rollback = Task::ROLLBACK_FALSE;
         }
         $this->task->status = Task::STATUS_DONE;
         $this->task->save();
         // 可回滚的版本设置
         $this->_enableRollBack();
         // 记录当前线上版本(软链)回滚则是回滚的版本,上线为新版本
         $this->conf->version = $this->task->link_id;
         $this->conf->save();
     } catch (\Exception $e) {
         $this->task->status = Task::STATUS_FAILED;
         $this->task->save();
         // 清理本地部署空间
         $this->_cleanUpLocal($this->task->link_id);
         throw $e;
     }
     $this->renderJson([]);
 }