Continuous integration ⑤:jenkins Combining script to implement code automation deployment and one-click Rollback first version __jenkins

Source: Internet
Author: User
Tags call shell curl rollback ssh
Continuous integration ⑤:jenkins Combining script to implement code automation deployment and one-click Rollback first version

One: This article through Jenkins call shell script to get code from git server, package, deploy to Web server, remove Web server from load balancer, extract, copy configuration file, create soft connection, test each Web server, Add the Web server to load balancing, rolling back to any specified version, one key rollback to the previous version, and so on, the script is placed in the WWW user home directory and executed using the WWW user identity, and each Web server runs the Web service using the WWW user, and the same Web directory and permissions for the UID are the same. Stricter standardization can lead to a more secure production environment and higher efficiency:
1.1: Invoke the Shell script and environment preparation in the Jenkins project configuration:
1.1.1: #jenkins-Project-Configuration:

1.1.2:www the contents of the script in the user's home directory:

$ cat code_deploy.sh #!/bin/bash #Dir List Deployment node (that is, what needs to be done by the deployment node) # mkdir-p/deploy/code/web-demo # mkdir-p/deploy/config/ 
Web-demo/base # mkdir-p/deploy/config/web-demo/other # mkdir/deploy/tmp # mkdir/deploy/tar Chown-r # Www.www/deploy # chown-r Www.www/webroot # chown-r www.www/opt/webroot/# chown-r Www.www/webroot # What needs to be done on the client node # Mkdir/opt/web  Root # mkdir/webroot # chown-r Www.www/webroot # chown-r www.www/opt/webroot/# chown-r Www.www/webroot # [www@ ~]$
Touch/webroot/web-dem # Node List server node pre_list= "192.168.3.12" # Pre-production node group1_list= "192.168.3.12 192.168.3.13" Group2_list= "192.168.3.13" rollback_list= "192.168.3.12 192.168.3.13" # Log date and time variable log_date= ' date ' +%y-%m-%d ' # If executed after the time, this time is not fixed, this is the log used time log_time= ' date ' +%h-%m-%s ' # code package time variable cdate=$ (date "+%y-%m-%d") # Once executed, the script takes a fixed time assignment to the variable, which is a fixed ctime=$ (date + "%h-%m-%s") # Shell env script location Variable shell_name= ' deploy.sh # script name shell_dir= '/h ome/www/"# Script path shell_log=" ${shell_dir}/${shell_namE}.log "# Script execute log file path # code env code variable pro_name=" Web-demo "# project name function code_dir="/deploy/code/web-demo "# updated code from version management system 
Record config_dir= "/deploy/config/web-demo" # Save a configuration file for a different project, a directory that is a profile of a project or multiple profiles tmp_dir= "/deploy/tmp" # Temp directory Tar_dir= "/deploy/tar" # Packing directory lock_file= "/tmp/deploy.lock" # Lock file path usage () {# using the Help function echo $ ' usage: $ [de Ploy |  Rollback [List | emergency | version] "} writelog () {# write function loginfo=$1 # to log parameters as log input echo ' ${cdate} ${ctime}: 
    ${sehll_name}: ${loginfo} ' >> ${shell_log} # Lock function Shell_lock () {Touch ${lock_file}} # Unlock function Shell_unlock () {  Rm-f ${lock_file} # Get the function of the code code_get () {echo "Code_get" Writelog code_get cd $CODE _dir && git
    Pull # into the Code directory update code, this must be password-free update, this directory only for code updates can not put any other files Cp-rf ${code_dir} ${tmp_dir}/# temporarily save the Code and rename, package name is time + version number, ready to replicate to the Web server api_verl=$ (git show | grep commit | cut-d '-f2) api_ver=$ (Echo ${api_verl:0:8}) # version number} code_build () { # code compilation function echo Code_build} code_config () {# profile function Writelog ' code_config '/bin/cp-rf ${config_dir}/base/* ${tmp_dir}/' ${PRO_N AME} "# Place the configuration file in the temporary directory of the local save configuration file to temporarily save the Code project Pkg_name=" ${pro_name} "_" $API _ver "_" ${cdate}-${ctime} "# define code directory name CD ${t Mp_dir} && MV ${pro_name} ${pkg_name} # Rename code file to web-demo_123-20170629-11-19-10 format} code_tar () {# to package code function Writelog Code_tar CD ${tmp_dir} && tar czf ${pkg_name}.tar.gz ${pkg_name}--exclude= ". Git" # to package directories into compressed files For easy network transport Writelog "${pkg_name}.tar.gz packaged Success" # Record package successful log} CODE_SCP () {# code compression package SCP to client function Writelog " CODE_SCP "for node in $PRE _list;do # Loop server node list SCP ${tmp_dir}/${pkg_name}.tar.gz $node:/opt/webroot/# will compress code Package Copy to Web server/opt/webroot done for node in $GROUP 1_list;do # Loop server node list SCP ${tmp_dir}/${pkg_name}.tar.gz $n ode:/opt/webroot/# to copy the compressed code package to the Web server/opt/webroot done} cluster_node_add () {#将web服务器添加至前端负载 echo cluster_node_a DD} Cluster_node_removE () {# Removes the Web server from the cluster function (should not process the business when it is being deployed) Writelog "Cluster_node_remove"} url_test () {url=$1 curl-s--head $URL
        |grep ' OK ' if [$-ne 0];then Shell_unlock;
    Writelog "Test Error" && exit; fi} Pre_deploy () {# code decompression deployment function, pre-production node Writelog "Pre_deploy" for node in ${pre_list};d O # Cycle pre-production server node list Clust Er_node_remove ${node} # Remove the node from the front-end load before the echo ' Pre_deploy, Cluster_node_remove ${node} ' ssh ${node} ' CD/ Opt/webroot && tar zxf ${pkg_name}.tar.gz "#分别到web服务器执行压缩包解压命令 ssh ${node}" rm-f/webroot/web-demo Amp  Ln-s/opt/webroot/${pkg_name}/webroot/web-demo "# The core of the entire automation, creating a soft connection done} pre_test () {# Pre-production host Test function for node in ${pre_list};d o # Cycle pre-production host list Curl-s--head http://${node}:9999/index.html | grep OK # Test Web interface access if [$-eq 0];then # If Access succeeds Writelog "${node} Web Test ok!" # Records
                Log echo "${node} Web Test ok!" CLuster_node_add ${node} # After successfully calling add function to add server to node, writelog "Pre,${node} Add to cluster ok!" # Record add server to cluster log 
                Else # if Access fails Writelog "${node} Test no OK" # log echo "${node} Test not OK"
    Shell_unlock # Call Delete lock file function Break # End deployment fi done} Group1_deploy () {# code decompression deployment function  
        Writelog "Group1_code_deploy" for node in ${group1_list};d o # Recycle production server node list Cluster_node_remove $node echo "group1, Cluster_node_remove $node" ssh ${node} "cd/opt/webroot && tar zxf ${pkg_name}.tar.gz" # Separate To the Web server node to perform the compression package decompression command SSH ${node} "rm-f/webroot/web-demo && ln-s/opt/webroot/${pkg_name}/webroot/web-de Mo "# The core of the entire automation, creating a soft connection done by SCP ${config_dir}/other/192.168.3.13.server.xml 192.168.3.13:/WEBROOT/WEB-DEMO/SERVER.XM L # The profile of the variance project is SCP to this Web server and ends at the end of the project} group1_test () {# Production host Test function for node in ${pre_list};d o # Recycle production host List curl- S--head Http://${node}:9999/index.html |
            grep "OK" #测试web界面访问 if [$-eq 0];then #如果访问成功 writelog "${node} Web Test ok!" #记录日志
            echo "Group1_test,${node} Web Test ok!" Cluster_node_add writelog "${node} Add to cluster ok!" #记录将服务器 added to the log of the cluster else #如果访问失败 writ Elog "${node} Test no OK" #记录日志 echo "${node} Test no OK" Shell_unlock # Call Delete lock file function bre AK # End Deployment fi done} emergency_code_get () {#获取代码的函数 writelog "Code_get" CD ${code_dir} && git Reset--hard head^ #进入到代码目录更新代码, the password must be updated here, this directory only for code updates can not put any other file/bin/cp-rf ${code_dir} ${tmp_dir}/#临时保存代码并重命名, package name is Time + Version number, ready to replicate to Web server api_verl=$ (git show | grep commit | cut-d '-f2) api_ver=$ (Echo ${api_verl:0:8}) #取八位} emerge
        Ncy () {#紧急回退到上一个版本函数 emergency_code_get #执行将代码回退到上一个版本函数 code_build; #如果要编译执行编译函数 code_config; #cp配置文件 Code_tar; #打包 CODE_SCP; #scp到服务器 Cluster_node_remove Pre_deploy; #预生产环境部署 pre_test; #预生产环境测试 Group1_deploy; #生产环境部署 group1_test; #生产环境测试 Code_config; #cp差异文件 #code_test; #代码测试 Shell_unlock #执行完成后删除锁文件} rollback_fun () {for node in $ROLLBACK _list;do # Loop server Node List # Note that you must add ' Number, otherwise the remote command ssh $node "rm-f/webroot/web-demo && ln-s/opt/webroot/$1/webroot/web-demo" # cannot be rolled back to the specified version immediately.
        The specified version parameter, echo "${node} rollback success!" DONE} rollback () {# code rollback main function If [-Z $];then shell_unlock # Delete lock file echo ' please input rollback versi
    On "&& exit 3;
        Fi case "in #" take the second parameter as own first parameter list) ls-l/opt/webroot/*.tar.gz;;
        *) Rollback_fun $ ESAC} main () {if [-f $LOCK _file]; # then # first determine if the lock file is not in, if a lock file exits directly echo "Deploy is running" && exit fi deploy_method=$1 # Avoid errors The first argument of the script as a variable rollback_ver=$2 c ASE $DEPLOY _methodIn deploy) # If the first argument is deploy perform the following operations Shell_lock; # Create a lock before performing a deployment. If there are other people executing at the same time, prompt lock file exists code_get; # get Code Code_build; # If you want to compile the Execute compiler function code_config;    # CP configuration file Code_tar;    # packing CODE_SCP;  # SCP to server Pre_deploy;    # Pre-production environment deployment pre_test; # Pre-production environment Test group1_deploy;   # Production environment Deployment group1_test; # production environment Test Shell_unlock;
        # Delete lock file after execution completes;; Rollback) # If the first argument is rollback perform the following operations Shell_lock;
            # before rollback is also the first to create a lock file rollback $ROLLBACK _ver; Shell_unlock;
        # execution Complete delete lock file;; Emergency) emergency;
        #紧急回退就不需要参数了, but in the implementation of the time to confirm whether the urgent fallback, to avoid input errors;;
    *) usage; ESAC} main $

1.1.4: Modify the current Web page:

[Www@master ~]$ cd Web-demo
[Www@master web-demo]$ Echo ' 

1.4: Roll back to any version:
1.4.1: Where do I see the rollback to the version?:
$ ll/deploy/tmp/#部署服务器, Web server in Nginx defined directory view version

1.4.3: Perform rollback in Jenkins:

[Root@slave01 ~]# ll/opt/webroot/total 20672 drwxr-xr-x 5 www www 4096 June 11:36 WEB-DEMO_123_2017-06-26-11-36-44-RW -rw-r--1 www www 1243347 June 22:03 web-demo_123_2017-06-26-11-36-44.tar.gz drwxr-xr-x 5 www www 4096 June 11:39 web-
demo_123_2017-06-26-11-39-02-rw-rw-r--1 www. www. 1243347 June 22:06 web-demo_123_2017-06-26-11-39-02.tar.gz Drwxr-xr-x 5 www www 4096 june 12:04 web-demo_123_2017-06-26-12-04-19-rw-rw-r--1 www. 1243351 June 22:31 Web-demo _123_2017-06-26-12-04-19.tar.gz drwxr-xr-x 5 www www 4096 june 12:16 web-demo_123_2017-06-26-12-16-49-rw-rw-r--1 www w WW 1243347 June 22:43 web-demo_123_2017-06-26-12-16-49.tar.gz drwxr-xr-x 5 www www 4096 June 12:18 web-demo_123_2017-0 6-26-12-18-09-rw-rw-r--1 www www 1243347 June 22:45 web-demo_123_2017-06-26-12-18-09.tar.gz drwxr-xr-x 5 www www 4096 June 12:18 web-demo_123_2017-06-26-12-18-57-rw-rw-r--1 www www 1243369 June 22:46 web-demo_123_2017-06-26-12-18-57.t ar.gz-rw-rw-r--1 www www. 29 06:21 web-demo__2017-06-30-14-28-54.tar.gz drwxrwxr-x 2 www www 4096 June 14:30 web-demo__2017-06-30-14-30-22-rw-rw-r --1 www www 130 june 06:23 web-demo__2017-06-30-14-30-22.tar.gz drwxrwxr-x 2 www www 4096 June 14:31 web-demo__2017-0 6-30-14-31-53-rw-rw-r--1 www www 219 june 06:24 web-demo__2017-06-30-14-31-53.tar.gz drwxrwxr-x 4 www www 4096 June 30 14:59 web-demo_75463f1b_2017-06-30-14-59-58-rw-rw-r--1 www. 1236456 June 06:52 web-demo_75463f1b_2017-06-30-14-59 -58.tar.gz drwxrwxr-x 4 www www 4096 june 15:05 web-demo_75463f1b_2017-06-30-15-05-57-rw-rw-r--1 www www 1236450 June 2 9 06:58 web-demo_75463f1b_2017-06-30-15-05-57.tar.gz drwxrwxr-x 4 www www 4096 June 14:01 web-demo_75463f1b_2017-07-10- 14-01-34-rw-rw-r--1 www www 1236446 June 14:01 web-demo_75463f1b_2017-07-10-14-01-34.tar.gz drwxrwxr-x 4 www. 4096 June 15:18 web-demo_78869143_2017-06-30-15-18-29-rw-rw-r--1 www www 1236465 June 07:11 web-demo_78869143_2017-06-30- 15-18-29.tar.gz Drwxrwxr-x4 www. 4096 June 14:00 web-demo_78869143_2017-07-10-14-00-35-rw-rw-r--1 www www 1236453 June 14:00 web-demo_78869 143_2017-07-10-14-00-35.tar.gz drwxrwxr-x 3 www. 4096 June 14:14 web-demo_91d09cc2_2017-06-30-14-14-42-rw-rw-r--1 W WW www 1236371 June 06:06 web-demo_91d09cc2_2017-06-30-14-14-42.tar.gz drwxrwxr-x 3 www. 4096 June 14:15 Web-demo_9
1d09cc2_2017-06-30-14-15-16-rw-rw-r--1 www. 1236382 June 06:08 web-demo_91d09cc2_2017-06-30-14-15-16.tar.gz Drwxrwxr-x 4 www. 4096 14:08 web-demo_9a43cf55_2017-07-10-14-08-46-rw-rw-r--1 www www. 1233708

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.