Jenkins实现代码clone与项目部署

 

代码clone(全自动非交互式):
        通过shell 脚本或者命令
        通过jenkins clone
    
    git基于ssh公钥实现代码自动clone,不需要输入账户名和密码,要求公钥和私钥不能来回换服务器,而且不能push代码
    http是交互式的,不会用于jenkins的代码clone,但是clone后做代码修改后可以重新上传至gitlab
    
    部署方式:
        1.通过shell 命令clone代码,然后scp到各个web服务器,然后停止tomcat,代码替换,启动tomcat
        2.通过jenkins clone代码,然后对代码打包,然后scp到各个Web服务器,最后停服务,做代码替换,以及启动服务



四、Jenkins实现代码clone与项目部署
1.安装gitlab相关  https://www.cnblogs.com/Yuanbangchen/p/17061368.html

[root@localhost7G web1]#git clone http://192.168.80.100/java/web1.git
[root@localhost7G web1]#cd /web1/
[root@localhost7G web1]#echo " <h1>this is java web  v1 </h1>"  >  index.html 
[root@localhost7G web1]#git add .
[root@localhost7G web1]#git commit  -m  "v1"
[root@localhost7G web1]#git push

2.安装jinkins相关 https://www.cnblogs.com/Yuanbangchen/p/17061384.html


3.部署 web 服务器环境
192.168.80.100   localhost7A.localdomain    gitlab
192.168.80.110   localhost7B.localdomain    jeinkins 
192.168.80.120   localhost7C.localdomain    keepalived+harproxy
192.168.80.130   localhost7D.localdomain    keepalived+harproxy
192.168.80.140   localhost7E.localdomain    tomcat
192.168.80.150   localhost7F.localdomain    tomcat
192.168.80.160   localhost7G.localdomain    tomcat
4.安装keepalived+harproxy
[root@localhost7C ~]# cat /etc/haproxy/haproxy.cfg 
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats

defaults
    mode                    http

    option                  httplog
    option                  dontlognull
    option http-server-close
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

listen stats
   mode http
   bind 0.0.0.0:9999
   stats enable
   log global
   stats uri /haproxy-status
   stats auth haadmin:12345

listen tomcat-web
   bind 192.168.80.222:80
   mode tcp
   balance roundrobin
    server 192.168.80.160 192.168.80.160:8080 check inter 2s fall 3 rise 5
    server 192.168.80.150 192.168.80.150:8080 check inter 2s fall 3 rise 5
    server 192.168.80.140 192.168.80.140:8080 check inter 2s fall 3 rise 5


[root@localhost7C ~]# cat /etc/keepalived/keepalived.conf 
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id localhost7E
   vrrp_iptables
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.0.18
}
vrrp_instance zzhz {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass centos
    }
    virtual_ipaddress {
        192.168.80.222/24 dev eth0 label eth0:1
    }
}
5.三台安装tomcat并设置相关
[root@localhost7F ~]# cd /usr/local/
[root@localhost7F local]# tar xvf apache-tomcat-8.5.69
[root@localhost7F local]# ln -sv apache-tomcat-8.5.69 tomcat


mkdir /data/tomcat/tomcat_appdir -p       #保存 web 压缩包
mkdir /data/tomcat/tomcat_webapps         #tomcat app 目录
mkdir /data/tomcat/tomcat_webapps/myapp #创建myapp项目
echo "gitlab app v1"  > /data/tomcat/tomcat_webapps/myapp/index.html

cat  /usr/local/tomcat/conf/server.xml
      <Host name="localhost"  appBase="/data/tomcat/tomcat_webapps"  unpackWARs="true" autoDeploy="true">

设置权限相关操作。
useradd -m www -u 2019 -s /bin/bash #创建账户并设置密码
passwd www
chown www:www /data/  -R
chown www:www  /usr/local/tomcat/ -R

su - www -c "/etc/init.d/tomcat start"
6.jenkins服务器设置
git基于ssh公钥实现代码自动clone, 生成公钥,并设置gitlab的公钥信息。

[root@localhost7B ~]#mkdir /data/git/linux39 -p      #git克隆目录。
[root@localhost7B ~]# ssh-keygen
[root@localhost7B ~]# cat /root/.ssh/
id_rsa       id_rsa.pub  

7.在gitlab添加jenkins服务器上的root用户的公钥 (id_rsa.pub) 
测试: 不用账号和密码
[root@localhost7B ~]#git clone git@192.168.80.100:java/web1.git 
[root@localhost7B linux39]# git clone git@192.168.80.100:java/web1.git
正克隆到 'web1'...
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 25 (delta 4), reused 24 (delta 3)
接收对象中: 100% (25/25), done.
处理 delta 中: 100% (4/4), done.

[root@localhost7B linux39]# ls
web1
    
8. jenkins服务器root用户的公钥复制给三台tomcat,测试创建的文件属性 ssh-copy-id www@192.168.80.140 ssh-copy-id www@192.168.80.150 ssh-copy-id www@192.168.80.160 ssh www@192.168.80.160 "touch /tmp/xxxx"

 

9.jenkins项目中 创建选项中的Execute Shell,通过shell 命令clone代码,然后scp到各个web服务器,然后停止tomcat,代码替换,启动tomcat.
 
 

 

 echo $USER
 cd  /data/git/linux39  && rm  -rf web1 && git clone git@192.168.80.100:java/web1.git
 ssh www@192.168.80.140 "/etc/init.d/tomcat stop"
 ssh www@192.168.80.150 "/etc/init.d/tomcat stop"
 ssh www@192.168.80.160 "/etc/init.d/tomcat stop"
 scp -r /data/git/linux39/web1/*  www@192.168.80.140:/data/tomcat/tomcat_webapps/myapp/
 scp -r /data/git/linux39/web1/*  www@192.168.80.150:/data/tomcat/tomcat_webapps/myapp/
 scp -r /data/git/linux39/web1/*  www@192.168.80.160:/data/tomcat/tomcat_webapps/myapp/
 ssh www@192.168.80.140  "/etc/init.d/tomcat start"
 ssh www@192.168.80.150  "/etc/init.d/tomcat start"
 ssh www@192.168.80.160  "/etc/init.d/tomcat start"


更新项目测试:
[root@localhost7G web1]# echo " <h1>this is java web v2 </h1>" > index.html
[root@localhost7G web1]# git add .
[root@localhost7G web1]# git commit -m "v2"
[root@localhost7G web1]# git push
Username for 'http://192.168.80.100': laomao
Password for 'http://laomao@192.168.80.100':
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.80.100/java/web1.git
383699a..bfbcb67 master -> master

 

构建控制台显示:

 

 

 

 

 

 

另一种方式 :通过jenkins clone代码(调用jenkins服务器中root用户的私钥),然后对代码打包,然后scp到各个Web服务器,最后停服务,做代码替换,以及启动服务
1.[root@localhost7B linux39]# cat /root/.ssh/id_rsa
2.在jenkins添加root用户的私钥

3.添加地址和私钥

 


 

cd  /var/lib/jenkins/workspace/linux39-app1  &&  tar cvf myapp.tar.gz  ./*
scp myapp.tar.gz  www@192.168.80.140:/data/tomcat/tomcat_appdir 
scp myapp.tar.gz  www@192.168.80.150:/data/tomcat/tomcat_appdir 
scp myapp.tar.gz  www@192.168.80.160:/data/tomcat/tomcat_appdir 

ssh www@192.168.80.140 "/etc/init.d/tomcat stop"
ssh www@192.168.80.150 "/etc/init.d/tomcat stop"
ssh www@192.168.80.160 "/etc/init.d/tomcat stop"

ssh www@192.168.80.140 "rm -rf /data/tomcat/tomcat_webapps/myapp/* && cd /data/tomcat/tomcat_appdir &&  tar xvf myapp.tar.gz -C /data/tomcat/tomcat_webapps/myapp/"
ssh www@192.168.80.150 "rm -rf /data/tomcat/tomcat_webapps/myapp/* && cd /data/tomcat/tomcat_appdir &&  tar xvf myapp.tar.gz -C /data/tomcat/tomcat_webapps/myapp/"
ssh www@192.168.80.160 "rm -rf /data/tomcat/tomcat_webapps/myapp/* && cd /data/tomcat/tomcat_appdir &&  tar xvf myapp.tar.gz -C /data/tomcat/tomcat_webapps/myapp/"

 ssh www@192.168.80.140  "/etc/init.d/tomcat start"
 ssh www@192.168.80.150  "/etc/init.d/tomcat start"
 ssh www@192.168.80.160  "/etc/init.d/tomcat start"

更新项目测试:
[root@localhost7G web1]# echo " <h1>this is java web v3</h1>" > index.html
[root@localhost7G web1]# git add .
[root@localhost7G web1]# git commit -m "v3"
[root@localhost7G web1]# git push
Username for 'http://192.168.80.100': laomao
Password for 'http://laomao@192.168.80.100':
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.80.100/java/web1.git
383699a..bfbcb67 master -> master

 

 

 

 

posted @ 2023-02-09 17:22  yuanbangchen  阅读(342)  评论(0编辑  收藏  举报