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

简介:

1 发布php代码

- jenkins已经搭建完成,现在开始来做一个php发布代码的任务

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

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

- 需要生成一对密钥对用来登录远程机器(远程机器即是发布php代码的机器)

1
2
3
4
5
[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
// 我之前有生成过密钥对在这我可以拿来直接用,你也可以根据自己的需求来重新生成

- 之后在设置jenkins

1
2
3
4
系统管理-系统设置-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?imageslim)

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

如果是多台web server,继续点击“增加”,重复以上操作

- 创建新的任务

722c81001d7cf47f08153d4e7b08d301.png-wh_

8f55534c9977d170c6a1ffd832229cf2.png-wh_

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

“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_

- 查看发布的结果

1
2
3
4
[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(快速搭建)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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代码上传到我们的私有仓库里

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
[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
  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私有仓库查看是否上传

1
2
[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_

-最后就开始构建 

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

1
2
3
4
5
[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

- 这里需要再次注意下,如果是自己搭建的私有仓库一定要记得每台机器上要有相应的密钥。切记切记。  

当然后期只要是构建成功还需要发一封邮件提醒,来提升工作效率!!





     本文转自我不是瘦子51CTO博客,原文链接:http://blog.51cto.com/chy940405/1984173,如需转载请自行联系原作者


相关文章
|
16天前
|
开发工具 git
git篇3:idea中创建项目并提交到远程Git仓库
git篇3:idea中创建项目并提交到远程Git仓库
|
1月前
|
开发工具 git C++
【git 实用指南】git下载、拉取 代码
【git 实用指南】git下载、拉取 代码
171 2
|
2月前
|
存储 前端开发 开发工具
Git Hooks实战:提交前检查修改文件中是否包含调试代码
Git Hooks实战:提交前检查修改文件中是否包含调试代码
47 0
|
1月前
|
开发工具 git
【git 实用指南】git 上传代码
【git 实用指南】git 上传代码
33 2
|
1月前
|
开发工具 git
Git -- 代码上传错误 error: failed to push some refs to ‘git@gitee.com:JMFive/uni-shop2.git‘
Git -- 代码上传错误 error: failed to push some refs to ‘git@gitee.com:JMFive/uni-shop2.git‘
|
5天前
|
开发工具 git
git 拉取代码仓库代码报错(合并错误 refusing to merge unrelated histories)
git 拉取代码仓库代码报错(合并错误 refusing to merge unrelated histories)
15 0
|
9天前
|
数据可视化 开发工具 git
Git代码版本管理入门
Git代码版本管理入门
|
27天前
|
开发工具 C语言 数据安全/隐私保护
git提交代码到远端仓库的方法详解
git提交代码到远端仓库的方法详解
|
29天前
|
算法 开发工具 git
【git 实用指南】git 增加 本地代码 git add 相关命令和复杂情况需求
【git 实用指南】git 增加 本地代码 git add 相关命令和复杂情况需求
92 0
|
1月前
|
Shell Linux 开发工具
【Shell 命令集合 系统管理 】Linux 查看当前Git仓库的提交历史 gitps命令 使用指南
【Shell 命令集合 系统管理 】Linux 查看当前Git仓库的提交历史 gitps命令 使用指南
16 0