/** * 更新到指定commit版本 * * @param string $commit * @return bool */ public function updateToVersion($task) { $copy = GlobalHelper::str2arr($task->file_list); $fileAndVersion = []; foreach ($copy as $file) { $fileAndVersion[] = StringHelper::explode($file, " ", true, true); } $branch = $task->branch == 'trunk' ? $task->branch : ($this->getConfig()->repo_mode == Project::REPO_BRANCH ? 'branches/' : 'tags/') . $task->branch; // 先更新 $versionSvnDir = sprintf('%s-svn', rtrim(Project::getDeployWorkspace($task->link_id), '/')); $cmd[] = sprintf('cd %s ', $versionSvnDir); $cmd[] = $this->_getSvnCmd(sprintf('svn checkout %s/%s .', $this->getConfig()->repo_url, $branch)); // 更新指定文件到指定版本,并复制到同步目录 foreach ($fileAndVersion as $assign) { if (in_array($assign[0], ['.', '..'])) { continue; } $cmd[] = $this->_getSvnCmd(sprintf('svn up %s %s', $assign[0], empty($assign[1]) ? '' : ' -r ' . $assign[1])); // 多层目录需要先新建父目录,否则复制失败 if (strpos($assign[0], '/') !== false) { $cmd[] = sprintf('mkdir -p %s/%s', Project::getDeployWorkspace($task->link_id), dirname($assign[0])); } $cmd[] = sprintf('cp -rf %s %s/%s', rtrim($assign[0], '/'), Project::getDeployWorkspace($task->link_id), dirname($assign[0])); } $command = join(' && ', $cmd); return $this->runLocalCommand($command); }
/** * 更新到指定commit版本 * * @param TaskModel $task * @return bool|int */ public function updateToVersion(TaskModel $task) { // 先更新 $versionSvnDir = rtrim(Project::getDeployWorkspace($task->link_id), '/'); $cmd[] = sprintf('cd %s ', $versionSvnDir); $cmd[] = $this->_getSvnCmd(sprintf('svn up -q --force -r %d', $task->commit_id)); $command = join(' && ', $cmd); return $this->runLocalCommand($command); }
/** * 更新到指定commit版本 * * @param string $commit * @return bool */ public function updateToVersion($task) { // 先更新 $destination = Project::getDeployWorkspace($task->link_id); $this->updateRepo($task->branch, $destination); $cmd[] = sprintf('cd %s ', $destination); $cmd[] = sprintf('/usr/bin/env git reset -q --hard %s', $task->commit_id); $command = join(' && ', $cmd); return $this->runLocalCommand($command); }
/** * 更新到指定commit版本 * * @param string $commit * @return bool */ public function updateToVersion($commit, $version) { // 先更新 $destination = Project::getDeployWorkspace($version); $this->updateRepo('master', $destination); $cmd[] = sprintf('cd %s ', $destination); $cmd[] = sprintf('/usr/bin/env git reset %s', $commit); $cmd[] = '/usr/bin/env git checkout .'; $command = join(' && ', $cmd); return $this->runLocalCommand($command); }
/** * 获取远程服务器要操作的任务命令 * * @param $task string * @param $version string * @return string string */ public static function getRemoteTaskCommand($task, $version) { $tasks = GlobalHelper::str2arr($task); if (empty($tasks)) { return ''; } $cmd = []; $workspace = rtrim(Project::getDeployWorkspace($version), '/'); $version = Project::getReleaseVersionDir($version); $pattern = ['#{WORKSPACE}#', '#{VERSION}#']; $replace = [$workspace, $version]; foreach ($tasks as $task) { $cmd[] = preg_replace($pattern, $replace, $task); } return join(' && ', $cmd); }
/** * post-deploy部署代码后置触发任务 * git代码检出之后,可能做一些调整处理,如vendor拷贝,配置环境适配(mv config-test.php config.php) * * @return bool */ public function postDeploy($version) { $tasks = GlobalHelper::str2arr($this->getConfig()->post_deploy); if (empty($tasks)) { return true; } $cmd = []; $workspace = rtrim(Project::getDeployWorkspace($version), '/'); $pattern = ['#{WORKSPACE}#']; $replace = [$workspace]; foreach ($tasks as $task) { $cmd[] = preg_replace($pattern, $replace, $task); } $command = join(' && ', $cmd); return $this->runLocalCommand($command); }
/** * post-deploy部署代码后置触发任务 * git代码检出之后,可能做一些调整处理,如vendor拷贝,配置环境适配(mv config-test.php config.php) * * @return bool */ public function postDeploy($version) { $tasks = GlobalHelper::str2arr($this->getConfig()->post_deploy); if (empty($tasks)) { return true; } // 本地可能要做一些依赖环境变量的命令操作 $cmd = ['source /etc/profile']; $workspace = rtrim(Project::getDeployWorkspace($version), '/'); $pattern = ['#{WORKSPACE}#']; $replace = [$workspace]; // 简化用户切换目录,直接切换到当前部署空间:{deploy_from}/{env}/{project}-YYmmdd-HHiiss $cmd[] = "cd {$workspace}"; foreach ($tasks as $task) { $cmd[] = preg_replace($pattern, $replace, $task); } $command = join(' && ', $cmd); return $this->runLocalCommand($command); }
/** * 更新到指定commit版本 * * @param TaskModel $task * @return bool|int */ public function updateToVersion(TaskModel $task) { // 兼容无trunk、无branches、无tags下为空 $branch = $task->branch == 'trunk' || $task->branch == '' ? $task->branch : ($this->getConfig()->repo_mode == Project::REPO_BRANCH ? 'branches/' : 'tags/') . $task->branch; // 先更新 $versionSvnDir = sprintf('%s-svn', rtrim(Project::getDeployWorkspace($task->link_id), '/')); $cmd[] = sprintf('cd %s ', $versionSvnDir); $cmd[] = $this->_getSvnCmd(sprintf('svn checkout -q %s/%s .', $this->getConfig()->repo_url, $branch)); // 更新指定文件到指定版本,并复制到同步目录 $fileAndVersion = $this->getFileAndVersionList($task); foreach ($fileAndVersion as $assign) { if (in_array($assign['file'], ['.', '..'])) { continue; } $cmd[] = $this->_getSvnCmd(sprintf('svn up -q %s %s', $assign['file'], empty($assign['version']) ? '' : ' -r ' . $assign['version'])); // 多层目录需要先新建父目录,否则复制失败 if (strpos($assign['file'], '/') !== false) { $cmd[] = sprintf('mkdir -p %s/%s', Project::getDeployWorkspace($task->link_id), dirname($assign['file'])); } $cmd[] = sprintf('cp -rf %s %s/%s', rtrim($assign['file'], '/'), Project::getDeployWorkspace($task->link_id), dirname($assign['file'])); } $command = join(' && ', $cmd); return $this->runLocalCommand($command); }
/** * 收尾做处理工作,如清理本地的部署空间 * * @param $version * @return bool|int */ public function cleanUpLocal($version) { $cmd[] = "rm -rf " . Project::getDeployWorkspace($version); if ($this->config->repo_type == Project::REPO_SVN) { $cmd[] = sprintf('rm -rf %s-svn', rtrim(Project::getDeployWorkspace($version), '/')); } $command = join(' && ', $cmd); return $this->runLocalCommand($command); }
/** * 收尾做处理工作,如清理本地的部署空间 * * @param $version * @return bool|int */ public function cleanUpLocal($version) { $cmd[] = 'rm -rf ' . Project::getDeployWorkspace($version); $cmd[] = sprintf('rm -f %s/*.tar.gz', dirname(Project::getDeployPackagePath($version))); $command = join(' && ', $cmd); return $this->runLocalCommand($command); }
/** * 收尾做处理工作,如清理本地的部署空间 * * @param $version * @return bool|int */ public function cleanUp($version) { $command = "rm -rf " . Project::getDeployWorkspace($version); return $this->runLocalCommand($command); }