shell方式来编译发布java项目
规范化Jenkins,编译服务器编译目录设定为:
编译脚本目录: bin
发布脚本目录: deploy
编译代码目录: source
[aideploy@java-jenkins crmck]$ ll
total 12
drwxr-xr-x 2 aideploy aigrp 4096 Feb 13 15:59 bin
drwxr-xr-x 12 aideploy aigrp 4096 May 11 14:53 deploy
drwxr-xr-x 3 aideploy aigrp 4096 May 12 15:25 source
进入bin目录创建对应的编译脚本:
针对多个微服务java-jar包,采用一个简单的for循环遍历当前目录的dmmclist内的所有代码目录
编译前先rm删除旧代码(保证每次更新拉取的是最新的代码)
进入对应的遍历目录,开始执行一个copy对应环境变量参数文件的动作打包进jar包
打完jar包执行一个copy到对应的deploy的环境变量目录
至此编译简单的shell脚本配置完毕
[aideploy@java-jenkins crmck]$ cd bin/
[aideploy@java-jenkins bin]$ cat build-crmkc.sh
#!/bin/bash
svnpath=/app/aideploy/work/crmck/source
deploypath=/app/aideploy/work/crmck/deploy
#env=dev2yw
dmmc=`cat ./dmmclist`
echo "##########开始删除旧代码#####################"
rm -rf $svnpath/*
echo "##########开始更新代码#####################"
cd $svnpath/
svn co https://192.168.228.46:18080/svn/CRM_CODE/trunk/crm-v0.3-trunk-dev/crmkc
echo "##########开始install 安装 kc_common_sso_msa-master代码#####################"
cd $svnpath/crmkc/kc_common_sso_msa-master/
mvn clean install -Dmaven.test.skip=true
echo "##########开始将指定环境变量 $env 配置文件更新到代码/src/main/resources/目录下#####################"
for dm in $dmmc
do
cp -rf $svnpath/crmkc/environment/$env/* $svnpath/crmkc/$dm/src/main/resources/
echo "##########开始进入代码 "$dm" 目录更新代码#####################"
cd $svnpath/crmkc/$dm
echo "##########开始build "$dm" 编译更新代码#####################"
mvn clean package -Dmaven.test.skip=true
if [ $? -eq 0 ]
then
echo "the $dm is build successfully"
else
echo "the $dm is build failed,please check......build...log..........."
exit 1;
fi
done
cd $svnpath/crmkc/
mkdir -p $deploypath/$env/
\cp -rvf */target/*.jar $deploypath/$env/
echo "##########代码编译成功#####################"
echo "##########请检查deploy发布目录 $deploypath 是否编译成功#####################"
[aideploy@java-jenkins bin]$ cat dmmclist
kc_tmplt_tmpltsvc_msa-master
kc_tmplt_catalogsvc_msa-master
kc_tmplt_atomsvc_msa-master
kc_task_job_msa-master
kc_sensitive_wordsvc_msa-master
kc_search_platform_msa-master
kc_search_appsvc_msa-master
kc_manage_wordsvc_msa-master
kc_manage_tagsvc_msa-master
kc_manage_distsvc_msa-master
kc_manage_cust_msa-master
kc_doc_multimediasvc_msa-master
kc_doc_managesvc_msa-master
kc_doc_favorite_msa-master
kc_doc_detailsvc_msa-master
kc_doc_catalogsvc_msa-master
kc_common_staticdata_msa-master
kc_common_indexsvc_msa-master
kc_comment_feedback_msa
kc_base-master
[aideploy@java-jenkins bin]$
配置发布脚本
进入发布目录deploy:
由于是微服务jar包服务,可以通过Jenkins的 ssh插件
或者简单的免密动作scp到对应部署服务器即可
或者还可以使用sshpass工具带着密码直接推送到对应服务器
这两个脚本均可以写入Jenkins的shell窗口内
为了方便简洁查看就将对应的脚本文件放在编译服务器内啦
至此发布脚本配置完成
[aideploy@java-jenkins deploy]$ ls
bak deploy-crmkc.sh dev2yw prod2sd sit2bx sit2sd sit2zq
crmck-218.tar.gz deploy-crmkc.shbak prod2bx prod2yw sit2jt sit2yw
[aideploy@java-jenkins deploy]$ ls dev2yw/
kc_base-1.0.0.jar kc_doc_favorite_msa-1.0.0.jar kc_search_platform_msa-1.0.0.jar
kc_comment_feedback_msa-1.0.0.jar kc_doc_managesvc_msa-1.0.0.jar kc_sensitive_wordsvc_msa-1.0.0.jar
kc_common_indexsvc_msa-1.0.0.jar kc_doc_multimediasvc_msa-1.0.0.jar kc_task_job_msa-1.0.0.jar
kc_common_sso_msa-1.3.7.jar kc_manage_cust_msa-1.0.0.jar kc_tmplt_atomsvc_msa-1.0.0.jar
kc_common_sso_msa-1.3.7-sources.jar kc_manage_distsvc_msa-1.0.0.jar kc_tmplt_catalogsvc_msa-1.0.0.jar
kc_common_staticdata_msa-1.0.0.jar kc_manage_tagsvc_msa-1.0.0.jar kc_tmplt_tmpltsvc_msa-1.0.0.jar
kc_doc_catalogsvc_msa-1.0.0.jar kc_manage_wordsvc_msa-1.0.0.jar
kc_doc_detailsvc_msa-1.0.0.jar kc_search_appsvc_msa-1.0.0.jar
[aideploy@java-jenkins deploy]$
[aideploy@java-jenkins deploy]$ cat deploy-crmkc.sh
*******......******
......
crmkc5ip=192.168.229.9
ml5='/data/crmkc'
xmml5='/app/aideploy/work/crmck/deploy'
bak5='/data/crmkc/crmkcbak/'
echo "将文件包发送到$crmkc5ip 的$ml5 目录下"
echo "将"$xmml5"目录下的对应jar包发送到目标root@"$crmkc5ip"服务器"
#ssh -p 22 root@"$crmkc4ip" " \cp -r "$ml4"/*.jar "$bak4""
#传输需要的部署jar包
scp -P 22 -r "$xmml5"/$env/kc_doc_catalogsvc_msa-1.0.0.jar root@"$crmkc5ip":"$ml5"
scp -P 22 -r "$xmml5"/$env/kc_doc_multimediasvc_msa-1.0.0.jar root@"$crmkc5ip":"$ml5"
scp -P 22 -r "$xmml5"/$env/kc_manage_wordsvc_msa-1.0.0.jar root@"$crmkc5ip":"$ml5"
scp -P 22 -r "$xmml5"/$env/kc_task_job_msa-1.0.0.jar root@"$crmkc5ip":"$ml5"
scp -P 22 -r /app/aideploy/work/crmck/source/crmkc/environment/$env/* root@"$crmkc5ip":"$ml5"
ssh -p 22 root@192.168.229.9 "cd /data/crmkc/ && sh start.sh>nohup.out"
echo "################################ root@"$crmkc5ip" 服务器发布成功"
查看Jenkins-shell-java配置job
build编译任务配置:
deploy发布任务配置:
python方式来编译发布java项目
由于java项目过多,考虑使用python脚本来进行编译发布
调用json文件,通过json文件来统一管理多个java项目
在bin目录下创建一个config目录
python脚本通过读取./config下的对应json文件
来获取对应的代码仓库路劲以及编译代码目录来进行编译
至此编译python脚本配置完毕
[aideploy@java-jenkins bin]$ cat build.py
# -*- coding: utf-8 -*-
# @Author : pengge
# @Time : 2018/4/28 16:51
import subprocess
import json
import sys
def get_build_info(config_name):
with open('config/' + config_name, 'r', encoding='utf-8') as f:
content = f.read()
return json.loads(content)
def build_war(build_info, env_name):
"""
根据参数进行构建
:param build_info: 构建信息
:param env_name: 环境名
:return:
"""
print('[info]开始编译!')
svn_path = build_info['svn_path']
deploy_path = build_info['deploy_path'] + '/' + env_name
war_info_list = build_info['war_info_list']
mkdir_cmd = 'mkdir -p %s' % deploy_path
build_cmd = 'cd %s;mvn clean package -P%s -Dmaven.test.skip=true' % (svn_path, env_name)
# 解决一个pom文件对应多个项目问题
cp_cmd_list = []
for war_info in war_info_list:
war_path = war_info['war_path']
dist_war_name = war_info['dist_war_name']
cp_cmd_list.append('cp %s %s/%s' % (war_path, deploy_path, dist_war_name))
# 编译代码
print(build_cmd)
build_response = subprocess.getstatusoutput(build_cmd)
if build_response[0] is 0:
if build_response[1].count('failure') > 1:
print('[error]编译失败:\n%s' % build_response[1])
exit(1)
# 检查环境参数是否存在
elif build_response[1].count('it does not exist.') >= 1:
print('[error]编译失败:\n%s' % build_response[1])
print('[error]编译失败:请检查 %s 是否存在!\n' % env_name)
exit(1)
else:
print('[info]编译成功\n')
else:
print('[error]编译失败:\n%s' % build_response[1])
exit(1)
# 根据env_name创建发布目录
mkdir_response = subprocess.getstatusoutput(mkdir_cmd)
if mkdir_response[0] is 0:
print('[info]创建部署目录:\n%s\n' % mkdir_cmd)
else:
print('[error]创建部署目录失败:\n%s\n%s' % (mkdir_cmd, mkdir_response[1]))
exit(1)
# 拷贝代码到发布目录
for cp_cmd in cp_cmd_list:
cp_response = subprocess.getstatusoutput(cp_cmd)
if cp_response[0] is 0:
print('[info]拷贝编译机包到部署目录:\n%s\n' % cp_cmd)
else:
print('[error]拷贝编译包失败:\n%s\n%s' % (cp_cmd, cp_response[1]))
exit(1)
print('[info]编译结束!')
def build_jar(build_info):
print('[info]开始编译!')
svn_path = build_info['svn_path']
maven_cmd = build_info['maven_cmd']
build_cmd = 'cd %s; %s' % (svn_path, maven_cmd)
# 编译代码
print(build_cmd)
build_response = subprocess.getstatusoutput(build_cmd)
if build_response[0] is 0:
if build_response[1].count('failure') > 1:
print('[info]编译成功\n')
else:
print('[error]编译失败:\n%s' % build_response[1])
exit(1)
print('[info]编译结束!')
def svn_checkout(url, path):
username = 'aideploy'
password = 'Aideploy6'
clean_cmd = 'rm -rf %s' % path
checkout_cmd = 'svn co --force -r HEAD --username %s --password %s %s %s' % (username, password, url, path)
print('[info]开始更新代码!')
# 重建主机SVN目录
print(clean_cmd)
clean_response = subprocess.getstatusoutput(clean_cmd)
if clean_response[0] is 0:
if clean_response[1].count('failure') > 1:
print('[info]清理SVN目录成功\n')
else:
print('[error]清理SVN目录失败:\n%s' % clean_response[1])
exit(1)
# 下载代码
print('svn checkout %s %s' % (url, path))
checkout_response = subprocess.getstatusoutput(checkout_cmd)
if checkout_response[0] is 0:
if checkout_response[1].count('failure') > 1:
print('[info]checkout代码成功\n')
else:
print('[error]checkout代码失败:\n%s' % checkout_response[1])
exit(1)
print('[info]更新结束!\n')
def get_params(params):
config = params['config']
env_name = params['env_name']
app_name = params['app_name']
if len(params) != 3:
print('[error]:参数数量不对!')
get_help()
if config == '' or env_name == '' or app_name == '':
print('[error]:参数不可为空!')
get_help()
def get_help():
help_message = """
编译工具使用说明:
${PYTHON_HOME}/bin/python3 build.py '{"config":"config.json","env_name":"env_name","app_name":"app_name"}'
config : config目录下编译模块的json文件。如:cs.json(不可为空)
env_name: 环境名称。(不可为空)
app_name: 需要编译的模块名称,对应json文件中的app_name或者all。
【注意】:
1.参数中不可以有空格。
2.全量编译app_name为all即可。
"""
print(help_message)
def main(config, env_name, app_name):
build_info_list = get_build_info(config)
this_build_info_list = []
this_grade = -1
# 按照编译等级进行排序
build_info_list.sort(key=lambda k: (k.get('grade', 0)))
if app_name == 'all':
this_build_info_list = build_info_list
else:
# 获取此次编译列表
for build_info in build_info_list:
if build_info['name'] == app_name:
this_grade = build_info['grade']
if this_grade == -1:
print('[info]:%s文件中不存在%s' % (config, app_name))
# 获取此次编译列表
for build_info in build_info_list:
if build_info['grade'] < this_grade or build_info['name'] == app_name :
this_build_info_list.append(build_info)
# 进行编译
for build_info in this_build_info_list:
if build_info['grade'] == 0:
print('##############################################')
svn_checkout(build_info['svn_url'], build_info['svn_path'])
build_jar(build_info)
print('##############################################\n')
else:
print('##############################################')
svn_checkout(build_info['svn_url'], build_info['svn_path'])
build_war(build_info, env_name)
print('##############################################\n')
if __name__ == "__main__":
# 无参数报错提示
if len(sys.argv[0:]) <= 1:
get_help()
exit(1)
# 校验参数
param_dict = eval(sys.argv[1])
get_params(param_dict)
main(param_dict['config'], param_dict['env_name'], param_dict['app_name'])
[aideploy@java-jenkins config]$ cat view.json
[
{
"name": "view-api",
"grade": 0,
"svn_url": "https://192.168.228.46:18080/svn/CRM_CODE/branches/crm-v0.3-branch/view/view-api",
"svn_path": "/app/aideploy/work/view/source/agv-api",
"maven_cmd": "mvn clean package install -Dmaven.test.skip=true"
},
{
"name": "view-server",
"grade": 1,
"svn_url": "https://192.168.228.46:18080/svn/CRM_CODE/branches/crm-v0.3-branch/view/view-server",
"svn_path": "/app/aideploy/work/view/source/view-server",
"deploy_path": "/app/aideploy/work/view/deploy",
"war_info_list": [
{
"war_path": "/app/aideploy/work/view/source/view-server/target/view-server.war",
"dist_war_name": "view-server.war"
}
]
}
]
配置发布脚本
发布服务器为阿里云专有云
用到了3000多台的edas集群服务器
通过调用edas服务器的python-api-sdk来进行执行发布的动作
(对应的java-python-sdk可以参照人家阿里云官网的api接口介绍都有哒)
使用专有云的oss存储来存储对应的java-war包
引用到了同级目录的config目录下的xls表格文件
此excl表格内容为对应发布edas机器以及对应的发布war包名称
一 一对应通过表格信息来进行发布指定的机器以及war包服务
部分敏感信息已做处理像aksk-域名
(虽然是专有云内网环境,身为运维是有必要必须严谨一些的)
意思就是这么个意思、逻辑就是大概这么个逻辑
我相信只要是运维同学就能看的懂哒
至此发布脚本配置完毕
[aideploy@java-jenkins deploy]$ ls
00-readme.txt deploy-prd deploy.sh dev2sd lib prod-v3sd sit-v3sd
config deploy.py dev2jt do-deploy.sh prod-v3jt sit-v3jt utils
[aideploy@java-jenkins deploy]$ ls utils/
edas_utils.py oss_utils.py __pycache__
[aideploy@java-jenkins deploy]$ ls lib/
aliyun-sdk-oss-2.2.3.jar commons-logging-1.2.jar fastjson-1.2.17-sources.jar poi-ooxml-3.17.jar
commons-codec-1.9.jar commons-logging-1.2-sources.jar httpclient-4.4.1.jar poi-ooxml-schemas-3.17.jar
commons-io-2.4.jar deploy-edas.jar httpcore-4.4.1.jar
commons-io-2.4-sources.jar edas-open-api-sdk-2.10.1.jar jdom-1.1.jar
commons-lang-2.5.jar fastjson-1.2.17.jar poi-3.17.jar
[aideploy@java-jenkins deploy]$ ls config/
config.properties crmLy.xls date.txt deploy.xlsx view.xls
[aideploy@java-jenkins deploy]$ cat deploy.py
# -*- coding: utf-8 -*-
# @Author : pengge
# @Time : 2018/5/8 15:19
import sys
import time
import pandas as pd
from utils.edas_utils import EdasUtils
from utils.oss_utils import OssUtils
def main(action, version, module_name, env_name, app_name):
"""
阿里云edas/oss的API发布主函数
:param action: 执行的动作
:param version: 版本号
:param module_name: 模块名称
:param env_name: 环境名称
:param app_name: EDAS的app_name
:return:
"""
#https://edas.console.res.postcloud.cn/
edas_config = {'access_key_id': 'sp************',
'access_key_secret': 'uoly************',
'endpoint': 'edas-api.console************',
'region_id': 'cn-be************',
'api_version': '2017-08-01'}
oss_config = {'access_key_id': 'spi6************',
'access_key_secret': 'uolyHgaq************',
'endpoint': 'http://oss-cn-beijing-************'}
deploy_excel_data = get_all_deploy_info()
# 增加对表格信息的稽核
if action == 'check':
status = do_check(edas_config, deploy_excel_data)
if not status:
exit(1)
else:
exit(0)
deploy_info_list = []
bucket_list = []
for item in deploy_excel_data:
# 假如状态不为“U”,直接退出
if 'U' != item['state']:
continue
# 不是当前环境名,跳出
if env_name != item['env_name']:
continue
if module_name != item['module_name'] and module_name != 'all':
continue
if app_name != item['app_name'] and app_name != 'all':
continue
# 本地文件目录
local_file = '%s/%s' % (item['env_name'], item['pkg_name'])
# OSS对象名称
object_name = '%s/%s/%s/%s' % (item['module_name'], item['env_name'], version, item['pkg_name'])
# OSS对象下载地址
war_url = '%s/%s/%s' % (oss_config['endpoint'], item['bucket_name'], object_name)
# 获取edas对应部署OSS的bucket_list
if item['bucket_name'] not in bucket_list:
bucket_list.append(item['bucket_name'])
# EDAS和OSS操作需要的信息
deploy_info = {'app_id': item['app_id'],
'app_name': item['app_name'],
'local_file': local_file,
'object_name': object_name,
'bucket_name': item['bucket_name'],
'war_url': war_url}
deploy_info_list.append(deploy_info)
if len(bucket_list) == 0 or len(deploy_info_list) == 0:
print('[info]:没有需要发布的应用')
exit(0)
if action == 'stop':
print('##############################################')
print('[info]: 开始执行停止Edas服务指令!')
do_stop(edas_config, deploy_info_list)
print('[info]: 停止指令执行完毕!')
print('##############################################\n')
elif action == 'start':
print('##############################################')
print('[info]: 开始执行启动Edas服务指令!')
do_start(edas_config, deploy_info_list)
print('[info]: 启动指令执行完毕!')
print('##############################################\n')
elif action == 'deploy':
print('##############################################')
print('[info]: 开始执行部署Edas服务指令!')
do_deploy(edas_config, deploy_info_list, version)
print('[info]: 部署指令执行完毕!')
print('##############################################\n')
elif action == 'info':
print('##############################################')
print('[info]: 开始执行查询Edas服务信息指令!')
do_info(edas_config, deploy_info_list)
print('[info]: 查询指令执行完毕!')
print('##############################################\n')
elif action == 'ftp':
print('##############################################')
print('[info]: 开始上传包到OSS!')
do_ftp(oss_config, deploy_info_list, bucket_list)
print('[info]: 上传完毕!')
print('##############################################\n')
else:
print('[error]请输入正确的参数!')
exit(1)
def do_ftp(oss_config, edas_info_list, bucket_list):
"""
根据edas_info_list上传包到oss
:param oss_config: oss api配置信息
:param edas_info_list: 需要操作的edas信息list
:param bucket_list: edas对应bucket的list
:return: None
"""
for bucket_name in bucket_list:
oss_utils = OssUtils(oss_config, bucket_name)
for edas_info in edas_info_list:
if edas_info['bucket_name'] == bucket_name:
print('[info]: 开始上传!')
response = oss_utils.put_object(edas_info['object_name'], edas_info['local_file'])
print('AppName:' + edas_info['app_name'])
print('local_file:' + edas_info['local_file'])
print('war_url:' + edas_info['war_url'])
print('request_id:' + response.request_id)
if response.status == 200:
print('[info]: 上传成功!\n')
else:
print('[error]: 上传失败!\n')
def print_edas_info(edas_info, response, action):
print('AppId:' + edas_info['app_id'])
print('AppName:' + edas_info['app_name'])
print('response:' + str(response))
if response['Code'] == 200:
print('[info]: %s成功!\n' % action)
else:
print('[error]: %s失败!\n' % action)
def do_stop(edas_config, edas_info_list):
"""
根据edas_info_list停止edas服务
:param edas_config: edas api配置信息
:param edas_info_list: 需要操作的edas信息list
:return: None
"""
edas_utils = EdasUtils(edas_config)
for edas_info in edas_info_list:
response = edas_utils.stop_app(edas_info['app_id'])
print_edas_info(edas_info, response, '停止')
# 获取此次部署edas部署的状态,全部为0时退出
still_edas_state(edas_config, edas_info_list, 0)
def do_start(edas_config, edas_info_list):
"""
根据edas_info_list启动edas服务
:param edas_config: edas api配置信息
:param edas_info_list: 需要操作的edas信息list
:return: None
"""
edas_utils = EdasUtils(edas_config)
for edas_info in edas_info_list:
response = edas_utils.start_app(edas_info['app_id'])
print_edas_info(edas_info, response, '启动')
# 获取此次部署edas部署的状态,全部为1时退出
still_edas_state(edas_config, edas_info_list, 1)
def do_deploy(edas_config, edas_info_list, version):
"""
根据edas_info_list部署edas服务
:param edas_config: edas api配置信息
:param edas_info_list: 需要操作的edas信息list
:param version: 版本信息
:return: None
"""
edas_utils = EdasUtils(edas_config)
for edas_info in edas_info_list:
i = 1
while True:
response = edas_utils.deploy_app(app_id=edas_info['app_id'], package_version=version, deploy_type='url',
war_url=edas_info['war_url'], group_id='all')
if response['Code'] != 200:
print('[info]:5s后重新检查%s 的状态,请稍等。' % edas_info['app_name'])
time.sleep(5)
else:
break
# 重试超过三次退出
if i > 3:
print('[error]:%s 已重试3次,部署异常,请重新部署!!!' % edas_info['app_name'])
print(edas_info['war_url'])
print(edas_info['app_id'])
print(str(response))
break
i += 1
print_edas_info(edas_info, response, '部署')
# 获取此次部署edas部署的状态,全部为1时退出
still_edas_state(edas_config, edas_info_list, 1)
def do_info(edas_config, edas_info_list):
"""
根据edas_info_list获取das服务信息
:param edas_config: edas api配置信息
:param edas_info_list: 需要操作的edas信息list
:return: None
"""
edas_utils = EdasUtils(edas_config)
for edas_info in edas_info_list:
response = edas_utils.get_app_info(edas_info['app_id'])
if response['Code'] == 200:
print('app_name :%s' % response['Applcation']['Name'])
print('app_id :%s' % response['Applcation']['AppId'])
if 0 == response['Applcation']['RunningInstanceCount']:
print('app_state: %s' % 'False')
else:
print('app_state: %s' % 'True')
def do_check(edas_config, deploy_excel_data):
edas_utils = EdasUtils(edas_config)
status = True
for edas_info in deploy_excel_data:
if edas_info['state'] not in ['U', 'E']:
status = False
print('[error]: %s 的state字段不是U和E!' % app_name)
app_name = edas_info['app_name']
if not isinstance(app_name, str):
break
app_id = edas_info['app_id']
response = edas_utils.get_app_info(app_id)
if response['Code'] == 200:
if app_name != response['Applcation']['Name']:
status = False
print('[error]: %s 和app_id不一致!' % app_name)
else:
print('[error]: %s 请求失败!\n %s' % (app_name, str(response)))
return status
def get_all_deploy_info():
column_info = [('module_name', 0), ('env_name', 1), ('app_name', 2), ('app_id', 3), ('pkg_name', 4), ('bucket_name', 5),
('state', 6), ('remark', 7), ]
return read_excel('config/deploy.xlsx', 'deploy', column_info)
def still_edas_state(edas_config, edas_info_list, state):
edas_utils = EdasUtils(edas_config)
# 获取此次部署edas部署的状态,全部为 state 时退出
for edas_info in edas_info_list:
i = 1
while True:
response = edas_utils.get_app_info(app_id=edas_info['app_id'])
if response['Applcation']['RunningInstanceCount'] != state and state == 0:
print('[info]:5s后重新检查%s 的状态,请稍等。' % edas_info['app_name'])
time.sleep(5)
elif response['Applcation']['RunningInstanceCount'] < state :
print('[info]:5s后重新检查%s 的状态,请稍等。' % edas_info['app_name'])
time.sleep(5)
else:
break
# 重试超过三次退出
if i > 12:
print(str(response))
print('[error]:%s 已重试12次,应用状态异常,请登陆控制台检查!!!' % edas_info['app_name'])
break
i += 1
def read_excel(file, sheet_name, column_info):
"""
读取Excel表格内容
:param file: path/**.xls[x]
:param sheet_name: sheet页名字
:param column_info: [(columnA, 0), (columnA, 1)]
:return: list_dict
"""
excel_data = pd.DataFrame(pd.read_excel(file, sheet_name=sheet_name))
list_dict = []
for item in excel_data.values:
line_data = {}
for column in column_info:
name = column[0]
location = column[1]
line_data[name] = item[location]
list_dict.append(line_data)
return list_dict
def get_params(params):
"""
处理传给脚本的参数
:param params: 参数信息
:return:
"""
action = params['action']
version = params['version']
env_name = params['env_name']
# 对参数组合进行校验
if action not in ['stop', 'start', 'deploy', 'info', 'ftp', 'check']:
print('[error]:不存在该action')
get_help()
exit(1)
if env_name == '':
print('[error]:env_name不可为空')
get_help()
exit(1)
if action in ['ftp', 'deploy'] and version == '':
print('[error]:action为ftp和deploy时,version不可为空')
get_help()
exit(1)
def get_help():
help_message = """
阿里云V3部署工具使用说明:
${PYTHON_HOME}/bin/python3 deploy.py '{"action":"action","version":"version","env_name":"env_name","module_name":"module_name","app_name":"app_name"}'
action : 可选择action为stop、start、info、deploy、ftp。(不可为空)
version : action为ftp和deploy时,需要指定version。(不可为空)
env_name: 环境名称。(不可为空)
module : 模块名称/all。
app_name: EDAS的app_name/all。
【注意】:不是单个部署时,app_name为all即可。
"""
print(help_message)
if __name__ == '__main__':
# 无参数报错提示
if len(sys.argv[0:]) <= 1:
get_help()
exit(1)
# 校验参数
param_dict = eval(sys.argv[1])
print('###################参数信息###################')
print(sys.argv[1])
get_params(param_dict)
main(param_dict['action'], param_dict['version'], param_dict['module_name'], param_dict['env_name'], param_dict['app_name'])
python依赖
用到的python版本为3的
下图为大家准备一下对应的依赖包文件
没有pip3对应版本可以到官网直接下载安装即可
[aideploy@java-jenkins deploy]$ wget https://files.pythonhosted.org/packages/69/81/52b68d0a4de760a2f1979b0931ba7889202f302072cc7a0d614211bc7579/pip-18.0.tar.gz
[aideploy@java-jenkins deploy]$ python3 -V
Python 3.6.5
[aideploy@java-jenkins deploy]$ python3 setup.py install
[aideploy@java-jenkins deploy]$
[aideploy@java-jenkins deploy]$ pip3.6 freeze > requirement.txt
[aideploy@java-jenkins deploy]$ cat requirement.txt
alibaba-cloud-python-sdk-v2==1.0.5
aliyun-python-sdk-core==2.13.4
aliyun-python-sdk-core-v3==2.13.3
aliyun-python-sdk-ecs==4.16.7
aliyun-python-sdk-edas==2.43.0
aliyun-python-sdk-kms==2.6.0
asn1crypto==0.24.0
certifi==2019.3.9
cffi==1.12.3
chardet==3.0.4
crcmod==1.7
cryptography==2.6.1
idna==2.8
jenkins==1.0.2
jmespath==0.9.4
Markdown==3.1
mq-http-sdk==1.0.1
numpy==1.16.3
oss2==2.6.1
pandas==0.24.2
psutil==5.6.3
pycparser==2.19
pycryptodome==3.8.1
pymongo==3.8.0
pyOpenSSL==19.0.0
python-dateutil==2.8.0
pytz==2019.1
requests==2.21.0
SDK==1.0.0
six==1.12.0
urllib3==1.24.2
xlrd==1.2.0
xlwt==1.3.0
Jenkins执行结果
build-job-result
work_home=$HOME/work/view
cd ${work_home}/bin
# 更新配置文件
svn up ${work_home}/bin/config
cp ${work_home}/bin/config/*xls ${work_home}/deploy/config
# 开始编译
python3 -u build.py ${params}
deploy-job-result
module_name='view'
work_home=${HOME}/work/${module_name}
python_home=${HOME}/python3.6.5
cd $work_home/deploy
svn up /app/aideploy/work/view/bin/config
\cp -rvf /app/aideploy/work/view/bin/config/view.xls /app/aideploy/work/view/deploy/config/deploy.xlsx
# 处理参数
dt=`date +%Y%m%d%T`
echo "$dt"
params='{"action":"${action}","version":"'$dt'","module_name":"${module_name}","env_name":"${env_name}","app_name":"${app_name}"}'
params=${params/'${module_name}'/${module_name}}
params=${params/'${version}'/"'$dt'"}
params=${params/'${env_name}'/${env_name}}
params=${params/'${app_name}'/${app_name}}
ftp_pamrams=${params/'${action}'/'ftp'}
stop_pamrams=${params/'${action}'/'stop'}
deploy_pamrams=${params/'${action}'/'deploy'}
#start_pamrams=${params/'${action}'/'start'}
# 传包到OSS
python3 -u deploy.py ${ftp_pamrams}
# 停止EDAS服务
python3 -u deploy.py ${stop_pamrams}
# sleep 20s
# 部署EDAS服务
python3 -u deploy.py ${deploy_pamrams}
# sleep 30s
# 启动EDAS服务
#python3 -u deploy.py ${start_pamrams}
至此Jenkins的shell-python发布任务即配置完毕啦
还引用了公司内部的需求平台,以及自动化发布版本管理系统,在此就不截图啦
类似开源的zentao、收费的 jira、YouTrack等等版本管理工具均可以接入Jenkins对接接口
祝愿各位能更加优秀,更加强大~
努力到无能为力,拼搏到感动自己~ 加油!