Jenkins H5編譯、打包、發布


記錄Jenkins H5編譯、打包、發布
#!/usr/bin/env python3
# -*- coding:GBK -*-
# author by Michael Ho
# contact:herui@gszq.com

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):
                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"7_06_001"

    # 0.編譯
    npm_path = r'"C:\Program Files\nodejs\npm"'
    npm_source = r"https://registry.npm.taobao.org"
    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_ENVIRONMENT") + "_" + 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_ENVIRONMENT") + "_" + time.strftime('%Y%m%d') + ".7z"
    zip_file(zip_exe, d_dir, file)


    # 4.170環境部署全量包
    if os.getenv("TEST_ENVIRONMENT") == "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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM