Jenkins 部署发布php项目

根据 Linux Git 更新至最新版本(yum) 这篇文章可以将git更新至想要的版本

前置环境 部署相关可以参考这篇文章 https://www2.jianshu.com/p/7be08071f2ca

1.创建git仓库服务

机器:192.168.137.10
新建git用户,密码暂定为git,并创建仓库

[root@localhost project]# git -v
git version 2.41.0
[root@localhost git_repo]# ip a | grep "th0"
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 192.168.137.10/24 brd 192.168.137.255 scope global noprefixroute eth0
[root@localhost project]# id git
id: git: no such user
[root@localhost project]# useradd git
[root@localhost project]# passwd git
更改用户 git 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# mkdir -p /data/git_repo
[root@localhost ~]# cd /data/git_repo/
[root@localhost git_repo]# git init --bare php_test.git
提示:使用 'master' 作为初始分支的名称。这个默认分支名称可能会更改。要在新仓库中
提示:配置使用初始分支名,并消除这条警告,请执行:
提示:
提示: git config --global init.defaultBranch <名称>
提示:
提示:除了 'master' 之外,通常选定的名字有 'main'、'trunk' 和 'development'。
提示:可以通过以下命令重命名刚创建的分支:
提示:
提示: git branch -m <name>
已初始化空的 Git 仓库于 /data/project/php_test.git/
[root@localhost git_repo]# chown -R git:git php_test.git/

机器:192.168.137.13
当作git客户端,clone代码测试

[root@localhost ~]# git -v
git version 2.41.0
[root@localhost ~]# ip a | grep "th0"
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 192.168.137.13/24 brd 192.168.137.255 scope global noprefixroute eth0
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WxSPBrSEqJ/HTSIcrrJsm1cS1AvxUG9BudZfC8BMDe8 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|   o+o.=Oo+      |
|   o=.oo.*.=     |
|  .+.o.o+ =..    |
|  ..+.oo.+.. .   |
|   o.+.+S oEo .  |
|. ..o.o .o . .   |
|.o  o.  .        |
|.o..             |
|.oo              |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id git@192.168.137.10
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.137.10 (192.168.137.10)' can't be established.
ECDSA key fingerprint is SHA256:2UBXEcp9pfytUIIeh5dpZk7ksw/sQaXjPnOKAbvezvs.
ECDSA key fingerprint is MD5:a9:ce:96:0d:5d:e8:e6:de:52:51:c4:6b:fb:8e:b9:5e.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
git@192.168.137.10's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'git@192.168.137.10'"
and check to make sure that only the key(s) you wanted were added.

[root@localhost project]# cat /root/.ssh/known_hosts 
192.168.137.10 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLUzHamX9PLgF+uwENrWIOhDbsv8Zky1GfX5Qbm8XR7Ae+VzG5aZLdHgb3+AwZpbHlAry5cNz9Pas0kvlmuir44=
[root@localhost ~]# git config --global user.email "you@example.com"
[root@localhost ~]# git config --global user.name "Your Name"
[root@localhost ~]# mkdir -p /data/project
[root@localhost ~]# cd /data/project/
[root@localhost project]# git clone git@192.168.137.10:/data/git_repo/php_test.git
正克隆到 'php_test'...
警告:您似乎克隆了一个空仓库。
[root@localhost project]# git clone git@192.168.137.10:/data/git_repo/php_test.git php_test1
正克隆到 'php_test1'...
警告:您似乎克隆了一个空仓库。
[root@localhost project]# cd php_test1/
[root@localhost php_test1]# touch test.php
[root@localhost php_test1]# git add -A
[root@localhost php_test1]# git commit -am "first commit"
[master(根提交) 1556015] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.php
[root@localhost php_test1]# git push origin master
枚举对象中: 3, 完成.
对象计数中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 208 字节 | 208.00 KiB/s, 完成.
总共 3(差异 0),复用 0(差异 0),包复用 0
To 192.168.137.10:/data/git_repo/php_test.git
 * [new branch]      master -> master
[root@localhost php_test1]# cd ../php_test
[root@localhost php_test]# git pull
remote: 枚举对象中: 3, 完成.
remote: 对象计数中: 100% (3/3), 完成.
remote: 总共 3(差异 0),复用 0(差异 0),包复用 0
展开对象中: 100% (3/3), 188 字节 | 188.00 KiB/s, 完成.
来自 192.168.137.10:/data/git_repo/php_test
 * [新分支]          master     -> origin/master
[root@localhost php_test]# ls
test.php
[root@localhost php_test]# cd ..
[root@localhost project]# ll
总用量 0
drwxr-xr-x. 3 root root 34 8月  26 20:10 php_test
drwxr-xr-x. 3 root root 34 8月  26 20:09 php_test1
# 验证结束了,干掉它
[root@localhost project]# rm -rf php_test
[root@localhost project]# rm -rf php_test1/

2.jenkins新建php_test 的 items

ip:192.168.137.12
先安装 Git plugin、Publish Over SSH 插件
安装完后重启Jenkins服务:systemctl restart jenkins

在Jenkins服务器生成秘钥,将公钥传到git server的机器

这个是jenkins web 执行ssh 远程执行命令用的

[root@localhost soft]# ip a | grep "th0"
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 192.168.137.12/24 brd 192.168.137.255 scope global noprefixroute eth0
[root@localhost soft]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cEXXnmja2j8q18RwBlRI5YzVQ1ZTsja86jidfJZpCP0 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|         .oo+=*==|
|         . .o*o+o|
|      . .   .+B..|
|       o    +.=o |
|        S  = =.  |
|          o o.o  |
|           *.* o |
|          +oB E  |
|          .+o*.. |
+----[SHA256]-----+
[root@localhost soft]# ssh-copy-id root@192.168.137.13
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.137.13 (192.168.137.13)' can't be established.
ECDSA key fingerprint is SHA256:cWPcosZgoMKUOou0J1OyxzcDr5WU5mpZ62+WITAOm2k.
ECDSA key fingerprint is MD5:d7:a3:08:c8:2f:6f:5b:76:7d:f7:a1:01:6e:0f:ec:2e.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.137.13's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.137.13'"
and check to make sure that only the key(s) you wanted were added.

[root@localhost soft]# cat /root/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA+u3kRcdaXbxseBZQTHROZkuQBqAVUtUIrXFxXZe5BNt7qtu5
8iT8bhez4d+gK9k3Kh+31E+9xyKVJMuPbJ9viZSBnY+5pjsCw8Dr8ymXXByeJ/tz
tWjXy6W9AsbHEhm7b4pajAaQYxvUMs6DbT9oJKMHzMfiRF8qol0Fr/yJyaz2zlTR
HBeLktDgwp6/CnHsApwlOlAA4brb3MqwXTqPBMZ3l2z4byZCgp17MliDz+MClURu
ghfDg6ZuQ+IMZ4zxIJlAJNNkZ4BwWFEk5H8dPUXAa8au5rOwOpuBiP0DVieFWeSG
t752SKySyscEBRLrpy/MADQb4uHXSPP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O
qZiyVdkCgYEAisrK0U16l+19y8PcGZtux6m8jaOk48pQJ7laR3uguCZlbqZhmpkS
gmXbwmcFmOsi45m2tBLtEX3sURyCxuwzZbFE5xNFfEIBSPM+T8USmqIskM8dJbAf
n/+62IDbCiFEi0eMjRH0qR7BtlU2I89DsrhUTkjbNZmhjlXTsw/V3FM=
-----END RSA PRIVATE KEY-----

在Jenkins页面点击manage Jenkins—>configure system,拉到最下面在key栏填写Jenkins服务器私钥,新增要发布代码的服务器name、ip、用户、目录:



填写完成后点击test configuration按钮,如果显示success,表示配置成功,点击应用即可,如果需要发布代码到多台机器,这里就需要添加多台机器的信息。

在Jenkins首页新建任务:


在源码管理模块中填写对应的仓库地址:

私有仓库显示不可访问,Jenkins服务器(192.168.137.12)切jenkins用户生成密钥数据,再把生成的公钥放到git仓库服务器的/home/git/.ssh/authorized_keys文件里

[root@localhost soft]# su -s /bin/bash jenkins
bash-4.2$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:GtIObWdKc1LdcYAwoFqpilhAdpREG2LoExt2zHe0gjE jenkins@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|.=*E. ooo. .o..  |
|=+o=*+ ..o.. o   |
|+ =o=.... . .    |
| = + o..         |
|  = o O S        |
|oo   * X         |
|+     +          |
|                 |
|                 |
+----[SHA256]-----+
bash-4.2$ more /var/lib/jenkins/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDrnF500ZGtdo2WhsQZop+T0NpO983Gm37QUBBZLIXd+MbkXasCeGL7a2e1Qu6WbLEFaRMQgsOqKWLxI3OUxRnx5OWcoWex928ak6shGfbn+hytf+q/cFM0sofmQ6QFfyuKEabs4uCI2pv60szuonNFqhtmKBtqrq+XII3fdqVWtzEgh6eH7ZfWye1u1Xgq/hBCWA5N6sVwRTr/U9QtGlrS1E/xsz0m7jLTRvYXD8
Z9zIP2va3c0VRJyPiK9gy3hnXoAR6wWXIKqzK3ekHRmSJYgwkTkvMdQDG8dRzaqhfPl3yVLtkTRPirIxGCZxI1/P9y39lRCGkr1bzPkLSLTpfb jenkins@localhost.localdomain
bash-4.2$ exit
exit
[root@localhost soft]#

git仓库服务器上面操作

[root@localhost git_repo]# vim /etc/ssh/sshd_config
# vim sshd_config修改 PubkeyAuthentication 配置为yes
[root@localhost git_repo]# grep "PubkeyAuthentication" /etc/ssh/sshd_config 
#PubkeyAuthentication yes
PubkeyAuthentication yes
[root@localhost git_repo]# vim /home/git/.ssh/authorized_keys
# vim authorized_keys添加 jenkins 用户的公钥
[root@localhost git_repo]# cat /home/git/.ssh/authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDrnF500ZGtdo2WhsQZop+T0NpO983Gm37QUBBZLIXd+MbkXasCeGL7a2e1Qu6WbLEFaRMQgsOqKWLxI3OUxRnx5OWcoWex928ak6shGfbn+hytf+q/cFM0sofmQ6QFfyuKEabs4uCI2pv60szuonNFqhtmKBtqrq+XII3fdqVWtzEgh6eH7ZfWye1u1Xgq/hBCWA5N6sVwRTr/U9QtGlrS1E/xsz0m7jLTRvYXD8Z9zIP2va3c0VRJyPiK9gy3hnXoAR6wWXIKqzK3ekHRmSJYgwkTkvMdQDG8dRzaqhfPl3yVLtkTRPirIxGCZxI1/P9y39lRCGkr1bzPkLSLTpfb jenkins@localhost.localdomain

再次查看就可以发现正常了


配置Send files or execute commands over SSH

**/**表示全部文件(拉取下来的文件哪些被发布到指定机器),
Remove prefix可以指定截掉的前缀目录,这里留空即可,
Remote directory指定远程服务器上代码存放路径,Exec command为文件传输完成后要执行的命令,比如可以是更改文件权限的命令,
设置完成后点击 “Add Transfer Set”,如果还有另外的机器,可以点击 “Add Server”重复以上操作

选择Send files or execute commands over SSH:


在Jenkins主页点击该项目后点击立即构建(build now)即可将git仓库的代码发布到指定服务器:


在输出信息中显示success则表示发布成功:


在应用服务器机器上查看:

[root@localhost project]# ip a | grep "th0"
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 192.168.137.13/24 brd 192.168.137.255 scope global noprefixroute eth0
[root@localhost project]# pwd
/data/project
[root@localhost project]# ll
总用量 0
-rw-r--r--. 1 nobody root 0 8月  26 23:47 test.php
[root@localhost project]# cat test.php 
[root@localhost project]#

再次去其他机器修改代码仓库内容,验证结果:
这是从未拉过仓库代码的机器,拉去代码修改文件并提交

[root@localhost soft]# git clone git@192.168.137.10:/data/git_repo/php_test.git
正克隆到 'php_test'...
git@192.168.137.10's password: 
remote: 枚举对象中: 3, 完成.
remote: 对象计数中: 100% (3/3), 完成.
remote: 总共 3(差异 0),复用 0(差异 0),包复用 0
接收对象中: 100% (3/3), 完成.
[root@localhost soft]# cd php_test/
[root@localhost php_test]# ll
总用量 0
-rw-r--r--. 1 root root 0 8月  27 00:11 test.php
[root@localhost php_test]# echo "123" > test.php 
[root@localhost php_test]# cat test.php 
123
[root@localhost php_test]# git commit -am "sencond commit"
[master ce641c5] sencond commit
 1 file changed, 1 insertion(+)
[root@localhost php_test]# git push origin master
git@192.168.137.10's password: 
枚举对象中: 5, 完成.
对象计数中: 100% (5/5), 完成.
写入对象中: 100% (3/3), 236 字节 | 236.00 KiB/s, 完成.
总共 3(差异 0),复用 0(差异 0),包复用 0
To 192.168.137.10:/data/git_repo/php_test.git
   1556015..ce641c5  master -> master

jenkins web 页面重新构建


在应用服务器机器上查看:

[root@localhost project]# ip a | grep "th0"
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 192.168.137.13/24 brd 192.168.137.255 scope global noprefixroute eth0
[root@localhost project]# ll
总用量 4
-rw-r--r--. 1 nobody root 4 8月  27 00:14 test.php
[root@localhost project]# cat test.php 
123
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 223,726评论 6 521
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 95,697评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 170,734评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,508评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,522评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,051评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,429评论 3 427
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,403评论 0 278
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,930评论 1 323
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,977评论 3 343
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,122评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,763评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,454评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,931评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,047评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,613评论 3 380
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,150评论 2 363

推荐阅读更多精彩内容