发布Java网站项目

发布框架

1634711617448.png

大致步骤

001 分布式构建

步骤详见:【分布式构建(Master/Slave)】
1634712393035.png

002 Git参数化

0001 设置分支

1634712872062.png

0002 设置发布节点(17服务器)

1634712932678.png

0003 新建项目

1634713601564.png
1634713757320.png

0004 推送现有文件夹到新建项目

#把18服务器的项目tomcat-java-demo-master推送到gitLab
[root@bogon tomcat-java-demo-master]# git config --global user.name "lql"
[root@bogon tomcat-java-demo-master]# git config --global user.email "lql_h@163.com"

[root@bogon ~]# cd tomcat-java-demo-master
[root@bogon tomcat-java-demo-master]# git init
[root@bogon tomcat-java-demo-master]# git remote add origin http://gitlab.pcitc.com/test/demo.git
[root@bogon tomcat-java-demo-master]# git add .
[root@bogon tomcat-java-demo-master]# git commit -m "Initial commit"
[root@bogon tomcat-java-demo-master]# git push -u origin master 

#注意
推送master的权限至少是Maintainer

003 配置git地址

1634717804581.png

0001 做一个测试

1634718309331.png
#17的服务器,即slave构建
[root@localhost jenkins_home]# cd  /opt/jenkins_home/workspace/
[root@localhost workspace]# ls
java-demo  

004 代码编译构建

0001 修改阿里云仓库

#网址
https://developer.aliyun.com/mvn/guide
#阿里云仓库
aliyunmaven*阿里云公共仓库https://maven.aliyun.com/repository/public
#修改setting
/usr/local/maven/conf

0002 构建shell测试

mvn clean package -Dmaven.test.skip=true
ls
ls target
1634721059090.png

005 拷贝构建文件到远程Web服务器

0001 清理tomcat

[root@localhost webapps]# tar -xzvf apache-tomcat-8.5.59.tar.gz 
[root@localhost webapps]# mv apache-tomcat-8.5.59 /usr/local/tomcat
[root@localhost webapps]# cd /usr/local/tomcat/
[root@localhost webapps]# cd webapps/
[root@localhost webapps]# rm -rf *

0002 修改SSH Servers的远程目录

1634722367970.png

0003 构建Send files

target/*.war
target/
usr/local/tomcat/webapps
ls /usr/local/tomcat/webapps
1634722568616.png
1634722601754.png

0004 检查结果

[root@bogon java-demo]# cd /usr/local/tomcat/webapps/
[root@bogon webapps]# ls
ly-simple-tomcat-0.0.1-SNAPSHOT.war

006 部署程序

# 备份已部署的程序
tomcat=/usr/local/tomcat
cd $tomcat/webapps
mv ROOT.war /data/backup/$(date +"%F_%T")_ROOT.war
# 部署新程序并重启Tomcat
mv ly-simple-tomcat-0.0.1-SNAPSHOT.war  ROOT.war
pid=$(ps -ef |grep tomcat |egrep -v 'grep' |awk '{print $2}')
[ -n "$pid" ] && kill -9 $pid
JAVA_HOME=/usr/local/jdk
export JAVA_HOME
/bin/bash /usr/local/tomcat/bin/startup.sh
1634734813186.png

集成Ansible,实现发布多台Web服务器

发布框架

1634765250764.png

安装ansible插件

ansible

ansible服务器准备

ansible配置文件

[root@bogon ~]# vi /etc/ansible/hosts  
[web1]
192.168.153.17
192.168.153.18

playbook.yml

- hosts: webservers
  tasks:
  - name: 查看主机内存
    shell: free -m
    register: result
  - debug: msg={{result.stdout_lines}}  

ansible-playbook运行测试

[root@bogon ~]# ansible-playbook playbook.yml -uroot -k
SSH password:root
TASK [Gathering Facts] 
ok: [192.168.153.18]
ok: [192.168.153.17]
TASK [查看主机内存] 
changed: [192.168.153.18]
changed: [192.168.153.17]
TASK [debug] 
ok: [192.168.153.17] => {
    "msg": [
        "              total        used        free      shared  buff/cache   available", 
        "Mem:           3773         417        2893          11         462        3063", 
        "Swap:          3999           0        3999"
    ]
}
ok: [192.168.153.18] => {
    "msg": [
        "              total        used        free      shared  buff/cache   available", 
        "Mem:           3773        2559         150          22        1063         832", 
        "Swap:          3999           0        3999"
    ]
}

ansible插件配置

1634768016393.png
1634768054478.png

Credentials

#确保19能够正确连接到17和18
[root@localhost .ssh]# ssh-copy-id root@192.168.153.17
[root@localhost .ssh]# ssh-copy-id root@192.168.153.18

ansible插件的构建运行

1634768459419.png

ansbile远程发布

配置文件

# vi /opt/jenkins/playbook.yaml
- hosts: "{{ env }}"
  gather_facts: no
  tasks:
  - name: 推送部署包到远程服务器
    copy: src="{{ item }}" dest=/tmp/ROOT.war
    with_fileglob:
      - "/opt/jenkins_home/workspace/java-demo/target/*.war"
  - name: 部署新程序并重启Tomcat
    shell: |
      tomcat=/usr/local/tomcat
      cd $tomcat/webapps
      mv *.war /data/backup/$(date +"%F_%T")_ROOT.war
      mv /tmp/*.war $tomcat/webapps/ROOT.war
      pid=$(ps -ef |grep tomcat |egrep -v 'grep' |awk '{print $2}')
      [ -n "$pid" ] && kill -9 $pid
      export JAVA_HOME=/usr/local/jdk
      nohup /usr/local/tomcat/bin/startup.sh

设置可选择的环境变量

1634771383041.png

设置环境变量对应的值

1634771619665.png
1634771648104.png

项目构建

1634887912031.png

发表评论

后才能评论