</pre>基于目前业务的版本,使用的maven 及tomcat <p></p><p>如果我们使用 Jenkins 发布是比较好的,但是存在一定的问题,就是需要学习时间,</p><p>基于我们的项目,我使用python 自动构建发布环境</p><p>脚本如下:</p><p></p><pre code_snippet_id="498203" snippet_file_name="blog_20141027_2_7484436" name="code" class="python">#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import ConfigParser
homedir = os.getcwd()
print homedir
config = ConfigParser.ConfigParser()
config.read("config.propties")
user_name = config.get("SVN", "user_name").decode('utf8').encode('cp936') 
user_password = config.get("SVN", "user_password").decode('utf8').encode('cp936') 
check_out_dir = homedir
log_name = config.get("SVN", "log_name").decode('utf8').encode('cp936') 
script_dir = homedir
pkg_name=homedir+os.path.sep+"mabaobang-web"+os.path.sep+"target"+os.path.sep+"mabaobang-web.war"
rmote_ip=config.get("SVN", "rmote_ip")
rmote_user=config.get("SVN", "rmote_user")
rmote_passwd=config.get("SVN", "rmote_passwd")
print "pkg_name",pkg_name
script_dir=config.get("SVN", "script_dir").decode('utf8').encode('cp936')
command = "svn log -v --xml " + check_out_dir + " > " + log_name + " --username  " + user_name + " --password " + user_password
os.system( command )
os.chdir(check_out_dir)
print os.path.abspath(".").decode('cp936').encode('utf8')
command1="svn update "+ check_out_dir + " --username  " + user_name + " --password " + user_password +" --no-auth-cache --non-interactive --trust-server-cert"
os.system( command1 )
os.system("mvn clean:clean package");
file_object = open('123123.txt', 'w')
file_object.write("open 192.168.15.234\n")
file_object.writelines("put "+pkg_name+"\n")
#file_object.writelines(file.newlines)
file_object.writelines("bye\n")
#file_object.writelines(file.newlines)
#file_object.writelines(file.newlines)
file_object.flush()
file_object.close()

if  os.path.exists(pkg_name):
    os.chdir(homedir)
    try:
        command_sftp="ftp.exe -l "+ rmote_user +" -pw "+rmote_passwd+" -b 123123.txt "
        os.system( command_sftp )
    except Exception,e:
        print  "done"
    finally:
        command_ssh="ssh.exe -ssh -pw "+ rmote_passwd +"  "+rmote_passwd+ "@192.168.15.234 '/home/momabo/deploy.sh' "
        os.system( command_ssh )
    

配置文件如下:


[SVN]
user_name=111111
user_password=000000
#check_out_dir=D:\11111\1111-parent
log_name=E:\log.log
#out_put_dir=D:\1111\111-parent
#war_name=D:\11111111-parent\11111111111111-web\target\111111111-web.war
rmote_ip=192.168.15.234
rmote_user=xxxx
需要二个开源软sftp.exe  http://the.earth.li/~sgtatham/putty/0.63/x86/psftp.exe

和 ssh.exe  http://the.earth.li/~sgtatham/putty/0.63/x86/plink.exe 进行下载后就可以了 


自动化发布后台脚本

!/bin/bash 
###?碌?梅?猫?陋掳????娄碌?war路?碌陆录??驴?录???忙
###碌??禄虏陆拢潞录矛虏茅???娄碌?掳眉??路帽??麓芦碌陆录??驴?录??拢潞
USER_HOME=/home/3213123
TOMCAT_HOME=${USER_HOME}/tomcat
#?娄??碌??驴?录
APP_HOME=${TOMCAT_HOME}/webapps/12313123-web
CONF_FILE=${APP_HOME}/WEB-INF/classes/config.properties
IP_ADD=127.0.0.1
MYSQL_USER_NAME=root
MYSQL_USER_PASSWD=213131
UPLOAD_DIR=/home/312312312/upload


###碌??禄虏陆拢潞录矛虏茅???娄碌?掳眉??路帽??麓芦碌陆录??驴?录??拢潞
if [ ! -f ${USER_HOME}/mabaobang-web.war ];then
    echo -e "\033[31m ERROR:Pkg ${USER_HOME}/mabaobang-web.war is not  exist. \033[0m"
    exit 1
fi
####碌?露镁虏陆?拢?鹿路镁?帽
${TOMCAT_HOME}/bin/shutdown.sh
####?楼鲁媒?禄虏驴路?碌?????
if [ ! -d ${APP_HOME} ];then
    echo -e "\033[31m ERROR:Dir ${APP_HOME} is not  exist. \033[0m"
    exit 1
else
   rm -rf ${APP_HOME}/*
fi


mv ${USER_HOME}/1312313-web.war ${APP_HOME}/
cd ${TOMCAT_HOME}/webapps/m312321g-web
jar -xvf ${APP_HOME}/3132133-web.war


##??赂??盲????录镁
dos2unix  ${CONF_FILE}


##??赂??盲????录镁
sed -i "s/\/\/127.0.0.1/\/\/${IP_ADD}/g" ${CONF_FILE}
sed -i "s/^[ ]*jdbc_username=.*/jdbc_username=${MYSQL_USER_NAME}/g" ${CONF_FILE}
sed -i "s/^[ ]*jdbc_password=.*/jdbc_password=${MYSQL_USER_PASSWD}/g" ${CONF_FILE}
                                                                                          




Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐