1 发布php代码
- jenkins已经搭建完成,现在开始来做一个php发布代码的任务
- 在发布php代码时先看看是否有两个插件
<code>在系统管理-管理插件- 已安装插件-</code>
<code>检查是否有“Git plugin”和“Publish Over SSH”两个插件,如果没有,则需点击“可选插件”,找到它并安装</code>
<code>[root@chy01 jenkins]</code><code># systemctl restart jenkins //重启服务</code>
- 需要生成一对密钥对用来登录远程机器(远程机器即是发布php代码的机器)
<code>[root@chy01 ~]</code><code># cd /root/.ssh/</code>
<code>[root@chy01 .</code><code>ssh</code><code>]</code><code># ls</code>
<code>aaa authorized_keys boy.pub id_rsa jump known_hosts lf1.pub</code>
<code>aaa.pub boy config id_rsa.pub jump.pub lf1</code>
<code>//</code><code>我之前有生成过密钥对在这我可以拿来直接用,你也可以根据自己的需求来重新生成</code>
- 之后在设置jenkins
<code>系统管理-系统设置-Publish over SSH 找到这一项,添加之前生成的密钥对的私钥即可</code>
<code>之后在将公钥放在想要发布php代码的机器(简称远程机器)我这边放在了[root@chy ~]</code><code># cat .ssh/authorized_keys</code>
<code>这个机器上面</code>
<code>[root@chy01 ~]</code><code># ssh 192.168.212.10 测试可以登录到远程的机器</code>

SSH Server,name自定义,Hostname填写线上web服务器的ip,Username填写root,Remote Directory填写/(根)
如果是多台web server,继续点击“增加”,重复以上操作
- 创建新的任务
<a href="https://s3.51cto.com/oss/201711/22/722c81001d7cf47f08153d4e7b08d301.png-wh_500x0-wm_3-wmp_4-s_4107307767.png" target="_blank"></a>
<a href="https://s2.51cto.com/oss/201711/22/8f55534c9977d170c6a1ffd832229cf2.png-wh_500x0-wm_3-wmp_4-s_3405987822.png" target="_blank"></a>
Repository URL”填写你项目的git地址,如果是公共项目可以不设置下面的参数,直接填写一个git地址即可,若是私有项目(-none),需要填写认证信息,比如可以选择 “SSH Username whith private key”,然后设置Username以及private key
“Branches to build” 默认为*/master,意思是发布的分支为master,保持默认
<a href="https://s3.51cto.com/oss/201711/22/eb0866031c75800370c37e0280bc46dd.png-wh_500x0-wm_3-wmp_4-s_1179492456.png" target="_blank"></a>
“构建触发器”和“构建环境”留空
- “构建”,选择 “Send files or execute commands over SSH”
<a href="https://s1.51cto.com/oss/201711/22/e05726844c8277295bed48d317a4f5b3.png-wh_500x0-wm_3-wmp_4-s_2869468305.png" target="_blank"></a>
Remove prefix可以指定截掉的前缀目录,这里留空即可,Remote directory指定远程服务器上代码存放路径,比如/data/wwwroot/www.aaa.com,Exec command为文件传输完成后要执行的命令,比如可以是更改文件权限的命令,设置完成后点击 “Add Transfer Set”,如果还有另外的机器,可以点击 “Add Server”重复以上操作
- 开始发布代码
<a href="https://s5.51cto.com/oss/201711/22/1d0ce9e85f96ecba5329b3fc501dd8a5.png-wh_500x0-wm_3-wmp_4-s_2890522697.png" target="_blank"></a>
- 查看发布的结果
<code>[root@chy ~]</code><code># ls /tmp/jenkins_test/</code>
<code>D11Z D13Z D15Z D18Z D20Z D22Z README.md</code>
<code>D12Z D14Z D17Z D19Z D21Z LICENSE </code>
<code>//</code><code>查看已经发布成功</code>
这里需要注意下如果是在git上重新更新了一下文件只需要在点下“立即构建”就会更新在git上更新的项目的内容
(如上我们用的是git的公共的仓库,可是我有一个私有的仓库应该怎么做呢?
- 需求:需要将discuz的代码发布到客户机上(chy这台机器上)。
在这我们有三台机器:
chy(192.168.212.10)--这台是最后的客户机查看discuz发布代码用的机器
chy02(192.168.212.12)--这台是搭建的git的私有仓库
chy01(192.168.212.11)--这台是要将discuz代码上传到私有仓库的一台机器,我这边需要上传到私有仓库然后jenkins用私有仓库的地址发布
如上的三台机器都是centos7
git的私有仓库192.168.212.12 chy02(快速搭建)
<code>[root@chy02 ~]</code><code># yum install -y git </code>
<code>[root@chy02 ~]</code><code># useradd -s /usr/bin/git-shell git </code>
<code>[root@chy02 ~]</code><code># cd /home/git/</code>
<code>[root@chy02 git]</code><code># mkdir .ssh</code>
<code>[root@chy02 git]</code><code># touch .ssh/authorized_keys</code>
<code>[root@chy02 git]</code><code># chmod 600 .ssh/authorized_keys </code>
<code>[root@chy02 git]</code><code># chown -R git:git .ssh</code>
<code>[root@chy02 git]</code><code># vi .ssh/authorized_keys </code>
<code>放入公钥,保证客户机可以访问我们的git私有仓库的服务端</code>
<code>[root@chy01 ~]</code><code># ssh [email protected]</code>
<code># cat /etc/motd </code>
<code>fatal: Interactive git shell is not enabled.</code>
<code>hint: ~</code><code>/git-shell-commands</code> <code>should exist and have </code><code>read</code> <code>and execute access.</code>
<code>Connection to 192.168.212.12 closed.</code>
到git的客户机里开始将discuz代码上传到我们的私有仓库里
<code>[root@chy01 ~]</code><code># cd /tmp</code>
<code>You have new mail </code><code>in</code> <code>/var/spool/mail/root</code>
<code>[root@chy01 tmp]</code><code># git clone [email protected]:/data/gitroot/sample.git</code>
<code>Cloning into </code><code>'sample'</code><code>...</code>
<code>warning: You appear to have cloned an empty repository.</code>
<code>[root@chy01 tmp]</code><code># cd sample/</code>
<code>[root@chy01 sample]</code><code># ls -la</code>
<code>total 12</code>
<code>drwxr-xr-x 3 root root 4096 Nov 22 00:14 .</code>
<code>drwxrwxrwt. 15 root root 4096 Nov 22 00:14 ..</code>
<code>drwxr-xr-x 7 root root 4096 Nov 22 00:14 .git</code>
<code>[root@chy01 sample]</code><code># vim discuzz.html</code>
<code>[root@chy01 sample]</code><code># git add discz.html </code>
<code>[root@chy01 sample]</code><code># git commit -m "daima"</code>
<code>[master d22de14] daima</code>
<code> </code><code>1 </code><code>file</code> <code>changed, 470 insertions(+)</code>
<code> </code><code>create mode 100644 discz.html</code>
<code>[root@chy01 sample]</code><code># git push</code>
<code>Counting objects: 3, </code><code>done</code><code>.</code>
<code>Compressing objects: 100% (2</code><code>/2</code><code>), </code><code>done</code><code>.</code>
<code>Writing objects: 100% (2</code><code>/2</code><code>), 252 bytes | 0 bytes</code><code>/s</code><code>, </code><code>done</code><code>.</code>
<code>Total 2 (delta 0), reused 0 (delta 0)</code>
<code>To [email protected]:</code><code>/data/gitroot/sample</code><code>.git</code>
<code> </code><code>a2ee2f6..d22de14 master -> master</code>
<code>如上是上传discuz的代码</code>
git私有仓库查看是否上传
<code>[root@chy02 sample.git]</code><code># git log --pretty=oneline</code>
<code>b081942e4a9eed04ffe34e7b2a6796270f1fcc69 discuzz 代码</code>
现在需要在jenkins里下发discuz的代码
- 如下是新建一个任务
<a href="https://s4.51cto.com/oss/201711/22/084d68c29b88580e625595264363e569.png-wh_500x0-wm_3-wmp_4-s_1608679932.png" target="_blank"></a>
- 创建git私有仓库的信息
<a href="https://s2.51cto.com/oss/201711/22/09cd2ddf4d2094ea2dadc462e0aac8dd.png-wh_500x0-wm_3-wmp_4-s_2689922747.png" target="_blank"></a>
- 如下的就与之前的公有仓库的操作是一致的,这里就不细说了
<a href="https://s3.51cto.com/oss/201711/22/3c22e86b29810e7561d8a896201f62d2.png-wh_500x0-wm_3-wmp_4-s_2246334591.png" target="_blank"></a>
-最后就开始构建
- 在客户机上查看是否有构建的任务
<code>[root@chy ~]</code><code># cd /tmp/jenkins_test/</code>
<code>[root@chy jenkins_test]</code><code># ls</code>
<code>1.txt D12Z D14Z D17Z D19Z D21Z discuzz.html LICENSE 习题答案.txt</code>
<code>D11Z D13Z D15Z D18Z D20Z D22Z discz.html README.md</code>
<code>查看到已有discuzz</code>
- 这里需要再次注意下,如果是自己搭建的私有仓库一定要记得每台机器上要有相应的密钥。切记切记。
当然后期只要是构建成功还需要发一封邮件提醒,来提升工作效率!!
本文转自我不是瘦子51CTO博客,原文链接:http://blog.51cto.com/chy940405/1984173,如需转载请自行联系原作者