jenkins 简单实现php集成上线部署
基于公司git版本控制,搭建jenkins实现php集成部署
一.安装jenkins相关依赖
1
2
3
4
5
6
7
8
9
10
|
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.8.0-openjdk yum install php yum install php-devel yum install php-pear yum install re2c yum install php-pear-phing |
二.jenkins配置和启动
1
2
|
service jenkins start 输入localhost:8080即可访问。 |
三.插件安装
1
2
3
4
|
系统管理-插件设置 安装以下插件 Phing(php构建工具) Publish Over SSH Plugin(通过ssh发布代码) |
四.配置SSH免登录
参考:ssh免密码登录
五.配置Publish Over SSH Plugin
系统管理—系统设置
Jenkins SSH Key:生成rsa的时候设置的ssh key,若没有设置则留空。
Key:填上发布服务器(jenkins所在机器)私钥的内容。默认路径 ~/.ssh/id_rsa
Hosename:填上部署服务器(应用服务器)的IP
username:填上部署服务器(应用服务器)的帐号
六.配置phing build.xml
进入当前job的项目目录 例如:cd /var/lib/jenkins/workspace/git
build.xml文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?xml version= "1.0" encoding= "UTF-8" ?> <project name= "api" default = "build" > <target name= "build" depends= "make_runtime,check,tar" /> <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= "test" /> <fileset id= "api.tar.gz" dir= "." > 注意:此处为对上传文件的限制,这里还可以使用 exclude 排除文件和文件夹,如果没有限制默认为上传所有 < include name= "test/**" /> < include name= "*.php" /> < include name= "*.xml" /> </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> <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= "tar" 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= "api.tar.gz" /> </tar> </target> </project> |
七.开始构建项目
新建—输入项目名称,选择构建自由风格的软件项目—下一步
八.配置源码管理(以git为例)
增加构建步骤,选择Invoke Phing targets,再新增一个构建步骤,选择Send files or execute commands over SSH
Phing targets配置如下
Send files or execute commands over SSH 配置如下
注释:
Source files:dist/api.${BUILD_NUMBER}.${BUILD_ID}.tar.gz
Remove prefix:dist/
Remote directory: /home/data/
Exec command:
mkdir -p /home/data/
mkdir-p /home/www/
cd /home/data/
tar -zxf api.${BUILD_NUMBER}.${BUILD_ID}.tar.gz -C /home/www/
**此处可以写进入到 应用服务器的shell脚本,例如给指定文件夹分配权限
/test/data/ 为应用服务器接收打包文件的目录
/test/www/ 为应用服务器PHP项目运行的目录
保存即可。
接下来进入项目–立即构建,完成后我们进入应用服务器,查看代码。
部署完成~