您的位置:首页 > 编程语言 > PHP开发

jenkins+php+svn快速部署测试环境开发环境快速部署

2018-03-12 13:33 676 查看
jenkins 虽然作为java常用的打包部署工具,不过也可以使用在phpweb项目部署管理,前段时间帮公司部署了开发环境与测试环境,简单分享一下。

1、内网web环境搭建lnmp,centos下编译安装或者yum安装 google有很多资料,这里就不赘述了
2、nginx配置dev域名指向开发环境代码目录,svn设定钩子开发人员有更新后自动svn up更新开发环境代码
       可参考http://www.360doc.com/content/14/0903/23/13647213_406884443.shtml

3、nginx配置test域名指向测试环境代码目录
4、服务器配置java环境后,下载jenkins,http://pkg.jenkins-ci.org/redhat-stable/  war包
直接java -jar jenkins.war启动jenkins,不需要安装啥tomcat(可将此命令加入开机自动启动配置)
5、建立jenkins任务,添加打包脚本,每次构建生成增量更新包与全量更新包,如下:
一、新建一个自由风格项目

二、配置svn

三、定时构建

这个定时是每天12点01分跟18点05分构建一下

四、设置shell打包脚本

cd /root/.jenkins/workspace
#find need update file
find project_name -mmin -1 | grep -v ".svn" > svn.log #jenkins构建后查找1分钟内有修改的文件
#clear history
rm -rf pakage/*
#cope auto file
/usr/local/php/bin/php makeAutoPk.php #将有修改文件打包,打包脚本下面有参考
#zip auto file to target path
cd pakage/<span style="font-family: Arial, Helvetica, sans-serif;">project_name</span><span style="font-family: Arial, Helvetica, sans-serif;">/</span>
datestr=$(date +%Y%m%d%H%M)
targetpath="/home/hn/update_code/"#更新放置的目录
mkdir ${targetpath}${datestr}
filename="/auto.zip"
zip -r ${targetpath}${datestr}${filename} .
#cp all file
cd ..
cp -a ../shop_test all
#delete nouse file
find all -name .svn|xargs rm -rf#删除svn更新文件
cd all
#删除不需要打包的文件
rm -rf temp
rm -rf images

#zip all file to target path
zip -r ${targetpath}${datestr}/all.zip .

#update解压增量包覆盖
unzip -o ${targetpath}${datestr}/auto.zip -d test_path#test_path为测试环境代码目录
</pre>makeAutoPk,php脚本<p></p><p></p><pre name="code" class="php"><?php
/**
* 制作增量包
*/
$fileClass = new FileStatic();
$fileClass->makePackege();

class FileStatic
{

public $targetPath;//目标路径
public $fileArr;//需要打包文件日志
public $deleteFile;//删除的文件
public $projectName;//项目名称
public $sourcePath;//代码源
public function __construct($projectName = 'shop'){
$this->projectName = $projectName;
$log = file_get_contents('svn.log');
$this->fileArr = explode("\n", trim($log));
$this->deleteFile = array();
$this->targetPath = __DIR__.'/pakage/'.date('Ymdhms',time());
$this->sourcePath = __DIR__;
if( !is_dir ($this->targetPath)) $this->mkdirs($this->targetPath,0775);
}
//打包
public function makePackege(){
if(empty($this->fileArr)){
echo "no file to make";
exit;
}
foreach($this->fileArr as $file){
$targetFile = $this->targetPath.'/'.$file;
if(is_dir($file)){//文件夹
if( !is_dir ($targetFile)) $this->mkdirs($targetFile,0775);
}elseif(is_file($file)){//文件
$folderName = dirname($targetFile);
if( !is_dir ($folderName)) $this->mkdirs($folderName,0775);
copy($this->sourcePath.'/'.$file,$targetFile);
}elseif(!file_exists($file)){//不存在的文件夹
$this->deleteFile[] = $file;
}
}
}

/**
* 创建目录
*/
private function mkdirs($dir, $mode = 0775){
if (is_dir($dir) || @mkdir($dir, $mode)){
return true;
}
if (!$this->mkdirs(dirname($dir), $mode)){
return false;
}
return @mkdir($dir, $mode);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息