持续集成解决的问题
- 统一代码发布
- 自动构建工程
- 多机自动化部署
主要使用工具
- LINUX服务器
- 集成工具Jenkins
- 构建工具Phing
环境搭建
- jenkins+php安装
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum install jenkins
yum install java
yum install java-1.6.0-openjdk
yum install php
yum install php-devel
yum install php-pear
yum install re2c
yum install php-pecl-imagick
yum install php-dom
yum install php-pear-phing
yum install php-phpunit-PHPUnit
yum install php-phpunit-phpcpd
pear channel-discover pear.phpmd.org
pear remote-list -c pdepend
pear install --alldeps pdepend/PHP_Depend
pear install --alldeps phpmd/PHP_PMD
- 启动jenkins浏览器访问 (服务器IP:8080)
service jenkins start
-
安装拓展
注:首次进入可以选择默认安装拓展
在浏览器打开 系统管理->管理插件->可选插件 搜索以下必须安装的拓展:
Role Strategy(基于角色的权限管理)
Publish Over SSH Plugin(通过ssh发布代码)
Phing(php构建工具)
PMD(代码静态检查)
Plot
JDepend
DRY
- 配置ssh面密码登录
注:发布机->jenkins所在的服务器 生产机->运行项目的服务器
在发布机生成公私钥
ssh-keygen -t rsa
在/root/.ssh/可以看到两个文件
- 将公钥发送到发布机
将发布机上的 /root/.ssh/id_rsa.pub 传送到 生产机的 .ssh/(目录没有.ssh请创建)
将 id_rsa.pub 改名为 authorized_keys
测试无密码链接 ssh 生产机IP - 在Jenkins配置ssh源
系统管理->系统设置
在页面找到Publish over SSH栏
配置 SSH Servers 如图:
最后点击测试 看一下是否成功。
- 在svn的项目的根目录下创建build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="test-corn" default="build">
<target name="build" depends="make_runtime,pdepend,phpmd,phpcpd,test,check,test-cron"/>
<property name="version-m" value="1.1" />
<property name="version" value="1.1.0" />
<property name="stability" value="stable" />
<property name="releasenotes" value="" />
<property name="tarfile" value="${phing.project.name}.${buildnumber}.${buildid}.tar.gz" />
<property name="pkgfile" value="${phing.project.name}.${version}.tgz" />
<property name="distfile" value="dist/${tarfile}" />
<property name="tests.dir" value="tests" />
<!--####程序的目录(根据实际情况更改)-->
<fileset id="test-cron.tar.gz" dir="./trunk">
<include name="Application/**"/>
<exclude name="Application/Common/Conf/config.php"/>
<exclude name="Application/Runtime/**"/>
<include name="Public/**"/>
<include name="ThinkPHP/**"/>
<include name="*.php"/>
</fileset>
<!--####集成构建相关配置-->
<target name="make_runtime">
<mkdir dir="${project.basedir}/Runtime" />
<mkdir dir="${project.basedir}/build/logs" />
<mkdir dir="${project.basedir}/build/pdepend" />
<mkdir dir="${project.basedir}/build/code-browser" />
</target>
<!--####php代码规模分析工具配置-->
<target name="pdepend" description="Calculate software metrics using PHP_Depend">
<exec executable="pdepend">
<arg value="--jdepend-xml=${project.basedir}/build/logs/jdepend.xml"/>
<arg value="--jdepend-chart=${project.basedir}/build/pdepend/dependencies.svg"/>
<arg value="--overview-pyramid=${project.basedir}/build/pdepend/overview-pyramid.svg"/>
<arg path="${project.basedir}/"/>
</exec>
</target>
<!--####php代码静态检查工具配置-->
<target name="phpmd" description="Perform project mess detection using PHPMD">
<phpmd>
<fileset dir="${project.basedir}">
<include name="protected/*.php" />
<include name="*.php" />
</fileset>
</phpmd>
</target>
<!--####php代码分析工具配置-->
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<phpcpd>
<fileset dir="${project.basedir}">
<include name="*.php" />
</fileset>
<formatter type="pmd" outfile="pmd-cpd.xml"/>
</phpcpd>
</target>
<!--####php单例测试配置-->
<target name="test" description="Run PHPUnit tests">
<phpunit haltonerror="true" haltonfailure="true" printsummary="true">
<batchtest>
<fileset dir="${tests.dir}">
<include name="**/*Test.php" />
</fileset>
</batchtest>
</phpunit>
</target>
<!--####构建参数配置-->
<target name="check" description="Check variables" >
<fail unless="version" message="Version not defined!" />
<fail unless="buildnumber" message="buildnumber not defined!" />
<fail unless="buildid" message="buildid not defined!" />
<delete dir="dist" failonerror="false" />
<mkdir dir="dist" />
</target>
<!--####构建参数配置-->
<target name="test-cron" depends="check" description="Create tar file for release">
<echo msg="Creating distribution tar for ${phing.project.name} ${version}"/>
<delete file="${distfile}" failonerror="false"/>
<tar destfile="${distfile}" compression="gzip">
<fileset refid="test-cron.tar.gz"/>
</tar>
</target>
</project>
创建项目
-
在首页->新建->构建一个自由风格的软件项目
-
配置SVN
-
配置构建触发的条件 默认是手动构建
Build after other projects are built:在其他项目触发的时候触发,里面有分为三种情况,也就是其他项目构建成功、失败、或者不稳定(这个不稳定我这里还木有理解)时候触发项目
Poll SCM:定时检查源码变更(根据SCM软件的版本号),如果有更新就checkout最新code下来,然后执行构建动作。我的配置如下:
*/5 * * * * (每5分钟检查一次源码变化)
Build periodically:周期进行项目构建(它不care源码是否发生变化),我的配置如下:
0 2 * * * (每天2:00 必须build一次源码)
-
php使用Phing拖拽构建
- 设置构建完成后的操作(文档保存和ssh部署)
最后保存回到主页点击构建 测试部署。