如何通过Git WebHooks+脚本实现自动更新发布代码之shell脚本
发表于:2024-11-22 作者:千家信息网编辑
千家信息网最后更新 2024年11月22日,小编给大家分享一下如何通过Git WebHooks+脚本实现自动更新发布代码之shell脚本,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一
千家信息网最后更新 2024年11月22日如何通过Git WebHooks+脚本实现自动更新发布代码之shell脚本
小编给大家分享一下如何通过Git WebHooks+脚本实现自动更新发布代码之shell脚本,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
脚本的一些特点和功能:
解决脚本的符号链接问题,准确获取脚本工作目录(从tomcat脚本中学到);
颜色显示,不同级别的信息用不同的颜色显示(共两种方案,前一种从一位不知名的国外工程师处得到,后一种从lnmp1.2脚本中得到);
生成capistrano目录结构;
清除过期目录和文件;
检查部署要求是否满足,如磁盘空间的需求等,其他的要求检查有待于添加;
部署失败后归滚;
项目废弃后删除项目;
脚本里面尽可能的考虑容错(异常判断)和部分细节;
脚本存在的已知的问题:
(1)脚本中有几个TODO,有待于改善;
(2)repository目录暂时没有用到;
(3)获取git的版本号问题;
(4)部署后生成的日志有待于优化;
(5)脚本基本使用英文(Chinglish)作为注释,部分注释可能不全;
(6)可能存在的其他问题;
为了后期的改进和部分需求变更,也为了便于获取该脚本,此脚本可以从github上获取,欢迎和接受提交任何bug、issue以及任何improvement。
脚本内容如下:
#!/bin/bash# Public header# =============================================================================================================================# resolve links - $0 may be a symbolic linkPRG="$0"while [ -h "$PRG" ]; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fidone# Get standard environment variablesPRGDIR=`dirname "$PRG"`# echo color functionfunction cecho { # Usage: # cecho -red sometext #Error, Failed # cecho -green sometext # Success # cecho -yellow sometext # Warning # cecho -blue sometext # Debug # cecho -white sometext # info # cecho -n # new line # end while [ "$1" ]; do case "$1" in -normal) color="\033[00m" ;;# -black) color="\033[30;01m" ;;-red) color="\033[31;01m" ;;-green) color="\033[32;01m" ;;-yellow) color="\033[33;01m" ;;-blue) color="\033[34;01m" ;;# -magenta) color="\033[35;01m" ;;# -cyan) color="\033[36;01m" ;;-white) color="\033[37;01m" ;;-n) one_line=1; shift ; continue ;;*) echo -n "$1"; shift ; continue ;;esacshiftecho -en "$color"echo -en "$1"echo -en "\033[00m"shiftdoneif [ ! $one_line ]; then echofi}# end echo color function# echo color function, smarterfunction echo_r () { #Error, Failed [ $# -ne 1 ] && return 0 echo -e "\033[31m$1\033[0m"}function echo_g () { # Success [ $# -ne 1 ] && return 0 echo -e "\033[32m$1\033[0m"}function echo_y () { # Warning [ $# -ne 1 ] && return 0 echo -e "\033[33m$1\033[0m"}function echo_b () {\ # Debug [ $# -ne 1 ] && return 0 echo -e "\033[34m$1\033[0m"}# end echo color function, smarterWORKDIR=$PRGDIR# end public header# =============================================================================================================================# Where to get source codeSOURCEURL=https://github.com/DingGuodong/GitOSCAutoDeploy.git# Setting how many days do you want save old releases, default is 10 dayssave_old_releases_for_days=10function setDirectoryStructure() { if [ -f $WORKDIR/.lock ];then echo_g "Set directory structure has been done, skipping. " return fi echo_b "Setting directory structure." # learn from capistrano # Refer: http://capistranorb.com/documentation/getting-started/structure/ # Refer: http://capistranorb.com/documentation/getting-started/structure/# # ├── current -> /var/www/my_app_name/releases/20150120114500/ # ├── releases # │ ├── 20150080072500 # │ ├── 20150090083000 # │ ├── 20150100093500 # │ ├── 20150110104000 # │ └── 20150120114500 # ├── repo # │ └──# ├── revisions.log # └── shared # └── # current is a symlink pointing to the latest release. This symlink is updated at the end of a successful deployment. If the deployment fails in any step the current symlink still points to the old release. # releases holds all deployments in a timestamped folder. These folders are the target of the current symlink. # repo holds the version control system configured. In case of a git repository the content will be a raw git repository (e.g. objects, refs, etc.). # revisions.log is used to log every deploy or rollback. Each entry is timestamped and the executing user (username from local machine) is listed. Depending on your VCS data like branchnames or revision numbers are listed as well. # shared contains the linked_files and linked_dirs which are symlinked into each release. This data persists across deployments and releases. It should be used for things like database configuration files and static and persistent user storage handed over from one release to the next. # The application is completely contained within the path of :deploy_to. If you plan on deploying multiple applications to the same server, simply choose a different :deploy_to path. # Check directories for deploy # [ ! -d $WORKDIR/current ] && mkdir $WORKDIR/current [ ! -d $WORKDIR/release ] && mkdir $WORKDIR/release [ ! -d $WORKDIR/repository ] && mkdir $WORKDIR/repository [ ! -d $WORKDIR/share ] && mkdir $WORKDIR/share # end directories structure touch $WORKDIR/.lock echo_g "Set directory structure successfully! "}function checkDependencies() { echo_b "Checking dependencies for deploy procedure. " # Refer: # if [ -z ${var+x} ]; then # echo "var is unset"; else echo "var is set to '$var'" # fi # if [ "$var x" = " x" ]; then # echo "var is empty"; else echo "var is set to '$var'" # fi # if [ -z $var ]; then # echo "var is empty"; else echo "var is set to '$var'" # fi if [[ -z $SOURCEURL ]]; then echo_r "Error: SOURCEURL is undefined! " exit 1 fi DISKSPACE=`df $WORKDIR | tail -n1 | awk '{print $(NF -2)}'` if [[ $DISKSPACE -lt 2097152 ]]; then echo_y "Warning: Disk space of $WORKDIR is smaller than 2GB" #exit 1 fi echo_g "All required dependencies check pass! "}function cleanOldReleases(){ save_days=${save_old_releases_for_days:-10} if [ ! -d $WORKDIR/release ]; then echo_b "Can NOT find release directory, skipping . " return fi need_clean=$(find $WORKDIR/release -mtime +$save_days -exec ls {} \;) if [ ! -z $need_clean ]; then echo_g "Expired releases found and will be removed from project! " find $WORKDIR/release -mtime +$save_days -exec rm -rf {} \; if [ $? -eq 0 ]; then echo_g "Expired releases have removed from project! " else echo_r "Can NOT remove expired releases, please alter to Admin users. " fi else echo_g "All releases are not expired, skipping. " fi}function deploy() { # check a directories lock, Note: this is redundant if [[ ! -f $WORKDIR/.lock ]]; then setDirectoryStructure fi cleanOldReleases checkDependencies # Make directory to release directory SOURCEDIR="$WORKDIR/release/$(date +%Y%m%d%H%M%S)" [ ! -d $SOURCEDIR ] && mkdir $SOURCEDIR # Get files from source code repository git clone $SOURCEURL $SOURCEDIR # svn co http://$SOURCEURL $WORKDIR/repository # TODO # get branchnames or revision numbers from VCS data # Remove .git or .svn [ -d $SOURCEDIR/.git ] && rm -rf $SOURCEDIR/.git [ -d $SOURCEDIR/.svn ] && rm -rf $SOURCEDIR/.svn # ifdef Complie # endif # Make source code symbolic link to current ( [ -f $WORKDIR/current ] || [ -d $WORKDIR/current ] ) && rm -rf $WORKDIR/current ln -s $SOURCEDIR $WORKDIR/current # Move conf and logs directies from release to share [ -d $WORKDIR/release/conf ] && mv $WORKDIR/release/conf $WORKDIR/share/conf [ -d $WORKDIR/release/logs ] && mv $WORKDIR/release/logs $WORKDIR/share/logs # Make conf and logs symbolic link to current [ -d $WORKDIR/share/conf ] && ln -s $WORKDIR/share/conf $WORKDIR/current/conf [ -d $WORKDIR/share/logs ] && ln -s $WORKDIR/share/logs $WORKDIR/current/logs # Start service or validate status if [[ -e $WORKDIR/current/bin/startup.sh ]]; then $WORKDIR/current/bin/startup.sh start RETVAL=$? else # TODO # external health check RETVAL=0 fi RETVAL=$? # if started ok, then create a workable program to a file if [[ $RETVAL -eq 0 ]]; then # Note cat with eof must start at row 0, and with eof end only, such as no blank spaces, etc cat >$WORKDIR/share/workable_program.log < 以上是"如何通过Git WebHooks+脚本实现自动更新发布代码之shell脚本"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
脚本
目录
问题
内容
篇文章
部分
代码
更新
不同
注释
需求
项目
颜色
检查
知名
不怎么
中学
信息
功能
后生
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发成本会计核算
基金项目开发用什么数据库
微软国内服务器
网络技术与应用图
数据库的数据清洗方法
ntp服务器离线部署
服务器 网站维护管理
dns服务器存在问题解决方法
网络安全内容 五年级
网络安全上市公司市值
成都房地产预售开盘数据库
怎么进入银行软件开发岗
数据库管理工作年薪多少
数据库和数据库技术
快译通下载软件开发
开学第一课网络安全教育平台
js 发送数据库
国家网络安全投入资金
软件开发一般包括
家用frp服务器
教师数据库实验报告
华为网络安全与用户隐私保护
工会国家网络安全周活动
非传统安全网络安全
软件开发需要满足
附加数据库 错误5123
大专工业软件开发技术
梧州软件开发
局域网服务器共享的文件夹看不见
服务器里不能解压缩