Gitlab+Jenkins学习之路(七)之发布PHP项目

时间:2023-12-12 11:09:44
  • 使用git+jenkins实现持续集成

Gitlab+Jenkins学习之路(七)之发布PHP项目

Step1:构建一个*风格的php-deploy

Gitlab+Jenkins学习之路(七)之发布PHP项目

Step2:Gernal配置,丢弃旧的构建,防止jenkins构建较多之后变臃肿

Gitlab+Jenkins学习之路(七)之发布PHP项目

Step3:源码管理:这里使用git插件,也可以使用svn插件

Gitlab+Jenkins学习之路(七)之发布PHP项目

将git路径存在这里还需要权限认证,否则会出现error 。
jenkins服务器上生成SSH-Key

[root@linux-node2 ~]# yum install -y git
[root@linux-node2 ~]# 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:00FQjn5P6qAtok4BuE42TyfuNJnblzZ389XwKBhmHjY root@linux-node2
The key's randomart image is:
+---[RSA ]----+
| .oo |
|. + |
|o . o |
| o . . . |
|.+.o . S E . . |
|+ =.= B O +.|
| ..B o + o ..o|
| .o = .B + o .. |
| .o+ o+.+ o o. |
+----[SHA256]-----+
[root@linux-node2 ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+/XyUMNP5Wm63hhcACosVyRtmJxTSnT46HSKZ2PhduTHPIJcfhVTmeCk1zwO0fP8PmwXFf0+G9ki8CupE/+xcOy14TCqxNgvP8o514IjXOFV7SO1lnHKH3t0RfaFEloRZHnrgIcWiZrFvYRT/TFznWO86sfHPzlW6WF9elTqRURNR60bwv9C6iw4PpYjGVIST/SeGl75axtUjJr544jB5pEWtNZ09ktCKpPeFBvgCc9exdzOQuzaCXQrroNU0p6MB6KkoIOmpkN92cDPdg4EgBYDjwcl38FbXOq3N0QL8PT+IZpHBWFVEAZU2alI02H5NNUy9p2K/Br4NY3UvYZiP root@linux-node2

Gitlab+Jenkins学习之路(七)之发布PHP项目

Gitlab+Jenkins学习之路(七)之发布PHP项目

命令行测试添加ssh-key后能否正常拉取
[root@linux-node2 ~]# git clone git@192.168.56.11:java/app1.git
Cloning into 'app1'...
The authenticity of host '192.168.56.11 (192.168.56.11)' can't be established.
ECDSA key fingerprint is SHA256:p2lhKmsPQ6K+dWHHvbJg0GV+Ni9VM7vlViKrYsZLP1s.
ECDSA key fingerprint is MD5:::1c::de::1c:4a:2f::b1:dc:e2:d0::.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.11' (ECDSA) to the list of known hosts.
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta )
Receiving objects: % (/), done.
[root@linux-node2 ~]# ll
total
-rw-------. root root Dec : anaconda-ks.cfg
drwxr-xr-x root root Dec : app1
-rw-r--r-- root root Mar jenkins-2.32.-1.1.noarch.rpm

Gitlab+Jenkins学习之路(七)之发布PHP项目

然后保存,立即构建一次,会拉取master的代码库

Gitlab+Jenkins学习之路(七)之发布PHP项目

点开Gitlab+Jenkins学习之路(七)之发布PHP项目可以看到控制台的输出,如图:

Gitlab+Jenkins学习之路(七)之发布PHP项目

/var/lib/jenkins/workspace/php-deploy目录用于存放代码信息

[root@linux-node2 php-deploy]# ll
total
-rw-r--r-- root root Dec : index.html
-rw-r--r-- root root Dec : new.html
-rw-r--r-- root root Dec : readme
[root@linux-node2 php-deploy]# pwd
/var/lib/jenkins/workspace/php-deploy

这里我们拉取的代码需要推送到远程代码目录,需要如下操作:
架设linux-node1为web服务,目录为/data/www/php-deploy

[root@linux-node1 ~]# mkdir /data/www/php-deploy -p
添加node2的免秘钥登陆
[root@linux-node1 ~]# vim .ssh/authorized_keys
[root@linux-node1 ~]# chmod .ssh/authorized_keys
[root@linux-node2 ~]# ssh 192.168.56.11
Last login: Mon Dec :: from 192.168.56.1
[root@linux-node1 ~]#

修改项目设置,配置构建:Execute shell,相当于将代码自动发布到linux-node1的代码目录中。

Gitlab+Jenkins学习之路(七)之发布PHP项目

接下来,我们重新构建一次,并查看控制台输出:

Gitlab+Jenkins学习之路(七)之发布PHP项目

在node1上查看目录,也会有相应的代码信息

[root@linux-node1 ~]# ll /data/www/php-deploy/
total
-rw-r--r-- root root Dec : index.html
-rw-r--r-- root root Dec : new.html
-rw-r--r-- root root Dec : readme
[root@linux-node1 ~]#