Jenkins + ansible 批量发布基于Tomcat的 Java 网站代码(使用*.war 包) Jenkins 持续集成实战

Jenkins 平台部署与安装:http://www.01wneo.xin/?p=620

Jenkins 平台设置与 Maven 编译工具的使用:http://www.01wneo.xin/?p=648

Ansible —- 安装配置:http://www.01wneo.xin/?p=451

[root@www-jfedu-net ~]# ll /usr/local/tomcat_jenkins/webapps/ROOT/jenkins.war
-rw-r--r--. 1 root root 64673620 2月  28 19:17 /usr/local/tomcat_jenkins/webapps/ROOT/jenkins.war
[root@www-jfedu-net ~]# vim /etc/ansible/hosts # 部署 .3 .4 .5 修改主机为免秘钥
192.168.1.16
[web]
192.168.1.3
192.168.1.4
192.168.1.5
[root@www-jfedu-net ~]# ansible web -m shell -a 'df -h' # 检查免秘钥是否成功
192.168.1.4 | SUCCESS | rc=0 >>
Filesystem            Size  Used Avail Use% Mounted on
rootfs                 10G  738M  9.3G   8% /
/dev/mapper/docker-253:0-33630259-ed5acf85379323fa55df3d75e8879aacd16b05a7528f72be792ee52394243465
                       10G  738M  9.3G   8% /
tmpfs                 489M     0  489M   0% /dev
tmpfs                 489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/cl-root    18G  7.1G   11G  40% /etc/resolv.conf
/dev/mapper/cl-root    18G  7.1G   11G  40% /etc/hostname
/dev/mapper/cl-root    18G  7.1G   11G  40% /etc/hosts
shm                    64M     0   64M   0% /dev/shm
/dev/mapper/cl-root    18G  7.1G   11G  40% /run/secrets

192.168.1.3 | SUCCESS | rc=0 >>
Filesystem            Size  Used Avail Use% Mounted on
rootfs                 10G  2.1G  8.0G  21% /
/dev/mapper/docker-253:0-33630259-87645b5cf71b6741a1bbf55adf60ef938a6a295f108aef06b1939569be4f3e1f
                       10G  2.1G  8.0G  21% /
tmpfs                 489M     0  489M   0% /dev
tmpfs                 489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/cl-root    18G  7.1G   11G  40% /etc/resolv.conf
/dev/mapper/cl-root    18G  7.1G   11G  40% /etc/hostname
/dev/mapper/cl-root    18G  7.1G   11G  40% /etc/hosts
shm                    64M     0   64M   0% /dev/shm
/dev/mapper/cl-root    18G  7.1G   11G  40% /run/secrets

192.168.1.5 | SUCCESS | rc=0 >>
Filesystem            Size  Used Avail Use% Mounted on
rootfs                 10G  738M  9.3G   8% /
/dev/mapper/docker-253:0-33630259-5c142cf37b1899cfd6914ed29f9db2f703289d73c93c4fe4384f0cc382edfd87
                       10G  738M  9.3G   8% /
tmpfs                 489M     0  489M   0% /dev
tmpfs                 489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/cl-root    18G  7.1G   11G  40% /etc/resolv.conf
/dev/mapper/cl-root    18G  7.1G   11G  40% /etc/hostname
/dev/mapper/cl-root    18G  7.1G   11G  40% /etc/hosts
shm                    64M     0   64M   0% /dev/shm
/dev/mapper/cl-root    18G  7.1G   11G  40% /run/secrets

# *.3 *.4 *.5 服务器需要提前搭建好 Tomcat

单个服务器部署 Tomcat:http://www.01wneo.xin/?p=844

# 批量发布网站代码

[root@www-jfedu-net ~]# cp /root/.jenkins/workspace/bbs.jfedu.net/target/edu.war /usr/local/src/
[root@www-jfedu-net ~]# ll /usr/local/src/edu.war 
-rw-r--r--. 1 root root 25900689 3月 3 15:00 /usr/local/src/edu.war
[root@www-jfedu-net ~]# ansible web -m synchronize -a "src=/usr/local/src/edu.war dest=/usr/local/tomcat/webapps/ROOT/"
192.168.1.3 | SUCCESS => {
 "changed": true, 
 "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /usr/local/src/edu.war 192.168.1.3:/usr/local/tomcat/webapps/ROOT/", 
 "msg": "<f+++++++++ edu.war\n", 
 "rc": 0, 
 "stdout_lines": [
 "<f+++++++++ edu.war"
 ]
}
192.168.1.4 | SUCCESS => {
 "changed": true, 
 "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /usr/local/src/edu.war 192.168.1.4:/usr/local/tomcat/webapps/ROOT/", 
 "msg": "<f+++++++++ edu.war\n", 
 "rc": 0, 
 "stdout_lines": [
 "<f+++++++++ edu.war"
 ]
}
192.168.1.5 | SUCCESS => {
 "changed": true, 
 "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /usr/local/src/edu.war 192.168.1.5:/usr/local/tomcat/webapps/ROOT/", 
 "msg": "<f+++++++++ edu.war\n", 
 "rc": 0, 
 "stdout_lines": [
 "<f+++++++++ edu.war"
 ]
}

# 批量解压edu.war
[root@www-jfedu-net ~]# ansible web -m shell -a "source /etc/profile;cd /usr/local/tomcat/webapps/ROOT/;jar -xf edu.war;ls"

# 批量启动 Tomcat nohup ***& 在后台启动 ***
[root@www-jfedu-net ~]# ansible web -m shell -a "source /etc/profile;nohup /usr/local/tomcat/bin/startup.sh&"

# 查看 Tomcat 启动进程
[root@www-jfedu-net ~]# ansible web -m shell -a "ps -ef | grep java"
192.168.1.4 | SUCCESS | rc=0 >>
root        124      1 31 19:47 ?        00:00:34 /usr/java/jdk1.8.0_161/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root        197    196  9 19:49 pts/1    00:00:00 /bin/sh -c ps -ef | grep java
root        199    197  0 19:49 pts/1    00:00:00 grep java

192.168.1.5 | SUCCESS | rc=0 >>
root        126      1 30 19:47 ?        00:00:34 /usr/java/jdk1.8.0_161/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root        197    196  9 19:49 pts/1    00:00:00 /bin/sh -c ps -ef | grep java
root        199    197  0 19:49 pts/1    00:00:00 grep java

192.168.1.3 | SUCCESS | rc=0 >>
root        124      1 30 19:47 ?        00:00:34 /usr/java/jdk1.8.0_161/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root        197    196 10 19:49 pts/1    00:00:00 /bin/sh -c ps -ef | grep java
root        199    197  0 19:49 pts/1    00:00:00 grep java

# 浏览器访问 *3 *4 *5 三台服务器

发表评论

zh_CNChinese
zh_CNChinese