jenkins git公有仓库与私有仓库发布代码

1 发布php代码php

- jenkins已经搭建完成,如今开始来作一个php发布代码的任务html

- 在发布php代码时先看看是否有两个插件git

在系统管理-管理插件- 已安装插件-
检查是否有“Git plugin”和“Publish Over SSH”两个插件,若是没有,则需点击“可选插件”,找到它并安装
[root@chy01 jenkins]# systemctl restart jenkins //重启服务

- 须要生成一对密钥对用来登陆远程机器(远程机器便是发布php代码的机器)web

[root@chy01 ~]# cd /root/.ssh/
[root@chy01 .ssh]# ls
aaa      authorized_keys  boy.pub  id_rsa      jump      known_hosts  lf1.pub
aaa.pub  boy              config   id_rsa.pub  jump.pub  lf1
//我以前有生成过密钥对在这我能够拿来直接用,你也能够根据本身的需求来从新生成

- 以后在设置jenkinsshell

系统管理-系统设置-Publish over SSH 找到这一项,添加以前生成的密钥对的私钥便可
以后在将公钥放在想要发布php代码的机器(简称远程机器)我这边放在了[root@chy ~]# cat .ssh/authorized_keys
这个机器上面
[root@chy01 ~]# ssh 192.168.212.10  测试能够登陆到远程的机器


![mark](http://oqz6pu8vi.bkt.clouddn.com/blog/20171120/163348636.png?p_w_picpathslim)vim

SSH Server,name自定义,Hostname填写线上web服务器的ip,Username填写root,Remote Directory填写/(根)  centos

若是是多台web server,继续点击“增长”,重复以上操做bash

- 建立新的任务服务器

722c81001d7cf47f08153d4e7b08d301.png-wh_

8f55534c9977d170c6a1ffd832229cf2.png-wh_

Repository URL”填写你项目的git地址,若是是公共项目能够不设置下面的参数,直接填写一个git地址便可,如果私有项目(-none),须要填写认证信息,好比能够选择 “SSH Username whith private key”,而后设置Username以及private key   app

“Branches to build” 默认为*/master,意思是发布的分支为master,保持默认

eb0866031c75800370c37e0280bc46dd.png-wh_

“构建触发器”和“构建环境”留空

- “构建”,选择 “Send files or execute commands over SSH” 

e05726844c8277295bed48d317a4f5b3.png-wh_

Remove prefix能够指定截掉的前缀目录,这里留空便可,Remote directory指定远程服务器上代码存放路径,好比/data/wwwroot/www.aaa.com,Exec command为文件传输完成后要执行的命令,好比能够是更改文件权限的命令,设置完成后点击 “Add Transfer Set”,若是还有另外的机器,能够点击 “Add Server”重复以上操做

- 开始发布代码

1d0ce9e85f96ecba5329b3fc501dd8a5.png-wh_

- 查看发布的结果

[root@chy ~]# ls /tmp/jenkins_test/
D11Z  D13Z  D15Z  D18Z  D20Z  D22Z     README.md
D12Z  D14Z  D17Z  D19Z  D21Z  LICENSE 
//查看已经发布成功

这里须要注意下若是是在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(快速搭建)

[root@chy02 ~]# yum install -y git 
[root@chy02 ~]# useradd -s /usr/bin/git-shell git 
[root@chy02 ~]# cd /home/git/
[root@chy02 git]# mkdir .ssh
[root@chy02 git]# touch .ssh/authorized_keys
[root@chy02 git]# chmod 600 .ssh/authorized_keys 
[root@chy02 git]# chown -R git:git .ssh
[root@chy02 git]# vi .ssh/authorized_keys 
放入公钥,保证客户机能够访问咱们的git私有仓库的服务端
[root@chy01 ~]# ssh git@192.168.212.12
# cat /etc/motd 
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.212.12 closed.

到git的客户机里开始将discuz代码上传到咱们的私有仓库里

[root@chy01 ~]# cd /tmp
You have new mail in /var/spool/mail/root
[root@chy01 tmp]# git clone git@192.168.212.12:/data/gitroot/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
[root@chy01 tmp]# cd sample/
[root@chy01 sample]# ls -la
total 12
drwxr-xr-x   3 root root 4096 Nov 22 00:14 .
drwxrwxrwt. 15 root root 4096 Nov 22 00:14 ..
drwxr-xr-x   7 root root 4096 Nov 22 00:14 .git
[root@chy01 sample]# vim discuzz.html
[root@chy01 sample]# git add discz.html 
[root@chy01 sample]# git commit -m "daima"
[master d22de14] daima
 1 file changed, 470 insertions(+)
 create mode 100644 discz.html
[root@chy01 sample]# git push
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 252 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To git@192.168.212.12:/data/gitroot/sample.git
   a2ee2f6..d22de14  master -> master
如上是上传discuz的代码

git私有仓库查看是否上传

[root@chy02 sample.git]# git log --pretty=oneline
b081942e4a9eed04ffe34e7b2a6796270f1fcc69 discuzz 代码

如今须要在jenkins里下发discuz的代码

- 以下是新建一个任务

084d68c29b88580e625595264363e569.png-wh_

- 建立git私有仓库的信息

ca6b7ffc24bbd56674bd907c480110e6.png-wh_

09cd2ddf4d2094ea2dadc462e0aac8dd.png-wh_

- 以下的就与以前的公有仓库的操做是一致的,这里就不细说了

3c22e86b29810e7561d8a896201f62d2.png-wh_

-最后就开始构建 

- 在客户机上查看是否有构建的任务

[root@chy ~]# cd /tmp/jenkins_test/
[root@chy jenkins_test]# ls
1.txt  D12Z  D14Z  D17Z  D19Z  D21Z  discuzz.html  LICENSE    习题答案.txt
D11Z   D13Z  D15Z  D18Z  D20Z  D22Z  discz.html    README.md
查看到已有discuzz

- 这里须要再次注意下,若是是本身搭建的私有仓库必定要记得每台机器上要有相应的密钥。切记切记。  

固然后期只要是构建成功还须要发一封邮件提醒,来提高工做效率!!

相关文章
相关标签/搜索