Jenkins job的自动部署脚本

CloasGao 2020-04-08

Jenkins job的自动部署脚本

需求:

1.从Jenkins的workspace中,将编译好的Java代码部署到D:\zzinfo\UserCenterCoordinator中

2.部署过程中,需要停止相关联的其他进程,如5558_CENTER64.exe | 5559_CENTER64_UserList.exe | APNs.exe

代码如下:

#!/usr/local/env python3
# -*- coding:utf-8 -*-
# author by Michael Ho
# contact: 

import psutil, shutil, os

# 杀死进程
def kill_process_name(process_name):
    pid_list = psutil.pids()
    for pid in pid_list:
        try:
            each_pro = psutil.Process(pid)
            if process_name.lower() in each_pro.name().lower():
                # logger.info(‘find and kill {}‘.format(process_name))
                each_pro.terminate()
                each_pro.wait(timeout=3)

        except psutil.NoSuchProcess as pid:
            pass

# copy文件
def copy_file(s_dir, d_dir):
    if os.path.exists(d_dir):
        shutil.rmtree(d_dir) # 删除zzinfo工作目录
    os.makedirs(d_dir)
    for root, dirs, files in os.walk(s_dir):
        for d in dirs:
            s_dir_name = os.path.join(root, d) # 列出workspace里面的子目录
            d_dir_name = s_dir_name.replace(s_dir, d_dir)
            if not os.path.exists(d_dir_name):
                os.makedirs(d_dir_name) # 创建zzinfo里面的子目录

        for f in files:
            s_file_name = os.path.join(root, f) # 列出workspace里面的所有文件的绝对路径
            d_file_name = s_file_name.replace(s_dir, d_dir) # 列出目标文件的绝对路径
            shutil.copyfile(s_file_name, d_file_name)


# main函数入口
if __name__ == ‘__main__‘:

    # set BUILD_ID is dontKillMe, or failed to start 5558, 5559 and APNs
    os.environ[‘BUILD_ID‘] = "dontKillMe"

    # kill 5558 | 5559 | APNs Process
    kill_process_name("5558_CENTER64.exe")
    kill_process_name("5559_CENTER64_UserList.exe")
    kill_process_name("APNs.exe")

    # copy lib
    lib_s_dir = os.getenv("WORKSPACE") + r‘\lib‘
    lib_d_dir = r"d:\zzinfo\UserCenterCoordinator\lib"
    copy_file(lib_s_dir, lib_d_dir)

    # copy bin
    bin_s_dir = os.getenv("WORKSPACE") + r‘\target\classes‘
    bin_d_dir = r"d:\zzinfo\UserCenterCoordinator\bin"
    copy_file(bin_s_dir, bin_d_dir)

    # 启动5558 | 5559 | APNs
    os.popen(r"start /d d:\zzinfo\center64-UserList_V3 5559_CENTER64_UserList.exe")
    os.popen(r"start /d d:\zzinfo\center64_V3 5558_CENTER64.exe")
    os.popen(r"start /d d:\zzinfo\APNs APNs.exe")

相关推荐