PHP项目自动打包部署操作指南

先上Shell脚本,后面再给大家讲解怎么配置Jenkins。

Github地址:https://github.com/TronGeek/AutoDeploy-Shell

从SVN配置库自动导出升级包的脚本

#!/bin/bash -e
################################################
#   Todo:自动从SVN配置库获取代码,导出差异文件。  
#   Author:归根落叶
#   Blog:http://www.ispenn.com             
################################################

#Function:  printLog()
#Author:    归根落叶
#Todo:  打印日志
#Param: logInfo(日志信息)
logPath="pwd/logs"     #日志存放路径
function printLog(){
    local errorCode=$?
    local logInfo=$1
    if [ ! -d ${logPath} ];then
        mkdir -p ${logPath}
    fi
    if [ $# -ne 1 ];then
        echo date +"%Y-%m-%d %H:%M:%S" "[ERROR] Usage:printLog logInfo" | tee --append ${logPath}/svnRuntimeLog-date +"%Y-%m-%d".txt
        exit 1
    fi
    if [ ${errorCode} -ne 0 ];then
        echo date +"%Y-%m-%d %H:%M:%S" "[ERROR] ${logInfo}" | tee --append ${logPath}/svnRuntimeLog-date +"%Y-%m-%d".txt
        return 1
    else
        echo date +"%Y-%m-%d %H:%M:%S" "${logInfo}" >> ${logPath}/svnRuntimeLog-date +"%Y-%m-%d".txt
    fi
}

#Function:  svnDo()
#Author:    归根落叶
#Todo:  操作SVN
#Param: userName(用户名)
#       passWord(密码)
#       operation[co(签出)|up(更新)|add(新增)|ci(提交)|export(导出)|copy(打分支|标签)|diff(对比两个版本)|info(获取信息)|log(打印日志)|gr(获取版本号)]
#       svnPath(路径1)
#       [ tagsPath(路径2)|localPath(路径3)
#       revision(版本)
#       logFile(日志文件)
#       getRevisionNum(获取倒数第几次更新的版本号) ]
function svnDo(){
    local userName=$1
    local passWord=$2
    local op=$3
    case ${op} in
        "co")
            if [ $# -ne 6 ];then
                echo "[ERROR] Usage:svnDo userName passWord co svnPath localPath revision"
                printLog "[ERROR] Usage:svnDo userName passWord co svnPath localPath revision"
                exit 1
            fi
            local svnPath=$4
            local localPath=$5
            local revision=$6
            echo "检出SVN[${svnPath}]"
            svn co --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" "${localPath}" --revision ${revision}
            printLog "检出SVN[${svnPath}]" || local errorCode=$?
            return ${errorCode};;
        "up")
            if [ $# -ne 5 ];then
                echo "[ERROR] Usage:svnDo userName passWord up localPath revision"
                printLog "[ERROR] Usage:svnDo userName passWord up localPath revision"
                exit 1
            fi
            local localPath=$4
            local revision=$5
            echo "更新SVN[${localPath}]"
            svn up --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${localPath}" --revision ${revision}
            printLog "更新SVN[${localPath}]" || local errorCode=$?
            return ${errorCode};;
        "add")
            if [ $# -ne 4 ];then
                echo "[ERROR] Usage:svnDo userName passWord add localPath"
                printLog "[ERROR] Usage:svnDo userName passWord add localPath"
                exit 1
            fi
            local localPath=$4
            echo "SVN新增文件[${localPath}]"
            svn add --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${localPath}"
            printLog "SVN新增文件[${localPath}]" || local errorCode=$?
            return ${errorCode};;
        "ci")
            if [ $# -ne 5 ];then
                echo "[ERROR] Usage:svnDo userName passWord ci localPath logFile"
                printLog "[ERROR] Usage:svnDo userName passWord ci localPath logFile"
                exit 1
            fi
            local localPath=$4
            local logFile=$5
            echo "提交到SVN[${localPath}]"
            svn ci --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${localPath}" --file "${logFile}"
            printLog "提交到SVN[${localPath}]" || local errorCode=$?
            return ${errorCode};;
        "export")
            if [ $# -ne 6 ];then
                echo "[ERROR] Usage:svnDo userName passWord export svnPath localPath revision"
                printLog "[ERROR] Usage:svnDo userName passWord export svnPath localPath revision"
                exit 1
            fi
            local svnPath=$4
            local localPath=$5
            local revision=$6
            if [ -f upList.txt ];then
                cat upList.txt |
                while read filePath; do
                    local exPath=${filePath%/*}/
                    if [[ (-d "${localPath}/${exPath}") && (! -d "${localPath}/${filePath}") ]];then
                        svn export --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}/${filePath}@" "${localPath}/${filePath}" --revision ${revision}
                        printLog "SVN导出文件[${svnPath}/${filePath}@${revision}]" || local errorCode=$? 
                    elif [ ! -d "${localPath}/${filePath}" ];then
                        mkdir -p "${localPath}/${exPath}"
                        printLog "创建目录[${localPath}/${exPath}]"
                        svn export --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}/${filePath}@" "${localPath}/${filePath}" --revision ${revision}
                        printLog "SVN导出文件[${svnPath}/${filePath}@${revision}]" || local errorCode=$? 
                    fi
                done
                rm -f upList.txt
            else
                printLog "没有文件需要更新,如果只是删除文件,下次更新会一起删除。" || local errorCode=$? 
            fi
            return ${errorCode};;
        "copy")
            if [ $# -ne 7 ];then
                echo "[ERROR] Usage:svnDo userName passWord copy svnPath tagsPath revision logFile"
                printLog "[ERROR] Usage:svnDo userName passWord copy svnPath tagsPath revision logFile"
                exit 1
            fi
            local svnPath=$4
            local tagsPath=$5
            local revision=$6
            local logFile=$7
            echo "SVN打分支/标签[${tagsPath}]"
            svn copy --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" "${tagsPath}" --revision ${revision} --file ${logFile}
            printLog "SVN打分支/标签[${tagsPath}]" || local errorCode=$?
            return ${errorCode};;
        "diff")
            if [ $# -ne 5 ];then
                echo "[ERROR] Usage:svnDo userName passWord diff svnPath revision"
                printLog "[ERROR] Usage:svnDo userName passWord diff svnPath revision"
                exit 1
            fi
            local svnPath=$4
            local revision=$5
            echo "SVN对比两个版本差异[${svnPath}@${revision}]"
            svn diff --force --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" --revision ${revision} --summarize > diff.txt
            printLog "SVN对比两个版本差异[${svnPath}@${revision}]" || local errorCode=$? 
            rm -f delList.txt
            rm -f upList.txt
            cat diff.txt |
            while read row; do
                local op=echo ${row} | awk '{print $1}'
                if [ ${op} == "D" ];then
                    echo ${row} | awk '{$1="";print $0}' | awk -F "${svnPath}" '{print $2}' >> delList.txt
                else
                    echo ${row} | awk '{$1="";print $0}' | awk -F "${svnPath}" '{print $2}' >> upList.txt
                fi        
            done
            echo "noneLine" >> delList.txt
            rm -f diff.txt
            return ${errorCode};;
        "info")
            if [ $# -ne 5 ];then
                echo "[ERROR] Usage:svnDo userName passWord info svnPath revision"
                printLog "[ERROR] Usage:svnDo userName passWord info svnPath revision"
                exit 1
            fi
            local tagsPath=$4
            local revision=$5
            echo "SVN获取[${tagsPath}]的信息"
            svn info --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${tagsPath}" --revision ${revision}
            printLog "SVN获取[${tagsPath}]的信息" || local errorCode=$?
            return ${errorCode};;
        "log")
            if [ $# -ne 5 ];then
                echo "[ERROR] Usage:svnDo userName passWord log svnPath revision"
                printLog "[ERROR] Usage:svnDo userName passWord log svnPath revision"
                exit 1
            fi
            local svnPath=$4
            local revision=$5
            svn log --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" --revision ${revision}
            printLog "SVN获取[${tagsPath}]的日志" || local errorCode=$?
            return ${errorCode};;
        "gr")
            if [ $# -ne 5 ];then
                echo "[ERROR] Usage:svnDo userName passWord gr svnPath getRevisionNum"
                printLog "[ERROR] Usage:svnDo userName passWord gr svnPath getRevisionNum"
                exit 1
            fi
            local svnPath=$4
            local getRevisionNum=$5
            svn log --non-interactive --trust-server-cert --username ${userName} --password ${passWord} "${svnPath}" | grep "^r[0-9]" | awk "NR==${getRevisionNum}" | sed -n -r "s/r([0-9]*).*/\1/p"
            printLog "SVN获取[${svnPath}]的版本号" || local errorCode=$?
            return ${errorCode};;
        *)
            echo "[ERROR] Usage:svnDo userName passWord operation"
            printLog "[ERROR] Usage:svnDo userName passWord operation"
            exit 1
    esac    
}

#Function:  editConf()
#Author:    归根落叶
#Todo:  修改配置文件
#Param: filePath(导出文件路径)
function editConf(){
    if [ $# -ne 1 ];then
        echo "[ERROR] Usage:editConf filePath"
        printLog "[ERROR] Usage:editConf filePath"
        exit 1
    fi
    local filePath=$1
    local file1="application/config/nosql.php"
    if [ -f ${filePath}/${file1} ];then
        echo "修改配置文件[${filePath}/${file1}]"
        sed -r -i "8,15s/mongodb:\/\/.*',/mongodb:\/\/127.0.0.1:27017',/" ${filePath}/${file1}
        sed -r -i "8,15s/'database'.*,/'database'   =>  'testdb',/" ${filePath}/${file1}
        printLog "修改配置文件[${filePath}/${file1}]"
    fi
}

#Function:  jsGrunt()
#Author:    归根落叶
#Todo:  Grunt自动压缩js、css、图片
#Param: filePath(源文件路径),localPath(SVN本地工作目录)
function jsGrunt(){
    if [ $# -ne 2 ];then
        echo "[ERROR] Usage:jsGrunt filePath localPath"
        printLog "[ERROR] Usage:jsGrunt filePath localPath"
        exit 1
    fi
    local filePath=$1 
    local localPath=$2
    local ver=date +"%Y%m%d%H%M%S"
    local workDir=pwd
    local modulesPath="/usr/local/lib/node_modules/LiveApp"
    if [[ (-d ${filePath}) && (ls ${filePath} | wc -l -gt 0) ]];then
        ls ${filePath} > tempdir.txt
        cat tempdir.txt |
        while read tdir;do
            local jsnum=find ${filePath}/${tdir} -name '*.js' | wc -l
            local cssnum=find ${filePath}/${tdir} -name '*.css' | wc -l
            if [[ ${jsnum} -gt 0 || ${cssnum} -gt 0 ]];then
                if [[ (-f ${localPath}/${tdir}/package.json) && (-f ${localPath}/${tdir}/Gruntfile.js) ]];then
                    rm -f ${localPath}/${tdir}/node_modules
                    ln -s ${modulesPath} ${localPath}/${tdir}/node_modules
                    cd ${localPath}/${tdir}
                    echo "Grunt压缩模板${tdir}的js、css和图片"
                    grunt 
                    printLog "Grunt压缩模板${tdir}的js、css和图片" 
                    if [ -d ./assets ];then
                        if [ -f ./app/tpl.php ];then
                            sed -r -i "s/init.min.js\?ver=[0-9\.]*\"/init.min.js\?ver=${ver}\"/g" ./app/tpl.php 
                            sed -r -i "s/app.min.css\?ver=[0-9\.]*\"/app.min.css\?ver=${ver}\"/g" ./app/tpl.php
                            cp -rf ./app/ ${workDir}/${filePath}/${tdir}/app/
                            printLog "自动更新版本号[./app/tpl.php]"
                        fi
                        cp -rf ./assets ${workDir}/${filePath}/${tdir}/
                        if [ -f ./assets/scripts/init.min.js ];then
                            sed -r -i "18,20s/\/\/.*preload/preload/" ./assets/scripts/init.min.js 
                            sed -r -i "18,20s/[^\/\/]base/\/\/base/" ./assets/scripts/init.min.js 
                            cp -f ./assets/scripts/init.min.js ${workDir}/${filePath}/${tdir}/assets/scripts/init.min.js 
                            printLog "自动切换到正式环境[./assets/scripts/init.min.js]"
                        fi
                    else
                        if [ -f ./tpl.php ];then
                            sed -r -i "s/init.min.js\?ver=[0-9\.]*\"/init.min.js\?ver=${ver}\"/g" ./tpl.php 
                            sed -r -i "s/app.min.css\?ver=[0-9\.]*\"/app.min.css\?ver=${ver}\"/g" ./tpl.php
                            cp -f ./tpl.php ${workDir}/${filePath}/${tdir}/tpl.php
                            printLog "自动更新版本号[./tpl.php]"
                        fi
                        cp -rf ./dist ${workDir}/${filePath}/${tdir}/ 
                        if [ -f ./dist/scripts/init.min.js ];then
                            sed -r -i "18,20s/\/\/.*preload/preload/" ./dist/scripts/init.min.js 
                            sed -r -i "18,20s/[^\/\/]base/\/\/base/" ./dist/scripts/init.min.js
                            cp -f ./dist/scripts/init.min.js ${workDir}/${filePath}/${tdir}/dist/scripts/init.min.js
                            printLog "自动切换到正式环境[./dist/scripts/init.min.js]" 
                        fi
                    fi
                    rm -f ./node_modules
                    ##################### 提交压缩文件到SVN开始 非正式环境不需要可删除 #####################
                    svnDo ${userName} ${passWord} add "pwd" 
                    echo "[Auto]模板${tdir} 压缩js、css和图片" > svnLog.txt
                    svnDo ${userName} ${passWord} ci "pwd" svnLog.txt 
                    rm -f svnLog.txt
                    ##################### 提交压缩文件到SVN结束 非正式环境不需要可删除 #####################
                    cd ${workDir}
                fi
            fi
        done
        rm -f tempdir.txt
        rm -rfv find ${filePath}/ -name '.svn' -type d
    fi
}

#SVN配置库代码路径
svnPath="https://hostname/svn/project/trunk"    
#Tag路径                              
tagsPath="https://hostname/svn/project/tags/release"    
#svn用户名                                 
userName="userName"     
#svn密码                              
passWord="passWord" 
#环境(test|pre|PRO)                                   
env="test"      

oldVersion=svnDo ${userName} ${passWord} gr ${svnPath} 2
printLog "获取上一次更新的版本号"
newVersion=svnDo ${userName} ${passWord} gr ${svnPath} 1
printLog "获取最新的版本号"
newTagsVersion=svnDo ${userName} ${passWord} gr ${tagsPath} 1 #获取tag版本号,如果不需要打标签,需要删除
if [[ (${oldVersion} -gt 0) && (${newVersion} -gt 0) ]];then    #判断是否是数字
    if [ -f tagsVersion ];then
        oldVersion=cat tagsVersion && [[ ${oldVersion} -gt 0 ]] || printLog "获取上次更新的版本号出错"
    fi  
    if [[ ${oldVersion} -eq ${newVersion} ]];then
        echo "没有新版本更新,目前新版本号为[${newVersion}]"
        printLog "没有新版本更新,目前新版本号为[${newVersion}]" && exit 1
    fi
    updir="ali_upgrade/${oldVersion}-${newVersion}"     #升级包导出路径
    downdir="ali_downgrade/${newVersion}-${oldVersion}" #还原包导出路径
    echo "从版本[${oldVersion}]升级到新版本[${newVersion}]"
    printLog "从版本[${oldVersion}]升级到新版本[${newVersion}]"
    ##################### 打SVN标签开始 非正式环境不需要可删除 #####################
    ##################### 下面这行代码为检查是否存在标签,强制先更新预发布环境,非正式环境可删除 #####################
    svnDo ${userName} ${passWord} info ${tagsPath}/tag_${newVersion} "HEAD" 
    echo "auto_tags:生产环境打包,SVN版本号[${newVersion}]" > svnLog.txt
    svnDo ${userName} ${passWord} log ${svnPath} ${oldVersion}:${newVersion} >> svnLog.txt
    if [[ ${newVersion} -gt ${newTagsVersion} ]];then
        svnDo ${userName} ${passWord} copy ${svnPath} ${tagsPath}/tag_${newVersion} ${newVersion} svnLog.txt 
    fi
    rm -f svnLog.txt
    ##################### 打SVN标签结束 非正式环境不需要可删除 #####################
    printLog "删除临时文件"
    rm -rf ${updir}
    rm -rf ${downdir}
    mkdir -p ${updir}
    mkdir -p ${downdir}
    printLog "升级,导出版本差异文件"
    svnDo ${userName} ${passWord} diff ${svnPath} "${oldVersion}:${newVersion}" 
    svnDo ${userName} ${passWord} export ${svnPath} ${updir} "HEAD" 
    editConf ${updir} 
    mv delList.txt delList${env}Up.txt
    ##################### 自动压缩开始 不需要可删除 #####################
    jsGrunt "${updir}/template" "./LiveAPP/template" 
    ##################### 自动压缩结束 不需要可删除 #####################
    cp -rfv ${updir}/ upgrade${env}_${oldVersion}-${newVersion}/
    tar zcvf upgrade${env}.tar.gz upgrade${env}_${oldVersion}-${newVersion}/
    tar zcvf ali_upgrade/upgrade_${oldVersion}-${newVersion}.tar.gz upgrade${env}_${oldVersion}-${newVersion}/
    rm -rf upgrade${env}_${oldVersion}-${newVersion}
    printLog "打升级包完成"
    ##################### 打还原包开始 不需要可删除 #####################
    printLog "还原,导出版本差异文件"
    svnDo ${userName} ${passWord} diff ${svnPath} "${newVersion}:${oldVersion}" 
    svnDo ${userName} ${passWord} export ${tagsPath}/tag_${oldVersion} ${downdir} "HEAD" 
    editConf ${downdir} 
    mv delList.txt delList${env}Down.txt
    cp -rfv ${downdir}/ downgrade${env}_${newVersion}-${oldVersion}/
    tar zcvf downgrade${env}.tar.gz downgrade${env}_${newVersion}-${oldVersion}/
    rm -rf downgrade${env}_${newVersion}-${oldVersion}
    printLog "打还原包完成"
    ##################### 打还原包结束 不需要可删除 #####################
    ##################### 打SVN标签开始 非正式环境不需要可删除 #####################
    newVersion=svnDo ${userName} ${passWord} gr ${svnPath} 1
    newTagsVersion=svnDo ${userName} ${passWord} gr ${tagsPath} 1
    echo "auto_tags:生产环境打包,SVN版本号[${newVersion}]" > svnLog.txt
    svnDo ${userName} ${passWord} log ${svnPath} ${oldVersion}:${newVersion} >> svnLog.txt
    if [[ ${newVersion} -gt ${newTagsVersion} ]];then
        svnDo ${userName} ${passWord} copy ${svnPath} ${tagsPath}/tag_${newVersion} ${newVersion} svnLog.txt 
    fi  
    rm -f svnLog.txt
    ##################### 打SVN标签结束 非正式环境不需要可删除 #####################
    echo ${newVersion} > tagsVersion
    exit 0
else
    printLog "版本号不正确:OldVersion#${oldVersion}  NewVersion#${newVersion}" && exit 1
fi

需要在服务器端执行的脚本

#!/bin/bash -e
################################################
#   Todo:自动部署项目代码。  
#   Author:归根落叶
#   Blog:http://www.ispenn.com             
################################################

#Function:  printLog()
#Author:    归根落叶
#Todo:  打印日志
#Param: logInfo(日志信息)
logPath="pwd/logs"
function printLog(){
    local errorCode=$?
    local logInfo=$1
    if [ ! -d ${logPath} ];then
        mkdir -p ${logPath}
    fi
    if [ $# -ne 1 ];then
        echo date +"%Y-%m-%d %H:%M:%S" "[ERROR] Usage:printLog logInfo" | tee --append ${logPath}/svnRuntimeLog-date +"%Y-%m-%d".txt
        exit 1
    fi
    if [ ${errorCode} -ne 0 ];then
        echo date +"%Y-%m-%d %H:%M:%S" "[ERROR] ${logInfo}" | tee --append ${logPath}/svnRuntimeLog-date +"%Y-%m-%d".txt
        return 1
    else
        echo date +"%Y-%m-%d %H:%M:%S" "${logInfo}" >> ${logPath}/svnRuntimeLog-date +"%Y-%m-%d".txt
    fi
}

#Function:  backup()
#Author:    归根落叶
#Todo:  备份部署包
#Param: fileName(备份文件名),backupPath(备份路径)
function backup(){
    if [ $# -ne 2 ];then
        echo "[ERROR] Usage:backup fileName backupPath"
        printLog "[ERROR] Usage:backup fileName backupPath"
        exit 1
    fi
    local fileName=$1
    local backupPath=$2
    local bakDate=date +'%Y%m%d'
    local bakTime=date +'%H%M'
    local delTime=date -d -7day +'%Y%m%d'
    echo "备份文件[${fileName}]至[${backupPath}/${bakDate}/${bakTime}-${fileName}]"
    if [ -d ${backupPath}/${bakDate} ];then
        mv ${fileName} ${backupPath}/${bakDate}/${bakTime}-${fileName}
        printLog "备份文件[${fileName}]至[${backupPath}/${bakDate}/${bakTime}-${fileName}]" 
    else
        mkdir -p ${backupPath}/${bakDate}
        mv ${fileName} ${backupPath}/${bakDate}/${bakTime}-${fileName}
        printLog "备份文件[${fileName}]至[${backupPath}/${bakDate}/${bakTime}-${fileName}]" 
    fi
    echo "删除7天前的备份文件[${backupPath}/${delTime}]"
    rm -rf ${backupPath}/${delTime}    
    printLog "删除7天前的备份文件[${backupPath}/${delTime}]"
}

#Function:  deploy()
#Author:    归根落叶
#Todo:  部署项目
#Param: packageFile(.tar.gz部署包名),delFile(删除文件列表),projectPath(项目路径)
function deploy(){
    if [ $# -ne 3 ];then
        echo "[ERROR] Usage:deploy packageFile delFile projectPath"
        printLog "[ERROR] Usage:deploy packageFile delFile projectPath"
        exit 1
    fi
    local packageFile=$1
    local delFile=$2
    local projectPath=$3
    if [ -f ${packageFile}.tar.gz ];then
        tar zxvf ${packageFile}.tar.gz
        if [ -f ${delFile} ];then
            cat ${delFile} |
            while read row; do
                if [ "${row}" == "noneLine" ];then
                    exit
                elif [ "${row}" != "" ];then 
                    rm -rfv ${projectPath}/${row}
                    printLog "删除 ${projectPath}/${row}"
                fi
            done
        fi
        echo "部署升级包[${packageFile}_*/]至[${projectPath}]"
        chown -R www.www ${packageFile}_*/
        printLog "更改[${packageFile}_*/]权限为www.www" 
        \cp -rfv ${packageFile}_*/* ${projectPath}
        printLog "部署升级包[${packageFile}_*/]至[${projectPath}]" 
        rm -rf ${packageFile}_*
        printLog "删除升级包[${packageFile}_*]"
    else
        printLog "升级包[${packageFile}.tar.gz]不存在!" && exit 1
    fi
}

#Function:  updateSql()
#Author:    归根落叶
#Todo:  更新数据库
#Param: host(数据库主机地址),username(数据库用户名),password(数据库密码),dbname(数据库名),sqlFile(数据库sql文件)
function updateSql(){
    if [ $# -ne 5 ];then
        echo "[ERROR] Usage:updateSql host username password dbname sqlFile"
        printLog "[ERROR] Usage:updateSql host username password dbname sqlFile"
        exit 1
    fi
    local host=$1
    local username=$2
    local password=$3
    local dbname=$4
    local sqlFile=$5
    if [ -f ${sqlFile} ];then
        local row=cat ${sqlFile}
        if [ "${row}" != "noneLine" ];then
            mysql -u"${username}" -p"${password}" -h"${host}" --default-character-set=utf8 ${dbname} < ${sqlFile}
            printLog "自动更新数据库[${host}:${dbname}]" 
        fi
    fi 
}

#数据库主机地址
host="127.0.0.1"
#数据库用户名                                 
username="username"     
#数据库密码                              
password="password"
#数据库名
dbname="testdb"
#数据库sql文件
sqlFile="auto.sql"
#环境(test|pre|PRO)                                   
env="test"
#服务器部署绝对路径                                  
envURL="/website/html"  

cd /home/www/   #根据升级包上传到服务器的目录而定
deploy "upgrade${env}" "delList${env}Up.txt" "${envURL}" 
updateSql "${host}" "${username}" "${password}" "${dbname}" "${sqlFile}"    #更新数据库,不需要可删除 
##################### 备份开始 非正式环境不需要可删除 #####################
backup "upgrade${env}.tar.gz" "/home/www/backup"
backup "delList${env}Up.txt" "/home/www/backup" 
backup "downgrade${env}.tar.gz" "/home/www/backup" 
backup "delList${env}Down.txt" "/home/www/backup" 
##################### 备份结束 非正式环境不需要可删除 #####################  

配置Jenkins

  1. 新建一个项目
    配置两个参数,方便以后快速复制构建项目。

  2. 配置SVN配置库地址
  3. 导出升级包的脚本配置
    增加构建步骤,选择“Execute shell”

    将svnExport.sh脚本中的代码复制粘贴到文本框中

    删除脚本后面的两行代码,因为已经用Jenkins参数化,在构建过程中会自动替换掉,不需要再单独赋值。
    “`shell
    #环境(test|pre|PRO)
    env="test"
    “`
    将其他参数替换为真实环境对应的值。

    脚本包含了自动压缩js的功能,如果不需要自动压缩js,可将这部分代码去掉:
    “`shell
    jsGrunt "${updir}/template" "./LiveAPP/template" || exit 1
    “`
    如果需要自动替换修改配置文件,请修改脚本中的函数“editConf”:
    “`shell
    function editConf(){
    if [ $# -ne 1 ];then
    printLog "[ERROR] Usage:editConf filePath"
    exit 1
    fi
    local filePath=$1
    local file1="application/config/nosql.php"
    if [ -f ${filePath}/${file1} ];then
    echo "修改配置文件[${filePath}/${file1}]"
    sed -r -i "8,15s/mongodb:\/\/.*',/mongodb:\/\/127.0.0.1:27017',/" ${filePath}/${file1}
    sed -r -i "8,15s/'database'.*,/'database' => 'testdb',/" ${filePath}/${file1}
    printLog "修改配置文件[${filePath}/${file1}]"
    fi
    }
    “`
    将需要修改的文件和需要替换的内容配置好即可。

  4. 服务器端部署脚本配置
    增加构建步骤,选择“Send files or execute commands over SSH”,SSH插件需要事先配置好。

    选择配置好的服务器地址,在“Exec command”中输入:
    “`shell
    rm -f ./delList${env}Up.txt
    rm -f ./delList${env}Down.txt
    rm -f ./upgrade${env}.tar.gz
    rm -f ./downgrade${env}.tar.gz
    “`
    删除“升级包”、“还原包”和“需要删除的文件列表文件。

    再新建一个“Transfer Set”,在“Source files”中填写:
    “`shell
    upgrade${env}.tar.gz,delList${env}Up.txt,downgrade${env}.tar.gz,delList${env}Down.txt
    “`

    这些是需要传输的文件,在“Exec command”中将autoDeploy.sh脚本中的代码复制粘贴到文本框中。

    注意修改脚本后面的参数,改成真实环境的路径,删除下面两个参数:
    “`shell
    #环境(test|pre|PRO)
    env="test"
    #服务器部署绝对路径
    envURL="/website/html"
    “`
    因为已经用Jenkins参数化,在构建过程中会自动替换掉,不需要再单独赋值。

    如果不需要自动更新数据库,可将下面的代码行删除:
    “`shell
    updateSql "${host}" "${username}" "${password}" "${dbname}" "${sqlFile}" || exit 1
    “`

  5. 清理代码配置
    增加构建步骤,选择“Execute shell”,在文本框中输入:

    “`shell
    rm -f delList${env}Up.txt
    rm -f delList${env}Down.txt
    rm -f upgrade${env}.tar.gz
    rm -f downgrade${env}.tar.gz
    rm -rf ali_upgrade/*-*
    rm -rf ali_downgrade/*-*
    “`

    用于清理生成的临时文件,至此,一个自动部署的工程就配置好了。