您的位置 首页 php

Jenkins 发布PHP项目之二 自动化部署

接上一篇
Jenkins 发布PHP项目之一自动化部署

   

下面通过phing来发布PHP项目测试:

安装插件:

配置Publish Over SSH Plugin

系统管理—系统设置
Jenkins SSH Key:生成rsa的时候设置的ssh key,若没有设置则留空。

Key:填上发布服务器(jenkins所在机器)私钥的内容。默认路径 / var /lib/jenkins/.ssh/id_rsa

Hosename:填上部署服务器(应用服务器)的IP 10.211.55.3 这里特别说明一下,我的Jenkins服务器和我的应用服务器是同一台服务器只是目录不一样。

username:填上部署服务器(应用服务器)的帐号 www

在安装了Jenkins 的服务器上需要安装这个命令【a PHP build tool】

yum install php-pear-phing

# phing –help

Jenkins— 管理Jenkins–配置系统

The name of this configuration. This will be appear in the drop down list in the job configuration.

$ id

uid=1002(www) gid=1002(www) groups=1002(www)

/var/lib/jenkins/.ssh/id_rsa.pub内容放到 /home/www/.ssh/authorized_keys

cat /var/lib/jenkins/.ssh/id_rsa.pub >> /home/www/.ssh/authorized_key 这样下面的Puhlish ovef SSH测试OK

下图中的配置远程目录是/home/www/web01后来发现这个目录直接指到/就可以了。其实这个/就是代表的我的发布目录的根目录

如果要延伸目录层级,那就要确保的服务器上目录是真实存在的。比如 /home/www/web01

新建一个任务:

这里可以选择复制-也可以选择Freestyle project

源码管理这里不再多叙:

可以参考前面的文章:

至此,我们可以构建一次去生成目录,因为后面需要用到对应目录。

下面开始构建部分:

构建下

增加构建步骤,选择Invoke Phing targets,

参考文本配置如下:

 ,我们buildid=${BUILD_ID}
buildnumber=${BUILD_NUMBER}  

这里可以看到有个build.xml文件的调用。

参考:

 /var/lib/jenkins/workspace/php-deploy-ssh/build.xml


<?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=".">
            <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>  

再新增一个构建步骤,

选择Send files or execute commands over SSH

Send files or execute commands over SSH 配置如下

参考注释:

Source files:dist/api.${BUILD_NUMBER}.${BUILD_ID}.tar.gz

Remove prefix:dist/ 通过最终输出结果可以看到这个dist目录 /var/lib/jenkins/workspace/php-deploy-ssh/dist

Remote directory:/home/data/ 这个需要我手动创建这个目录,且我的www用户的属主:mkdir /home/data/ && chown www:www data

Exec command :通过执行的结果来看下面的这个命令是在/home/www目录下再去创建了 /home/data/ 和/home/www/

cd /home/data/

tar -zxf api.${BUILD_NUMBER}.${BUILD_ID}.tar.gz -C /home/www/

# tree

`– api.13.13.tar.gz

0 directories, 1 file

# pwd

/home/www

centos7 root@parallels:/home/www# tree

.|– build.xml

`– phpinfo.php

0 directories, 2 files

现在构建,看看控制台输出

如果这里有报错的话,也不要担心,直接可以通过控制台输出查看具体的出错原因。

 控制台输出
Started by user admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/php-deploy-ssh
The recommended  git  tool is: NONE
using credential 73d0c28a-8320-4393-86eb-2b60472899a1
 > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > /usr/bin/git config remote.origin.url git@10.211.55.3:/home/git/repos/app.git # timeout=10
Fetching upstream changes from git@10.211.55.3:/home/git/repos/app.git
 > /usr/bin/git --version # timeout=10
 > git --version # 'git version 1.8.3.1'
using GIT_SSH to set credentials 
 > /usr/bin/git fetch --tags --progress git@10.211.55.3:/home/git/repos/app.git + refs /heads/*:refs/remotes/origin/* # timeout=10
 > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision e6b46a6cac691d2e01c3a17de454fe33c8491584 (refs/remotes/origin/master)
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f e6b46a6cac691d2e01c3a17de454fe33c8491584 # timeout=10
Commit message: "add php2 file"
 > /usr/bin/git rev-list --no-walk e6b46a6cac691d2e01c3a17de454fe33c8491584 # timeout=10
looking for '/var/lib/jenkins/workspace/php-deploy-ssh/build.xml' ... 
use '/var/lib/jenkins/workspace/php-deploy-ssh' as a working directory. 
[php-deploy-ssh] $ phing -buildfile /var/lib/jenkins/workspace/php-deploy-ssh/build.xml -Dbuildid=13 -Dbuildnumber=13 tar -logger phing.listener.DefaultLogger
Buildfile: /var/lib/jenkins/workspace/php-deploy-ssh/build.xml

api > check:

   [delete] Deleting directory /var/lib/jenkins/workspace/php-deploy-ssh/dist
    [mkdir] Created dir: /var/lib/jenkins/workspace/php-deploy-ssh/dist

api > tar:

     [echo] Creating distribution tar for api 1.1.0
   [delete] Could not find file /var/lib/jenkins/workspace/php-deploy-ssh/dist/api.13.13.tar.gz to delete.
      [tar] Building tar: /var/lib/jenkins/workspace/php-deploy-ssh/dist/api.13.13.tar.gz

BUILD FINISHED

Total time: 0.0457 seconds

SSH: Connecting from host [parallels]
SSH: Connecting with configuration [php] ...
SSH: EXEC: STDOUT/STDERR from command [cd /home/data/
tar -zxf api.13.13.tar.gz -C /home/www/ ] ...
SSH: EXEC: completed after 202 ms
SSH: Disconnecting configuration [php] ...
SSH: Transferred 1 file(s)
Build step 'Send files or execute commands over SSH' changed build result to SUCCESS
Finished: SUCCESS  

文章来源:智云一二三科技

文章标题:Jenkins 发布PHP项目之二 自动化部署

文章地址:https://www.zhihuclub.com/78977.shtml

关于作者: 智云科技

热门文章

网站地图