Recent continuous integration with jenkins+gitlab+ansible, automated deployment and version rollback. However, the Deploy plugin failed to do incremental upgrades and rollback operations, tossing a long time to decide to write their own script simple implementation.
Environment:
7.0 64-bit gitlab:v2. 9.0 git: 2.9 1.651. 3, git plugin, GitLab plugin, Dynamic Parameter plug-in ansible:2.1. 0.0
Various tool functions:
1, Gitlab do version management
2. Jenkins performs the deployment or rollback operations according to the specified parameters, as well as the presentation and recording of the execution results.
3. Ansible is responsible for remote execution of scripts.
All of the following are done on the premise that the above conditions are installed and available.
Deployment script:
#!/bin/SH#说明show_usage="args: [-L,-R,-B,-w]\[--local-repository=,--repository-url=,--backup-dir=,--webdir=]"#参数 # Local warehouse directory Opt_localrepo=""# git repository urlopt_url=""# backup Directory Opt_backupdir=""# web Directory opt_webdir=""Getopt_args=`getopt-O l:r:b:W:-al local-repository:,repository-url:,backup-dir:, WebDir:--"[email protected]"' eval set--"$GETOPT _args"#获取参数 while[-N" $" ] Do Case " $" inch-l|--local-repository) opt_localrepo=$2;Shift 2;; -r|--repository-url) opt_url=$2;Shift 2;; -b|--backup-dir) opt_backupdir=$2;Shift 2;; -W|--webdir) opt_webdir=$2;Shift 2;; --) break;; *)Echo$1,$2, $show _usage; Esac Doneif[[-Z $opt _localrepo | |-Z $opt _url | |-Z $opt _backupdir | |-Z $opt _webdir]]; Then Echo$show _usageEcho "Opt_localrepo: $opt _localrepo, Opt_url: $opt _url, Opt_backupdir: $opt _backupdir, Opt_webdir: $opt _webdir"Exit0fi# The directory where the deployment script is located scriptdir=`pwd'/jenkinsdeploy/mkdir-P $scriptdir # Current deployment version number Currversion= $scriptdir/currentversion.txt# Last deployment version lastversion= $scriptdir/Lastversion.txtif[!-F"$lastversion"]; Then Echo "">$lastversionfi# git commit log Gitcommitlog= $scriptdir/gitcommitlog.txt# List of diff files between two versions Difffile= $scriptdir/Difffile.txt#if["$repodif" = = ""];then# Echo "the warehouse local directory cannot be empty, please enter the local warehouse directory parameter! "# Exit1#fi# Switch to the local repository directory CD $opt _localrepo# update code git pull $opt _url# get the commit logs git log--pretty=format:"%H">$gitcommitlog # Get the current commit version Currentcommit=`Head-1$gitcommitlog 'Echo "Current Commit ID: $currentcommit"# Last commit of the deploymentIDLastdeployid=`Head-1$lastversion 'Echo "Lastdeployid: $lastdeployid"if["$lastdeployid"=""]; ThenLastdeployid=`Tail-1$gitcommitlog 'Echo "Lastdeployid: $lastdeployid"fiif["$lastdeployid"="$currentcommit"]; Then Echo "same as the last deployed version ID, $currentcommit, no deployment operation. "Exitfi# Get a list of files with differences between two versionsEcho "pwd: ' pwd '"gitdiff$lastdeployid $currentcommit--name-only >$difffileEcho "git diff $lastdeployid $currentcommit--name-only $difffile"> $scriptdir/log.txt# generate a backup directory based on the current time bakversion=`Date "+%y%m%d%h%m"' Bakdir= $opt _backupdir/$bakversionEcho "Bakdir: $bakdir"# Create a backup directorymkdir-p $bakdir # backup file, to save relative directory structure must switch to the program root, otherwise get absolute directory CD $opt _webdirCat$difffile |Xargs-i-tCP-RF--parents {} $bakdir/# Deployment, to save the relative directory structure must switch to the program root, otherwise get an absolute directory CD $opt _localrepoCat$difffile |Xargs-i-tCP-RF--parents {} $opt _webdir/# Log Current deployment version informationEcho$currentcommit >$lastversionEcho "$bakdir">> $scriptdir/backup.txt
Version rollback script:
#!/bin/SH#说明show_usage="args: [-B,-w]\[--backup-dir=,--webdir=]"#参数 # backup directory Opt_backupdir=""# web Directory opt_webdir=""Getopt_args=`getopt-O B:W:-al backup-dir:, WebDir:--"[email protected]"' eval set--"$GETOPT _args"#获取参数 while[-N" $" ] Do Case " $" inch-b|--backup-dir) opt_backupdir=$2;Shift 2;; -W|--webdir) opt_webdir=$2;Shift 2;; --) break;; *)Echo$1,$2, $show _usage; Esac Doneif[-Z $opt _backupdir | |-Z $opt _webdir]]; Then Echo$show _usageEcho "Opt_backupdir: $opt _backupdir, Opt_webdir: $opt _webdir"Exit0fi# The directory where the deployment script is located scriptdir=`pwd'/jenkinsdeploy/mkdir-P $scriptdir # backup record backupfile= $scriptdir/backup.txt# Gets the record of the last backup Bakdir=`Tail-1$backupfile 'if["$bakdir"=""]; Then Echo "Unable to get backup directory. "ExitfiEcho "backup directory: $bakdir"Echo "Web directory: $opt _webdir"CD $bakdirCP-RF--parents./*$opt _webdir/echo "successfully rolled back. "
Jenkins configuration: Add an optional parameter
This build is parameterized--Choice Parameter
The variable name is:
Deploy_envirenment
Variable Value:
Deployrollback
Execute script:
Echo$deploy _envirenment Case$deploy _envirenmentinchdeploy)Echo "Deploy: $deploy _envirenment"ansible webservers-M script-a"~/bashscript/xxxxxx_deploy.sh--local-repository=/www/test/test--repository-url=git Warehouse Address--backup-dir=/www/ Test/bak--webdir=/www/test/www" ;; Rollback)Echo "rollback: $deploy _envirenment"ansible webservers-M script-a'~/bashscript/xxxxxx_rollback.sh--backup-dir=/www/test/bak--webdir=/www/test/www' ;; *) exit;;Esac
Final execution
Output log to perform rollback operations
Execute script: Started by user xxxbuildinginchworkspace/var/lib/jenkins/jobs/ttt/Workspace[workspace] $/bin/SH-xe/tmp/hudson8451273492016631046.SH+EchoRollbackrollback+ Case$deploy _envirenmentinch+Echo 'Rollback:rollback'Rollback:rollback+ ansible webservers-m script-a'~/bashscript/xxxx_rollback.sh--backup-dir=/www/test/bak--webdir=/www/test/www'xxx.xxx.xxx.xxx| SUCCESS = { "changed":true, "RC":0, "stderr":"", "stdout":"backup directory:/www/test/bak/201607131627\r\nweb directory: \ r \ n successfully rolled back. \ r \ n", "Stdout_lines": [ "backup directory:/www/test/bak/201607131627", "Web directory:", "Roll back successfully. "]}finished:success
This is simply the implementation of automated deployment and rollback operations, if done more intelligently, combined with the database can be made to roll back to a version, and so on.
For more information see: http://www.cnblogs.com/rwxwsblog/category/850777.html, if you have a better idea, you might as well tell me. Thank you!
Jenkins combines ansible with shell for automated deployment and rollback