根据 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/MADQb4uHXSPPY+nrowwIDAQABAoIBAQDJZVXpi29D/I8y
cvZP5qlmAQ+/wYxbxxAH5R0P+U5OT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gmXbwmcFmOsi45m2tBLtEX3sURyCxuwzZbFE5xNFfEIBSPM+T8USmqIskM8dJbAf
n/+62IDbCiFEi0eMjRH0qR7BtlU2I89DsrhUTkjbNZmhjlXTsw/V3FM=
-----END RSA PRIVATE KEY-----
在Jenkins页面点击manage Jenkins—>configure system,拉到最下面在key栏填写Jenkins服务器私钥,新增要发布代码的服务器name、ip、用户、目录:
![](https://img.haomeiwen.com/i16255509/d56befbd5ff95c4c.png)
![](https://img.haomeiwen.com/i16255509/76398c57d00c3bb1.png)
填写完成后点击test configuration按钮,如果显示success,表示配置成功,点击应用即可,如果需要发布代码到多台机器,这里就需要添加多台机器的信息。
在Jenkins首页新建任务:
![](https://img.haomeiwen.com/i16255509/edc12ab1968b7383.png)
在源码管理模块中填写对应的仓库地址:
![](https://img.haomeiwen.com/i16255509/6a87eb5b4ba5fe95.png)
私有仓库显示不可访问,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
再次查看就可以发现正常了
![](https://img.haomeiwen.com/i16255509/6c8acc3cd6222c8b.png)
配置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:
![](https://img.haomeiwen.com/i16255509/6b01c31b8a7dc166.png)
在Jenkins主页点击该项目后点击立即构建(build now)即可将git仓库的代码发布到指定服务器:
![](https://img.haomeiwen.com/i16255509/eb416bd8705f2a2b.png)
在输出信息中显示success则表示发布成功:
![](https://img.haomeiwen.com/i16255509/771c958d5d9c174e.png)
在应用服务器机器上查看:
[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 页面重新构建
![](https://img.haomeiwen.com/i16255509/38eb19355d80c6a9.png)
在应用服务器机器上查看:
[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
网友评论