废话不多说,直接开整
我们先使用手动的方式,发布一次
克隆gitlab项目到jenkins节点
[root@jenkins ~]# ls
anaconda-ks.cfg
[root@jenkins ~]# git clone git@gitlab.lilong.com:dev/web.git
Cloning into 'web'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@jenkins ~]# ls
anaconda-ks.cfg web
[root@jenkins ~]# cd web/
[root@jenkins web]# ls
index.html
我们将代码打包,配置web节点与jenkins免密
[root@jenkins web]# tar zcvf /opt/web_test_$(date +%F).tar.gz ./* --exclude=.git
./index.html
[root@jenkins web]# ls -a
. .. .git index.html
[root@jenkins web]# ls /opt/
web_test_2022-10-25.tar.gz
### 免密 ###
[root@jenkins web]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.200.130
The authenticity of host '192.168.200.130 (192.168.200.130)' can't be established.
ECDSA key fingerprint is 6c:43:78:dd:bc:31:56:a7:97:92:33:00:cd:70:0c:e2.
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.200.130's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.200.130'"
and check to make sure that only the key(s) you wanted were added.
### 测试 ###
[root@jenkins web]# ssh root@192.168.200.130
Last login: Tue Oct 25 20:54:52 2022 from 192.168.200.10
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:88:d9:51 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.130/24 brd 192.168.200.255 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::14f:411c:9b43:7d87/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::40c4:a63:a369:9d43/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::e938:3d7d:4dde:b4fb/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
[root@localhost ~]# exit
logout
Connection to 192.168.200.130 closed.
将tar包通过scp发送到web节点:
[root@jenkins opt]# for i in 130 ; do scp /opt/web_test_$(date +%F).tar.gz root@192.168.200.${i}:/opt ; done
web_test_2022-10-25.tar.gz 100% 118 0.1KB/s 00:00
登录nginx-01验证:
[root@nginx-01 ~]# ls /opt/
web_test-2022-10-25.tar.gz
通过ssh连接web节点,解压,创建软连接
[root@jenkins ~]# cd web/
[root@jenkins web]# vim web-tar.sh
#!/bin/bash
for i in 130
do
ssh root@192.168.200.${i} "
cd /opt && \
mkdir -p web_test_$(date +%F) && \
tar xf web_test_$(date +%F).tar.gz -C /opt/web_test_$(date +%F) && \
rm -rf /usr/share/nginx/html && \
ln -s /opt/web_test_$(date +%F) /usr/share/nginx/html
"
done
[root@jenkins web]# ls
index.html web-tar.sh
[root@jenkins web]# sh web-tar.sh
### 登录web验证 ###
[root@nginx-01 opt]# ls /usr/share/nginx/html/
index.html
下面实现自动发布过程:
jenkins项目,添加构建过程,选择使用shell脚本
[root@jenkins ~]# vim web-tar.sh
#!/bin/bash
App_Name=web_test
App_Dir=/opt
App_Date=$(date +%F_%H_%M)
App_Host="192.168.200.130"
APP_Site=/usr/share/nginx/html
code_tar() {
cd ${WORKSPACE}
tar zcvf ${App_Dir}/${App_Name}_${App_Date}.tar.gz ./*
}
code_scp() {
for host in ${App_Host}
do
scp ${App_Dir}/${App_Name}_${App_Date}.tar.gz root@${host}:/${App_Dir}
ssh root@${host} "
cd ${App_Dir} && \
mkdir -p ${App_Name}_${App_Date} && \
tar xf ${App_Name}_${App_Date}.tar.gz -C ${App_Name}_${App_Date} && \
rm -rf ${App_Name}_${App_Date}.tar.gz && \
rm -rf ${App_Site} && \
ln -s ${App_Dir}/${App_Name}_${App_Date} ${App_Site}
"
done
}
main() {
code_tar
code_scp
}
main
脚本授权:
[root@jenkins ~]# chmod +x web-tar.sh
应用保存:
测试:
修改启动用户:
[root@jenkins ~]# ps -ef | grep jenkins
jenkins 15651 1 99 21:33 ? 00:00:11 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=%C/jenkins/war --httpPort=8080
[root@jenkins ~]# vim /etc/sysconfig/jenkins
JENKINS_USER="root"
[root@jenkins ~]# vim /usr/lib/systemd/system/jenkins.service
User=root
Group=root
[root@jenkins ~]# systemctl daemon-reload
[root@jenkins ~]# systemctl restart jenkins.service
[root@jenkins ~]# ps -ef | grep jenkins
root 16226 1 13 21:39 ? 00:00:11 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=%C/jenkins/war --httpPort=8080
root 16336 15065 0 21:40 pts/0 00:00:00 grep --color=auto jenkins
重新构建:
登录web节点验证:
[root@nginx-01 opt]# ls
web_test_2022-10-25 web_test_2022-10-25.tar.gz
[root@nginx-01 opt]# ls /usr/share/nginx/html/
index.html
更改代码验证:
[root@gitlab web]# vim index.html
HELLO
### 提交 ###
[root@gitlab web]# git add .
[root@gitlab web]# git commit -m "更改成HELLO"
[master b740dad] 更改成HELLO
1 file changed, 1 insertion(+)
[root@gitlab web]# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 257 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@gitlab.lilong.com:dev/web.git
7b3225f..b740dad master -> master
jenkins再次验证:
[root@nginx-01 nginx]# cat /usr/share/nginx/html/index.html
HELLO
浏览器访问:
点个关注吧