Jenkins H5打包

#!/usr/bin/env python3
# -*- coding:GBK -*-
# author by Michael Ho
# contact:[email protected]

import os, shutil, time
# npm编译
def npm_compile(npm_path, npm_source, s_dir):
    """
    :param npm_path:
    :param path:
    :return:
    """
    os.chdir(s_dir)
    print("当前工作目录是:{}".format(s_dir))
    print("开始从 {} 下载依赖包... ...".format(npm_source))
    npm_install = npm_path + r" install --registry=" + npm_source
    os.system(npm_install)
    print("开始编译... ...")
    npm_build = npm_path + r" run build"
    os.system(npm_build)

# 加密成.d文件
def H5_encode(encode_tool, s_dir, d_dir):
    """
    :param encode_tool: H5Encode加密工具
    :param s_path: workspace源代码路径
    :param d_path: 加密后的代码路径
    """
    if os.path.exists(d_dir):
        shutil.rmtree(d_dir)
    os.makedirs(d_dir)

    if not os.path.exists(encode_tool):
        print("加密工具不存在,请检查... ...")
        exit(0)
    else:
        H5_Encode_CMD = encode_tool + r" -D " + s_dir + r"\ " + d_dir + r"\ 0"
        print(H5_Encode_CMD)
        os.system(H5_Encode_CMD)

# 打包归档
def zip_file(zip_exe, path, file):
    """
    :param zip_exe: 需要打包的路径
    :param path: 需要打包的路径
    :param file: 压缩包的名称
    :return:
    """
    if os.path.exists(file):
        os.remove(file)

    zip_CMD = zip_exe + r" a " + file + r" " + path + r"\*"
    print(zip_CMD)
    print("{} 归档中... ...".format(file))
    os.system(zip_CMD)

    if not os.path.exists(file):
        print("{} 归档失败, 请检查 Jenkins 上脚本... ...".format(file))
        exit(0)

# 生成小包函数
def copy_app_H5(x_root, s_dir, d_dir):
    if not os.path.exists(x_root):
        print("小包目录不存在,构建停止 ... ...")
        exit(0)

    if os.path.exists(d_dir):
        shutil.rmtree(d_dir)

    for root, dirs, files in os.walk(x_root):
        for d in dirs:
            d_name = os.path.join(root, d).rstrip()
            d_dir_name = d_name.replace(x_root, d_dir)
            if not os.path.exists(d_dir_name):
                print("创建小包目标目录: {}".format(d_dir_name))
                os.makedirs(d_dir_name)

        for f_name in files:
            f_name = os.path.join(root, f_name).rstrip()
            if(os.path.splitext(f_name)[1] == ".d"):
                s_file = f_name.replace(x_root, s_dir)
                d_file = f_name.replace(x_root, d_dir)
                shutil.copyfile(s_file, d_file)
                # 判断复制是否成功
                if(s_file.replace(s_dir, "") == d_file.replace(d_dir, "")):
                    print("{} ---> 拷贝成功 ^.^".format(d_file))
                else:
                    print("拷贝过程发生错误,请检查...")

# 全量包部署
def copy_all_file(d_qdymanage, s_dir, d_dir):

    if os.path.exists(d_qdymanage):
        for file in os.listdir(d_dir):
            file = os.path.join(d_dir, file)
            if os.path.isfile(file):
                os.remove(file)
            if os.path.isdir(file) and file != d_qdymanage:
                shutil.rmtree(file) # 删除除qdymanage目录以外所有的目录和文件
    else:
        print("{} 目录不存在, 请检查! 构建停止... ...".format(d_qdymanage))
        exit(0)

    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) # 列出目标文件的绝对路径
            print("{0}  --->  {1}".format(s_file_name, d_file_name).rstrip())
            shutil.copyfile(s_file_name, d_file_name)


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

    os.environ['VERSION'] = r"8_02_000"

    # 0.编译
    npm_path = r'"C:\Program Files\nodejs\npm"'
    # npm_source = r"https://registry.npm.taobao.org"
    # npm_source = r"http://172.50.1.119:7001"
    npm_source = r"http://172.50.1.15:8081/repository/npm-group/"
    s_dir = os.getenv("WORKSPACE")
    npm_compile(npm_path, npm_source, s_dir)

    # 1.加密
    encode_tool = r"d:\Jenkins\H5Encode.exe"
    m_dir = s_dir + r"\dest" # %workspace%\dist 是H5源代码目录
    d_dir = r"d:\Jenkins\frontend_encrypt"
    H5_encode(encode_tool, m_dir, d_dir)

    # 2.打包归档(小包)
    # 2.1 生成小包
    x_root = r"d:\H5_app_src"
    xs_dir = d_dir
    xd_dir = r"d:\Jenkins\H5_app"
    copy_app_H5(x_root, xs_dir, xd_dir)

    # 2.2 小包打包
    zip_exe = r'"C:\"Program Files"\7-Zip\7z.exe"'
    src_file = r"H5_" + os.getenv("VERSION") + "_src_" + os.getenv("TEST_ENV") + "_" + time.strftime('%Y%m%d') + ".7z"
    src_dir = xd_dir
    zip_file(zip_exe, src_dir, src_file)

    # 3.打包归档(全量包)
    file =  r"H5_" + os.getenv("VERSION") + r"_" + os.getenv("TEST_ENV") + "_" + time.strftime('%Y%m%d') + ".7z"
    zip_file(zip_exe, d_dir, file)


    # 4.170环境部署全量包
    if os.getenv("TEST_ENV") == "js_test":
        d_qdymanage = r"d:\zzinfo\mszx\download\qdymanage"
        all_s_dir = d_dir
        all_d_dir = r"d:\zzinfo\mszx\download"
        copy_all_file(d_qdymanage, all_s_dir, all_d_dir)
herui

一枚干着DevOps工作的职业命理师

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据