Git + Jenkins learning path (14th): automated script deployment practices

Source: Internet
Author: User
I. Environment Description and preparation 1. Environment Description
Host Name IP address Role System
Deploy-Server 192.168.56.12 Release Centos 1, 7.4
Web 192.168.56.13 Web server and NFS server Centos 1, 7.4
2. Server preparation (1) Preparations for Server Release A. Add common users and configure passwords
[[email protected] ~]# useradd www[[email protected] ~]# id wwwuid=1000(www) gid=1000(www) groups=1000(www)[[email protected] ~]# passwd www[[email protected] ~]# yum install -y git tree
B. Create the directory required for deployment and initialize the GIT directory.
[[email protected] ~]# mkdir -pv /deploy/{code/{www,jxs,wap,gys,glzx,yyzx},config,tar,tmp}[[email protected] ~]# chown -R www.www /deploy[[email protected] ~]$ tree /deploy/deploy├── code│?? ├── glzx│?? ├── gys│?? ├── jxs│?? ├── wap│?? ├── www│?? └── yyzx├── config├── tar└── tmp[[email protected] ~]$ cd /deploy/code/www && git init
C. Create the configuration file config. php
[[email protected] ~]$ vim /deploy/config/config.php this is config.php[[email protected] ~]$ tree /deploy/deploy├── code│?? ├── glzx│?? ├── gys│?? ├── jxs│?? ├── wap│?? ├── www│?? └── yyzx├── config│?? └── config.php├── tar└── tmp
D. Configure SSH communication between the publisher and the target server
[[email protected] deploy]$ cat /home/www/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDzM3AW6/X+djvKJTsdFbY3ik+mlradxpD3COoTP5h6x509unksuCdduV7awPjEGHvK2GVjJmvckxdvLkMc23p7bsctHlturPN2VozJTrYwXMAbmxf97cKE/fpKhjPXG8HlWBLpEaTM8PITgvdcyaeAUaIN+/h5VrA8TZKFAgbxDLxtgwqPzYIG9nqCO7MMCgzhJxI6PDQ6KVU9rHal/p4XKTIy4Rq4FzZTav2tS4zNJ7kX9+e6EO0JTooPanJXBTltLOJJsKxnlA7tc20rq6+0XVqbUBKYahL/8ZzkxZozNrNq7wtIuuJo0WTDFzDdPcJyAGlRWLuwct7y4p4UApVz [email protected][[email protected] ~]$ ssh-copy-id [email protected]/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/www/.ssh/id_rsa.pub"The authenticity of host '192.168.56.13 (192.168.56.13)' can't be established.ECDSA key fingerprint is SHA256:ahG6dBy/Z1nUIUWhQQrylsiwBlnDKC/jz8rnaPU2eF0.ECDSA key fingerprint is MD5:6e:58:0b:02:1c:a4:41:51:e8:7d:33:4d:46:bb:a0:68.Are you sure you want to continue connecting (yes/no)? yes/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys[email protected]'s password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh '[email protected]'"and check to make sure that only the key(s) you wanted were added.[[email protected] ~]$ ssh 192.168.56.13Last login: Fri Nov  2 18:01:02 2018[[email protected] ~]$ 
E. Copy the SSH public key to GitHub or gitlab.

(2) Prepare the Web server a. Deploy the NFS server
[[email protected] ~]# yum install -y nfs-utils rpcbind[[email protected] ~]# vim /etc/exports/nas/www 192.168.56.0/24(rw,sync,no_root_squash)/nas/jxs 192.168.56.0/24(rw,sync,no_root_squash)/nas/wap 192.168.56.0/24(rw,sync,no_root_squash)/nas/glzx 192.168.56.0/24(rw,sync,no_root_squash)/nas/yyzx 192.168.56.0/24(rw,sync,no_root_squash)/nas/gys 192.168.56.0/24(rw,sync,no_root_squash)[[email protected] ~]# systemctl start rpcbind[[email protected] ~]# systemctl start nfs[[email protected] ~]# showmount -eExport list for web:/nas/gys  192.168.56.0/24/nas/yyzx 192.168.56.0/24/nas/glzx 192.168.56.0/24/nas/wap  192.168.56.0/24/nas/jxs  192.168.56.0/24/nas/www  192.168.56.0/24
B. Mount the shared directory
[[email protected] ~]# mkdir /webroot/{www,jxs,wap,gys,glzx,yyzx}[[email protected] ~]# chown -R www.www /webroot[[email protected] ~]# mount -f nfs 192.168.56.13:/nas/www /webroot/www[[email protected] ~]# df -hFilesystem               Size  Used Avail Use% Mounted on/dev/mapper/centos-root   18G  1.7G   16G  10% /devtmpfs                 482M     0  482M   0% /devtmpfs                    493M     0  493M   0% /dev/shmtmpfs                    493M  6.8M  486M   2% /runtmpfs                    493M     0  493M   0% /sys/fs/cgroup/dev/sda1                497M  171M  326M  35% /boottmpfs                     99M     0   99M   0% /run/user/0192.168.56.13:/nas/www    18G  1.7G   16G  10% /webroot/www
C. Deploy nginx + PHP
[[email protected] ~]# useradd www[[email protected] ~]# passwd www[[email protected] ~]# yum install -y nginx php php-fpm[[email protected] ~]# vim /etc/nginx/nginx.confuser www;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;events {    worker_connections 1024;}http {    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;    tcp_nodelay         on;    keepalive_timeout   65;    types_hash_max_size 2048;    include             /etc/nginx/mime.types;    default_type        application/octet-stream;    # Load modular configuration files from the /etc/nginx/conf.d directory.    # See http://nginx.org/en/docs/ngx_core_module.html#include    # for more information.    include /etc/nginx/conf.d/*.conf;}[[email protected] ~]# mkdir /data/web -pv && chown -R www.www /data[[email protected] ~]# vim /etc/nginx/nginx.confserver {        listen 80;        server_name  localhost;        root /data/web/www;        index index.php index.htm index.html;        client_max_body_size 20m;        location / {                proxy_read_timeout 150;                try_files $uri $uri/ /index.php;        }        location ~* .*\.php$ {                #try_files     $uri =404;                include fastcgi_params;                fastcgi_pass 127.0.0.1:9000;                fastcgi_split_path_info       ^(.+\.php)(/.+)$;                fastcgi_param PATH_INFO       $fastcgi_path_info;                fastcgi_index index.php;                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        }}[[email protected] ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[[email protected] ~]# systemctl start nginx[[email protected] ~]# vim /etc/php-fpm.d/www.conf user wwwgroup www....[[email protected] ~]# php-fpm -t[03-Nov-2018 10:16:18] NOTICE: configuration file /etc/php-fpm.conf test is successful[[email protected] ~]# systemctl start php-fpm[[email protected] ~]# curl localhost -IHTTP/1.1 200 OKServer: nginx/1.12.2Date: Sat, 03 Nov 2018 02:16:36 GMTContent-Type: text/htmlContent-Length: 35Last-Modified: Sat, 03 Nov 2018 02:07:00 GMTConnection: keep-aliveETag: "5bdd02c4-23"Accept-Ranges: bytes
Ii. Release script writing 1. automated deployment process design
  1. The publisher obtains code (pull directly) and pulls the code from the svn or git repository.
  2. Compile code on the publisher (Optional). the Java language needs to be compiled, and PHP does not need to be compiled)
  3. Copy the configuration file to the code file (because the configuration file contains confidential information such as the database, it must be processed independently)
  4. Code packaging (compress and package the code)
  5. SCP to target server directory
  6. Remove the target server from the cluster
  7. Decompress the uploaded code on the target server.
  8. Decompress the package and soft link the code root directory to the directory file to implement the version link.
  9. Restart (optional, Tomcat needs to be restarted)
  10. Test
  11. Add to cluster
2. automated deployment script writing
#!/bin/bash#deploy-server create dir as root#[ -d /deploy ] && mkdir -pv /deploy/{code/web-demo,config,tar,tmp} && chown -R www.www /deploy#node-server create dir as root#[ -d /data ] && mkdir -pv /data/web && chown -R www.www /data#nfs dir /nas/www mount to /webroot#Node ListNODE="192.168.56.13"#Shell ENVSHELL_NAME="deploy.sh"SHELL_DIR="/home/www"SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log"#Code ENVPRO_NAME="www"CODE_DIR="/deploy/code/www"CONFIG_DIR="/deploy/config"TMP_DIR="/deploy/tmp"TAR_DIR="deploy/tar"LOCK_FILE="/tmp/deploy.lock"#Date/Time ENVLOG_DATE='date "+%Y-%m-%d"'LOG_TIME='date "+%H-%M-%S"'CDATE=$(date "+%Y-%m-%d")CTIME=$(date "+%H-%M-%S")useage(){    echo $"Useage: $0 { deploy |rollback [ list | version ] }"}url_test(){    URL=$1    curl -s --head $URL |grep '200 OK'    if [ $? -ne 0 ];then        shell_unlock;        writelog "test error" && exit;    fi}writelog(){    LOGINFO=$1    echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO} " >> ${SHELL_LOG}}code_get(){    writelog "code_get";    cd ${CODE_DIR} && git pull [email protected]:kin08200/learngit.git    cp -r ${CODE_DIR} ${TMP_DIR}    API_VERL=$(git show |grep commit |cut -d ' ' -f2 )        API_VER=$(echo ${API_VERL:0:6})    echo $API_VER}code_config(){    writelog "code_config"    /bin/cp -r ${CONFIG_DIR}/* ${TMP_DIR}/${PRO_NAME}        PKG_NAME=${PRO_NAME}_"${API_VER}"_"${CDATE}-${CTIME}"        cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}}code_tar(){    writelog "code_tar"    cd ${TMP_DIR} && tar -czf ${PKG_NAME}.tar.gz ${PKG_NAME}        writelog "${PKG_NAME}.tar.gz"}code_scp(){    writelog "code_scp"    scp ${TMP_DIR}/${PKG_NAME}.tar.gz $NODE:/webroot/www}code_deploy(){    writelog "code_deploy"    ssh $NODE "cd /webroot/www && tar -zxf ${PKG_NAME}.tar.gz"    ssh $NODE "rm -rf /data/web/www && ln -s /webroot/www/${PKG_NAME} /data/web/www"}code_test(){    url_test "http://192.168.56.13"}rollback_fun(){    ssh $NODE "rm -rf /data/web/www && ln -s /webroot/www/$1 /data/web/www"   }rollback(){    if [ -z $1 ];then        shell_unlock;        echo "Please input rollback version." && exit;        fi        case $1 in             list)            ls -l /deploy/tmp/*.tar.gz            ;;            *)            rollback_fun $1            ;;        esac}shell_lock(){    touch ${LOCK_FILE}}shell_unlock(){    rm -f ${LOCK_FILE}}main(){    if [ -f ${LOCK_FILE} ];then        echo "Deploy is running" && exit    fi    DEPLOY_METHOD=$1    ROLLBACK_VER=$2    case $DEPLOY_METHOD in        deploy)            shell_lock;        code_get;        code_config;        code_tar;            code_scp;        code_deploy;        code_test;        shell_unlock;        ;;    rollback)        shell_lock;        rollback $ROLLBACK_VER;        shell_unlock;        ;;    *)        useage;        ;;    esac}main $1 $2
Iii. Release and Test 1. Add SSH trust to the development machine and GitHub
[[email protected] ~]$ cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+ta7tkljnIgqvgEugQvIfVxH/a+geX250ZhOpe+14Q8EBQSI+qGyXCeihln+0aQpzIERvNBZ7JjWCg5XeQlgPBgCmqoQKNTWl/NUBT+uY/NY9fIGdRCVBvVcDC554Be48zB57mtapKQEkqm/8kmq7sPRQDv98l5wvFvYOPxocmjnioDZr3GeYmgdFPNJ5WGg6yY29IHXgh2v3eCXLwX2Z2eUdKCpV1LS42wdAN8TqHFCEmthREIq2r86ZKPOovD6Micq7wa2yJqtA/hkv+DvEhRzOIVznfW5EptOyKYcittGu63JGMSbCr1uCdW7PLUQ8aIWDDlip+/EcIt0KkuJJ [email protected]
2. Clone the project to the development machine for development and testing.
[[email protected] ~]$ mkdir dev && cd dev[[email protected] dev]$ git clone [email protected]:kin08200/learngit.git[[email protected] dev]$ lltotal 4drwxrwxr-x 3 www www 4096 Nov  2 22:02 learngit[[email protected] dev]$ cd learngit
3rd modify the index.html File
[[email protected] learngit]$ lltotal 8-rw-rw-r-- 1 www www  0 Nov  2 04:31 123-rw-rw-r-- 1 www www 35 Nov  2 22:02 index.html-rw-rw-r-- 1 www www 80 Nov  2 04:31 readme.txt[[email protected] learngit]$ echo "
4. Submit code
[[email protected] learngit]$ git add .[[email protected] learngit]$ git commit -m "modify index.html"[[email protected] learngit]$ git push origin master
5. release code
[[email protected] ~]$ ./deploy.sh deployremote: Enumerating objects: 5, done.remote: Counting objects: 100% (5/5), done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0Unpacking objects: 100% (3/3), done.From github.com:kin08200/learngit * branch            HEAD       -> FETCH_HEADUpdating 9a781b2..683a440Fast-forward index.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)683a44www_683a44_2018-11-03-10-06-56.tar.gz                                                                                                                      100%   22KB   9.3MB/s   00:00    HTTP/1.1 200 OK
6. Test Access
[[email protected] ~]$ curl 192.168.56.13
7. rollback Test
[[Email protected] ~] # Ll/data/web/total 0 lrwxrwxrwx 1 WWW 43 Nov 3 www->/webroot/www/www_683a44_2018-11-03-10-06-56 test access as follows: view the version list below, and select rollback to www_9a781b_2018-11-03-09-59-44 version

[[Email protected] ~] $. /Deploy. sh rollback list-RW-r -- 1 WWW 22686 Nov 3/deploy/tmp/www_683a44_2018-11-03-10-06-56.tar.gz-rw-rw-r -- 1 WWW 22685 Nov 3/deploy/tmp/www_683a44_2018-11-03-10-43-55.tar.gz-rw-rw-r -- 1 WWW WWW 22222 Nov 3/deploy/tmp/www_9a781b_2018-11-03-09-48-36.tar.gz-rw-rw-r -- 1 WWW 22231 Nov 3/deploy/tmp/www_9a781b_2018-11-03-09-52-32.tar.gz-rw-rw-r -- 1 WWW 2 2228 Nov 3/deploy/tmp/www_9a781b_2018-11-03-09-58-15.tar.gz-rw-rw-r -- 1 WWW 22234 Nov 3/deploy/tmp/www_9a781b_2018-11-03-09-59-44.tar.gz [[email protected] ~] $./Deploy. Sh rollback www_9a781b_2018-11-03-09-59-44 [[email protected] ~] # Ll/data/web/total 0 lrwxrwxrwx 1 WWW 43 Nov 3 www->/webroot/www/www_9a781b_2018-11-03-09-59-44 and then access the test:

Git + Jenkins learning path (14th): automated script deployment practices

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.