当前位置:   article > 正文

python实现前端项目打包自动化部署_python多个打包 前端

python多个打包 前端

需要安装paramiko

pip install paramiko
  • 1

定义字典进行前端多环境的打包压缩连接服务器部署

buildDisct = {
        "cmdList": ["npm run build", "npm run build:test"],# 前端多环境打包命令
        "zipFileName": ["dist", "dist2"],# 生成的打包路径进行文件压缩
        "ssh": [
            {"host": 'xx.xx.xx.xxx',#服务器ip
              "port": 22,# 端口号
             "user": 'root', # 账号
             "private_key_path": 'E:\XXXX',# 本地的密钥路径
             "remote_directory": './nginx/html/web2',# 上传服务器路径
             "local_zip_path": r'E:\xx\dist\dist.zip',# 上传本地路径前端打包的zip
             "out_dir": 'dist'# 上传服务器指定的文件
             },
             {"host": 'xx.xx.xx.xxx',#服务器ip
              "port": 22,# 端口号
             "user": 'root', # 账号
             "private_key_path": 'E:\XXXX',# 本地的密钥路径
             "remote_directory": './nginx/html/web2',# 上传服务器路径
             "local_zip_path": r'E:\xx\dist2\dist2.zip',# 上传本地路径前端打包的zip
             "out_dir": 'dist'# 上传服务器指定的文件
             },
        ]
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

获取用户输入,直到输入正确才执行,输入错误就继续让输入(让用户指定全部打包还是正式服或者是测试服打包)

 while True:
        # 获取用户输入
        choice = input("Select build mode (all/formal/test): ")
        # 验证用户输入是否有效
        if choice in valid_modes:
            # 输入正确,跳出循环
            break
        else:
            print("Please enter a valid build mode")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

循环执行对应的打包部署命令

def perform_build_and_zip(cmd, out_dir, i):
        project = BuildProject()
        print(cmd)
        project.build(cmd)
        # 压缩成zip包
        zipFileModule.zip_directory_contents(out_dir, f'{out_dir}.zip')
        sshModule.upload_file(buildDisct["ssh"][i])
        
if choice == 'all':
        for i, cmd in enumerate(buildDisct['cmdList']):
            perform_build_and_zip(cmd, buildDisct["zipFileName"][i], i)
    elif choice == 'formal':
        perform_build_and_zip(
            buildDisct['cmdList'][0], buildDisct["zipFileName"][0], 0)
    elif choice == 'test':
        perform_build_and_zip(
            buildDisct['cmdList'][1], buildDisct["zipFileName"][1], 1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

感兴趣的可以添加作者共同进步,如果对你有帮助,请随意赞赏~~~
在这里插入图片描述
在这里插入图片描述

gtiee地址:https://gitee.com/lihuikun1/webautobuild

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/766285
推荐阅读
相关标签
  

闽ICP备14008679号