welcome here!
当你觉得不行的时候,就去路上走一走,这样你就是一个行人了

Jenkins-自动化前端部署

基本架构介绍

生产中需要对开发、测试、压测、生产环境的前端部署文件进行更新发版,但是相对来生产不那么的更新频繁,但是可以结合Jenkins和对应的服务器进行免密之后,配合Gitlab服务进行前端部署代码文件的自动化部署。基本架构如下:web服务做成高可用的形式,前面通过nginx做均衡负载进行调度处理。而Jenkins服务配合Gitab服务器对前端代码的自动化部署更新。

在这里插入图片描述

WEB服务器部署

NGINX-RPM安装包下载

web-1和web-2都一样的安装

# yum install nginx-1.8.1-1.el7.ngx.x86_64.rpm -y

# vim  /etc/nginx/conf.d/default.conf
# 在server字段中改默认的index的web页面
......
    location / {
        root   /home/html/picchealth/dist/;        #前端部署路径
        index  index.html index.htm;
    }
......

# mkdir /home/html/picchealth/dist -p  #创建前端部署路径文件

# systemctl start nginx.service 
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx -s reload

负载均衡服务器部署

# yum install nginx-1.8.1-1.el7.ngx.x86_64.rpm -y

# vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

upstream  websrv{                 #后端调度服务器
         server 10.252.71.220:80;                                                                                 
         server 10.252.71.221:80;
      }

server  {
    listen 80;
    location /  {
        proxy_pass http://websrv;    #调用
    }
}

    include /etc/nginx/conf.d/*.conf;
}

# systemctl start nginx.service 
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx -s reload

Jenkins服务脚本

usercid='15bc11a5-f7c8-4b22-a9f8-f1c859d9c18b'
remote_zip_path = '/home/html/piccHealth/dist'  
remote_script_path = '/backend/script'
node {
     result = sh(script: "${remote_script_path}/picc_web_auth.sh ${es} ${auth}", returnStatus: true)
    if (result != 0){
        error "密码验证失败......."
    }
    echo "=======================密码验证成功======================="

    def gitbranch = "${es}"
    def remotes
    if (es == 'dev'){
        remotes = ['xx.xx.68.156']
    }else if (es == 'test'){
        remotes = ['xx.xx.68.152']
    }else if(es == 'stress'){
        gitbranch = "stress"
        remotes = ['xx.xx.71.202', 'xx.xx.71.203']
    }else if(es == 'prod'){
        gitbranch = "master"
        remotes = ['xx.xx.71.62', 'xx.xx.71.63']  //empoly_st.sh——IP check
    }
    echo "gitbranch-------------${es}-----------------${gitbranch}-----------"

    def giturl = 'https://zhangbr:xiaoXiang525@gitlab.picchealth.com/picc-center/picc-web.git'

    def zip_name = 'output.zip'

    def final_path = '/usr/local/jenkins2/.jenkins/workspace/picc-web-empoly/' + zip_name
    try {
        stage('Preparation') {
            checkout([
                $class: 'GitSCM', 
                branches: [[name: gitbranch]], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [], 
                submoduleCfg: [], 
                userRemoteConfigs: [[credentialsId: usercid, url: giturl]]
            ])
        }
         stage('remove file') {
                //多服务器循环 
                for(remote in remotes){
                    sh '/bin/cp -rf ' + final_path + ' /root/picc-web-empoly-installation-package/  '
                    sh '/bin/cp -rf ' + final_path + ' /root/picc-web-empoly-installation-package1/  '
                }
            }
        stage('run script') {
                for(remote in remotes){
                    sh ' bash /root/picc-web-empoly-script/empoly_st.sh ' + es
                    //sh ' bash /root/picc-web-empoly-script/test_likai.sh'
                }
            }
    }catch (err) {
        currentBuild.result = "FAILURE"
        throw err
    }
}

Jenkins服务器中

[root@jkglfwpt-dev-1-nginx script]# pwd
/backend/script
[root@jkglfwpt-dev-1-nginx script]# vim picc_web_auth.sh
picc_web_auth=$1
password=$2
echo "Build for ${picc_web_auth}-ENV."
if [[ ${picc_web_auth} = 'dev' ]] && [[ ${password} = 'xxxxxx' ]];then
        echo "Validate success."
        exit 0;
elif [[ ${picc_web_auth} = 'test' ]] && [[ ${password} = 'xxxxxxxx' ]];then
        echo "Validate success."
        exit 0;
elif [[ ${picc_web_auth} = 'stress' ]] && [[ ${password} = 'xxxxxx' ]];then
        echo "Validate success."
        exit 0;
elif [[ ${picc_web_auth} = 'prod' ]] && [[ ${password} = 'xxxxxx' ]];then
        echo "Validate success."
        exit 0;
else
        echo "Validate fail please input again !"
        exit 1;
fi
echo ==================================="

远程web服务器执行脚本

虽然非常的冗余,但是执行能力还是可以

[root@jkglfwpt-dev-1-nginx ~]# vim /root/picc-web-empoly-script/empoly_st.sh 
#!/bin/bash
#Confirm whether it is executed by root
su - root
whoami=`whoami`
if [ "$(whoami)" != 'root' ]; then
        echo  "You  have no permission to run $0 as non-root user."
        exit  100;
fi

#Judge shell parameter number
if [ $# == 1 ];then
    echo "you choose ENV is $1 , $1 must is [ dev | test | stress |prod ] one of them !";
else
    echo "Need to add only a parameter [ dev | test | stress | prod ]"
    exit 99;
fi

#Judge branch execution to define variables
case "$1" in
        dev)
        EMPOLY_ENV="dev"
                EMPOLY_IP="xx.xx.68.156"  
            EMPOLY_NAME="output" 
        EMPOLY_PATH="/home/html/picchealth/dist"
        data_dir=`date +"%Y-%m-%d-%H-%M"`

        ssh root@${EMPOLY_IP} "mkdir /home/html/backup/${data_dir} -p"

        ssh root@${EMPOLY_IP} "mv ${EMPOLY_PATH}/* /home/html/backup/${data_dir}"

        mv /root/picc-web-empoly-installation-package/${EMPOLY_NAME}.zip  /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip

        scp /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip  root@${EMPOLY_IP}:${EMPOLY_PATH}

        ssh root@${EMPOLY_IP} "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"

        ssh root@${EMPOLY_IP} "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"

        mkdir /tmp/empoly -p
        mv /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip /tmp/empoly

        ;;
        test)
            EMPOLY_ENV="test"
            EMPOLY_IP="xx.xx.68.152"  
            EMPOLY_NAME="output" 
        data_dir=`date +"%Y-%m-%d-%H-%M"`
        EMPOLY_PATH="/home/html/picchealth/dist"
        ssh root@${EMPOLY_IP} "mkdir /home/html/backup/${data_dir} -p"

        ssh root@${EMPOLY_IP} "mv ${EMPOLY_PATH}/* /home/html/backup/${data_dir}"

        mv /root/picc-web-empoly-installation-package/${EMPOLY_NAME}.zip  /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip

        scp /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip  root@${EMPOLY_IP}:${EMPOLY_PATH}

        ssh root@${EMPOLY_IP} "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"

        ssh root@${EMPOLY_IP} "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"

        mkdir /tmp/empoly -p
        mv /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip /tmp/empoly

        ;;
        prod)
            EMPOLY_ENV="prod"
            EMPOLY_IP1="xx.xx.71.62"  
            EMPOLY_IP2="xx.xx.71.63"  
            EMPOLY_NAME="output" 
        data_dir=`date +"%Y-%m-%d-%H-%M"`
        EMPOLY_PATH="/home/html/picchealth/dist"
        ssh root@${EMPOLY_IP1} "mkdir /home/html/backup/${data_dir} -p"
        ssh root@${EMPOLY_IP2} "mkdir /home/html/backup/${data_dir} -p"

        ssh root@${EMPOLY_IP1} "mv ${EMPOLY_PATH}/* /home/html/backup/${data_dir}"
        ssh root@${EMPOLY_IP2} "mv ${EMPOLY_PATH}/* /home/html/backup/${data_dir}"

        mv /root/picc-web-empoly-installation-package/output.zip  /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip
        mv /root/picc-web-empoly-installation-package1/output.zip  /root/picc-web-empoly-installation-package1/${EMPOLY_ENV}.zip

        scp /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip  root@${EMPOLY_IP1}:${EMPOLY_PATH}
        scp /root/picc-web-empoly-installation-package1/${EMPOLY_ENV}.zip  root@${EMPOLY_IP2}:${EMPOLY_PATH}

        ssh root@${EMPOLY_IP1} "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"
        ssh root@${EMPOLY_IP2} "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"

        ssh root@${EMPOLY_IP1} "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"
        ssh root@${EMPOLY_IP2} "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"

        mkdir /tmp/empoly -p
        mkdir /tmp/empoly1 -p
        #mv /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip /tmp/empoly
        #mv /root/picc-web-empoly-installation-package1/${EMPOLY_ENV}.zip /tmp/empoly1

        ;;
        stress)
            EMPOLY_ENV="stress"
            EMPOLY_IP1="xx.xx.71.202"  
            EMPOLY_IP2="xx.xx.71.203"  
            EMPOLY_NAME="output" 
        data_dir=`date +"%Y-%m-%d-%H-%M"`
        EMPOLY_PATH="/home/html/picchealth/dist"
        ssh root@${EMPOLY_IP1} "mkdir /home/html/backup/${data_dir} -p"
        ssh root@${EMPOLY_IP2} "mkdir /home/html/backup/${data_dir} -p"

        ssh root@${EMPOLY_IP1} "mv ${EMPOLY_PATH}/* /home/html/backup/${data_dir}"
        ssh root@${EMPOLY_IP2} "mv ${EMPOLY_PATH}/* /home/html/backup/${data_dir}"

        mv /root/picc-web-empoly-installation-package/output.zip /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip
        mv /root/picc-web-empoly-installation-package1/output.zip /root/picc-web-empoly-installation-package1/${EMPOLY_ENV}.zip

        scp /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip  root@${EMPOLY_IP1}:${EMPOLY_PATH}
        scp /root/picc-web-empoly-installation-package1/${EMPOLY_ENV}.zip  root@${EMPOLY_IP2}:${EMPOLY_PATH}

        ssh root@${EMPOLY_IP1} "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"
        ssh root@${EMPOLY_IP2} "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"

        ssh root@${EMPOLY_IP1} "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"
        ssh root@${EMPOLY_IP2} "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"

        mkdir /tmp/empoly -p
        mkdir /tmp/empoly1 -p
        #mv /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip /tmp/empoly
        #mv /root/picc-web-empoly-installation-package1/${EMPOLY_ENV}.zip /tmp/empoly1
    ;;
        *)
            echo "Need to add only a parameter [ dev | test | stress | prod ]"
                exit 110;
        ;;
esac 

执行测试

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

前端路径文件展示

[root@uias-st-4-logcatch ~]# cd /home/html/picchealth/dist/
[root@uias-st-4-logcatch dist]# ll
total 41240
-rw-r--r-- 1 root root     2240 Oct 22 11:14 index.html
drwxr-xr-x 3 root root       36 Oct 23 15:00 __MACOSX
drwxr-xr-x 3 root root       55 Oct 22 11:17 output
-rw-r--r-- 1 root root 42222247 Oct 23 15:00 prod.zip
drwxr-xr-x 7 root root       69 Oct 22 11:17 static

后期脚本优化

Jenkins服务脚本优化

usercid='15bc11a5-f7c8-4b22-a9f8-f1c859d9c18b'
remote_zip_path = '/home/html/piccHealth/dist'  
remote_script_path = '/backend/script'
node {
     result = sh(script: "${remote_script_path}/picc_web_auth.sh ${es} ${auth}", returnStatus: true)
    if (result != 0){
        error "密码验证失败......."
    }
    echo "=======================密码验证成功======================="

    def gitbranch = "${es}"
    def remotes
    if (es == 'dev'){
        remotes = ['xx.xx.68.156']
    }else if (es == 'test'){
        remotes = ['xx.xx.68.152']
    }else if(es == 'stress'){
        gitbranch = "stress"
        remotes = ['xx.xx.71.202', '10.252.71.203']
    }else if(es == 'prod'){
        gitbranch = "master"
        remotes = ['xx.xx.xx.xx', 'xx.xx.xx.xx']  
    }
    echo "gitbranch-------------${es}-----------------${gitbranch}-----------"

    def giturl = 'https://zhangbr:xiaoXiang525@gitlab.picchealth.com/picc-center/picc-web.git'

    def zip_name = 'output.zip'

    def final_path = '/usr/local/jenkins2/.jenkins/workspace/picc-web-empoly/' + zip_name
    try {
        stage('Preparation') {
            checkout([
                $class: 'GitSCM', 
                branches: [[name: gitbranch]], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [], 
                submoduleCfg: [], 
                userRemoteConfigs: [[credentialsId: usercid, url: giturl]]
            ])
        }
         stage('copy file') {
                    sh '/bin/cp -rf ' + final_path + ' /root/picc-web-empoly-installation-package/  '

            }

        stage('run script') {
                for(remote in remotes){
                    sh ' bash /root/picc-web-empoly-script/empoly_st1.sh ' + es + " " + remote
                    //sh ' bash /root/picc-web-empoly-script/test_likai.sh ' + es + " " + remote     //表示注释
                }
            }
    }catch (err) {
        currentBuild.result = "FAILURE"
        throw err
    }
}

远程web服务器执行脚本优化

参数$1和$2都是Jenkins脚本中传递下来的 es 和 remote

# cat empoly_st1.sh
#!/bin/bash
#********************************************************************
#Author:  KAIVI
#Date:   2020-10-26
#FileName:   empoly.sh
#Description:  The test script
#Copyright (C):  2020 All rights reserved
#********************************************************************

#Confirm whether it is executed by root
su - root
whoami=`whoami`
if [ "$(whoami)" != 'root' ]; then
        echo  "You  have no permission to run $0 as non-root user."
        exit  100;
fi

#Judge shell parameter number
if [ $# == 2 ];then
    echo "you choose ENV is $1 , $1 must is [ dev | test | stress |prod ] one of them and employ host is $2";
else
    echo "Need to add two parameter $1 is [ dev | test | stress | prod ] and $2 is remote host "
    exit 99;
fi

#Global variable definition
EMPOLY_ENV=$1
EMPOLY_NAME="output" 
EMPOLY_PATH="/home/html/picchealth/dist"
DATA_DIR=`date +"%Y-%m-%d-%H-%M"`

#Judge branch execution to define variables
case "$1" in
        dev)
        ssh root@$2 "ip a"
        ssh root@$2 "mkdir /home/html/backup/${DATA_DIR} -p"
        ssh root@$2 "mv ${EMPOLY_PATH}/* /home/html/backup/${DATA_DIR}"
        mv /root/picc-web-empoly-installation-package/${EMPOLY_NAME}.zip  /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip
        scp /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip  root@$2:${EMPOLY_PATH}
        ssh root@$2 "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"
        ssh root@$2 "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"
        ;;
        test)
        ssh root@$2 "ip a"
        ssh root@$2 "mkdir /home/html/backup/${DATA_DIR} -p"
        ssh root@$2 "mv ${EMPOLY_PATH}/* /home/html/backup/${DATA_DIR}"
        mv /root/picc-web-empoly-installation-package/${EMPOLY_NAME}.zip  /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip
        scp /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip  root@$2:${EMPOLY_PATH}
        ssh root@$2 "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"
        ssh root@$2 "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"
        ;;
        prod)
        ssh root@$2 "ip a"
        ssh root@$2 "mkdir /home/html/backup/${DATA_DIR} -p"
        ssh root@$2 "mv ${EMPOLY_PATH}/* /home/html/backup/${DATA_DIR}"
        mv /root/picc-web-empoly-installation-package/output.zip  /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip
        scp /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip  root@$2:${EMPOLY_PATH}
        ssh root@$2 "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"
        ssh root@$2 "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"
        ;;
        stress)
        ssh root@$2 "ip a"
        ssh root@$2 "mkdir /home/html/backup/${DATA_DIR} -p"
        ssh root@$2 "mv ${EMPOLY_PATH}/* /home/html/backup/${DATA_DIR}"
        mv /root/picc-web-empoly-installation-package/output.zip  /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip
        scp /root/picc-web-empoly-installation-package/${EMPOLY_ENV}.zip  root@$2:${EMPOLY_PATH}
        ssh root@$2 "unzip ${EMPOLY_PATH}/${EMPOLY_ENV}.zip -d ${EMPOLY_PATH}/"
        ssh root@$2 "cp -av ${EMPOLY_PATH}/output/* ${EMPOLY_PATH}/"
    ;;
        *)
            echo "Need to add only a parameter [ dev | test | stress | prod ]"
                exit 110;
        ;;
esac
赞(0) 打赏
开源学习 相互进步www.likai.tech » Jenkins-自动化前端部署
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

点赞是最好的支持

微信扫一扫打赏