赞
踩
接上一篇
Jenkins发布PHP项目之一自动化部署
https://www.toutiao.com/i6883009657950962183/
下面通过phing来发布PHP项目测试:
安装插件:
系统管理-插件设置
安装以下插件
Phing(php构建工具) This plugin allows you to use Phing to build PHP Project.
Publish Over SSH Plugin(通过ssh发布代码)
Send build artifacts over SSH This plugin is up for adoption! We are looking for new maintainers. Visit our Adopt a Plugin initiative for more information.
配置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文件的调用。
参考:https://www.cnblogs.com/phpworld/p/8745544.html
/var/lib/jenkins/workspace/php-deploy-ssh/build.xml<?xml version="1.0" encoding="UTF-8"?>
再新增一个构建步骤,
选择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 adminRunning as SYSTEMBuilding in workspace /var/lib/jenkins/workspace/php-deploy-sshThe recommended git tool is: NONEusing credential 73d0c28a-8320-4393-86eb-2b60472899a1 > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10Fetching changes from the remote Git repository > /usr/bin/git config remote.origin.url git@10.211.55.3:/home/git/repos/app.git # timeout=10Fetching 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=10Checking out Revision e6b46a6cac691d2e01c3a17de454fe33c8491584 (refs/remotes/origin/master) > /usr/bin/git config core.sparsecheckout # timeout=10 > /usr/bin/git checkout -f e6b46a6cac691d2e01c3a17de454fe33c8491584 # timeout=10Commit message: "add php2 file" > /usr/bin/git rev-list --no-walk e6b46a6cac691d2e01c3a17de454fe33c8491584 # timeout=10looking 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.DefaultLoggerBuildfile: /var/lib/jenkins/workspace/php-deploy-ssh/build.xmlapi > check: [delete] Deleting directory /var/lib/jenkins/workspace/php-deploy-ssh/dist [mkdir] Created dir: /var/lib/jenkins/workspace/php-deploy-ssh/distapi > 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.gzBUILD FINISHEDTotal time: 0.0457 secondsSSH: 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 msSSH: Disconnecting configuration [php] ...SSH: Transferred 1 file(s)Build step 'Send files or execute commands over SSH' changed build result to SUCCESSFinished: SUCCESS
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。