Jenkins学习笔记5

bash 复制代码
[root@localhost ~]# cd  /var/lib/jenkins/workspace/nginx_root_sync
[root@localhost nginx_root_sync]# ll
total 16
-rw-r--r-- 1 jenkins jenkins  6 Sep 20 20:57 1.php
-rw-r--r-- 1 jenkins jenkins  5 Sep 20 20:57 2.php
-rw-r--r-- 1 jenkins jenkins  5 Sep 20 21:17 3.php
-rw-r--r-- 1 jenkins jenkins 35 Sep 20 20:57 README.md

这个目录其实就是一个workspace。

自动发布系统:

SSH免密:

开发者要产生ssh空密码密钥对,把公钥添加到gitlab(过程省略)

在开发者进行git clone创建好的项目,进行开发

bash 复制代码
[root@git-developer ~]# git clone git@192.168.17.118:root/auto_build_web.git
Cloning into 'auto_build_web'...
The authenticity of host '192.168.17.118 (192.168.17.118)' can't be established.
ECDSA key fingerprint is SHA256:4dogA08L5gdfaA69BGekE13kzSuIP2vcRp9rsqYnpn0.
ECDSA key fingerprint is MD5:93:34:b9:61:03:cd:00:92:b9:91:3d:b6:6c:7a:84:95.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.17.118' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@git-developer ~]# ll
total 4
-rw-------. 1 root root 1518 Sep  2 02:39 anaconda-ks.cfg
drwxr-xr-x  3 root root   35 Sep 20 21:41 auto_build_web
drwxr-xr-x  3 root root   74 Sep 20 21:16 jenkins

写好代码并上传:

bash 复制代码
[root@git-developer auto_build_web]# echo 1111 > 1.php
[root@git-developer auto_build_web]# git add *
[root@git-developer auto_build_web]# git commit -m "Added 1.php"
[main e5d9341] Added 1.php
 1 file changed, 1 insertion(+)
 create mode 100644 1.php


[root@git-developer auto_build_web]# git push -u origin main
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 275 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.17.118:root/auto_build_web.git
   e223804..e5d9341  main -> main
Branch main set up to track remote branch main from origin.

Jenkins安装对应的插件:

jenkins安装gitlab和gitlab hook插件:

安装好之后,要重启下jenkins。

Available:可获得的。

Installed:已安装的。

添加Jenkins服务器公钥到gitlab:

第1步: jenkins产生root用户的ssh空密码密钥对(步骤省略)

添加公钥到gitlab:

测试下连通性:

在jenkins-server服务器上,通过git clone方法测试。

在jenkins服务器上克隆仓库,确认连接OK

bash 复制代码
[root@jenkins-server ~]# git clone git@192.168.17.118:root/auto_build_web.git
Cloning into 'auto_build_web'...
The authenticity of host '192.168.17.118 (192.168.17.118)' can't be established.
ECDSA key fingerprint is SHA256:4dogA08L5gdfaA69BGekE13kzSuIP2vcRp9rsqYnpn0.
ECDSA key fingerprint is MD5:93:34:b9:61:03:cd:00:92:b9:91:3d:b6:6c:7a:84:95.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.17.118' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.


[root@jenkins-server ~]# ll
total 4
-rw-------. 1 root root 1518 Sep  2 02:39 anaconda-ks.cfg
drwxr-xr-x  3 root root   48 Sep 20 22:10 auto_build_web
drwxr-xr-x  2 root root   42 Sep 20 11:28 soft


[root@jenkins-server ~]# cd auto_build_web/
[root@jenkins-server auto_build_web]# ll
total 12
-rw-r--r-- 1 root root    5 Sep 20 22:10 1.php
-rw-r--r-- 1 root root 6183 Sep 20 22:10 README.md

Jenkins

这是一个构建任务名称:

然后点击OK。

这个root凭据需要进行下确认。

更改下主分支名称,不是master,是main。

Gitlab知道自己的projects或者仓库发生了变化,通过WebHook告诉jenkins,触发。

生成token令牌。

构建:这次我们不使用Publish over SSH了。我们使用执行Shell。

使用Shell,那说明我们可以使用Shell来发挥了。

bash 复制代码
#!/bin/bash
SOURCE_DIR=/var/lib/jenkins/workspace/$JOB_NAME/
DEST_DIR=/usr/share/nginx/html

/usr/bin/rsync -av --delete $SOURCE_DIR root@192.168.17.121:$DEST_DIR

rsync这个工具还是很强大的,需要学习下。

$JOB_NAME Name of the project of this build。

另外,执行此脚本不再是使用publish over ssh插件,而是使用jenkins服务器上的jenkins用户来执行的。

bash 复制代码
[root@jenkins-server ~]# grep jenkins /etc/passwd
jenkins:x:998:996:Jenkins Automation Server:/var/lib/jenkins:/bin/false


[root@jenkins-server ~]# usermod -s /bin/bash jenkins


[root@jenkins-server ~]# grep jenkins /etc/passwd
jenkins:x:998:996:Jenkins Automation Server:/var/lib/jenkins:/bin/bash

让jenkins具有登录权限。能够使用su jenkins命令进行切换。

问题: 既然是使用jenkins服务器上的jenkins用户来执行,为什么rsync不传给10.1.1.14的jenkins用户,而是传给它的root用户?

答: 因为10.1.1.14上没有jenkins用户,就算有jenkins用户,对/usr/share/nginx/html也没有写权限

我们再看下/usr/share/nginx/html目录的权限:

html的目录的拥有者是root,具有可读可写可执行的权限。

然后再执行命令:

再确认下rsync这个工具有没有安装:

小结:

使用jenkins用户执行rsync脚本同步到nginx的root用户。

再配置Jenkins跟gitlab的对接:

配置gitlab允许本地网络使用webhook:

gitlab默认在本地网络不能使用webhook,所以我们配置允许。(使用管理员配置,普通用户看不到下面的扳手图标。)

这个SSL verification的选项要去掉。

安全带来的问题对运维来说是麻烦。

Urlis blocked: Requests to the local network are not allowed

在配置的过程中遇到这个问题。

那一切就准备就绪。

然后我这边也将几个系统的时间重新进行了同步,保证每台服务器的时间都是相同的。

ntpdate time1.aliyun.com

systemctl restart ntpd

没有出现构建的自动运行,检查发现这个地方没有被勾选。Push events。

看到jenkins的这个地方开始进行构建。

可以看到文件已经同步到了nginx-server服务器上了。

然后我们再看下构建的日志:

相关推荐
醉颜凉12 天前
Elasticsearch高性能优化:Bulk API大规模数据导入性能调优全攻略
elasticsearch·性能优化·jenkins
sbjdhjd13 天前
从零搭建企业级 CI/CD(下):Jenkins+GitLab+Harbor 全链路实战指南
git·servlet·ci/cd·云原生·云计算·gitlab·jenkins
小美元13 天前
【爽之】使用jenkins实现前端一键发版
运维·jenkins
changxiang13 天前
Jenkins备忘
运维·jenkins
未若君雅裁15 天前
日志采集与ELK:从本地日志到集中检索分析
运维·elk·jenkins
Dontla16 天前
CI/CD前世今生(持续集成、持续交付、持续部署、Jenkins、Github Actions)
ci/cd·github·jenkins
Java 码思客16 天前
【ElasticSearch从入门到架构师】第7章-聚合查询——实现数据统计与分析
大数据·elasticsearch·jenkins
逻极16 天前
Jenkins 从入门到精通:CI/CD自动化流水线实战
ci/cd·自动化·jenkins·devops
炸炸鱼.16 天前
Git+Jenkins实战(一):从零搭建自动化发布与回滚系统(附完整代码)
git·jenkins
无人生还别怕17 天前
搭建jenkins服务并接入openldap认证
运维·jenkins