zoukankan      html  css  js  c++  java
  • Python 实现一键发布项目

    这是 Stephen 的第 87 篇原创文章

    由于公司业务项目所需,要每天频繁发布版本,高达几个小时一个版本,甚至一个小时多个版本,虽是小需求,小改动,但急用。基本上是边开发边发布的工作节奏。

    说到这,不禁眼眶有点湿润……羡慕每周发一版,每两周发一版,甚至更长时间发版周期的公司项目组。

    因这种高频次的发布需要,发布操作就显得格外繁琐,包括同步代码,打包,连接 Linux 服务器,备份旧版本,上传新版本 jar 包,重启项目等。如果把这个繁琐的工作“外包”出去,是不是就省事很多了。

    说干就干,招“外包”!

    刚好最近学了点 Python ,那就招 Python 吧,让 Python 替我把这繁琐的发布操作工作给干了。

    我只需要简单地通知下它就好。

    “通知” 长啥样,怎么通知;Python 具体又怎么干呢?

    ----------------------------代码实战分割线-----------------------------

    “通知” 就是我双击这个 publish-dashboard.bat 批处理文件,让它去执行另外一个 Python 文件 publishdashboard.py,Python 帮我做完同步代码,打包,连接 Linux 服务器,备份旧版本,上传新版本 jar 包,重启项目这些工作。

    publish-dashboard.bat

    1 python publishdashboard.py
    2 pause

    publishdashboard.py

     1 # coding=utf-8
     2 import os
     3 import time
     4 import datetime
     5 import paramiko
     6 
     7 # ssh 配置
     8 hostname = '192.168.1.126'
     9 username = 'root'
    10 password = 'root'
    11 port = 22
    12 
    13 # 相关文件,路径,命令的配置
    14 jar_name = 'dashboard-0.0.1-SNAPSHOT'
    15 command_cp = 'cp /root/dashboard/' + jar_name + '.jar /root/dashboard/back/' + jar_name + '-$(date +%Y%m%d-%H%M%S).jar'
    16 command_gitpull = 'cd D:dashboard-打包发布 && d: && git pull'
    17 command_mvnpackage = 'cd D:dashboard-打包发布 && d: && mvn package -Dmaven.test.skip=true'
    18 command_restart = 'sh /root/dashboard/restart-dashboard.sh'
    19 local_file = 'D:\dashboard-打包发布\target\' + jar_name + '.jar'
    20 remote_file = '/root/dashboard/' + jar_name + '.jar'
    21 
    22 # 同步代码,打包
    23 def buildjar():
    24 time.sleep(1)
    25 os.system(command_gitpull)
    26 print("git pull 成功 !")
    27 time.sleep(1)
    28 os.system(command_mvnpackage)
    29 print('package 成功 !')
    30 
    31 # 执行远程命令
    32 def exe(command):
    33 ssh = paramiko.SSHClient()
    34 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    35 ssh.connect(hostname, username=username, password=password)
    36 stdin, stdout, stderr = ssh.exec_command(command)
    37 result = stdout.read().decode()
    38 print(result)
    39 result = stderr.read().decode()
    40 print(result)
    41 
    42 # 上传文件
    43 def uploadfile(local_file, remote_file):
    44 try:
    45 t = paramiko.Transport((hostname, port))
    46 t.connect(username=username, password=password)
    47 sftp = paramiko.SFTPClient.from_transport(t)
    48 sftp.put(local_file, remote_file)
    49 print("upload %s to remote %s" % (local_file, remote_file))
    50 except Exception as e:
    51 print("error", e)
    52 
    53 # 函数入口
    54 if __name__ == '__main__':
    55 #去备份项目
    56 exe(command_cp)
    57 #去同步代码打包
    58 buildjar()
    59 #去上传项目jar
    60 uploadfile(local_file, remote_file)
    61 #去重启服务
    62 exe(command_restart)
    63 #来通知我这些事情你做成功了没有
    64 print('publish-dashboard successfully %s ' % datetime.datetime.now())


    -- end --

    喜欢就三连呀

    【微信公众号:Stephen】一个毕业三年后自学 Java 入行的程序员。
  • 相关阅读:
    【硬件】PCB设计步骤
    【modbus】modbus协议入门讲解
    【I2C】上拉电阻的选择
    【运放】失调电压、偏置电流
    【硬件】模拟地和数字地的隔离
    【电力】电流互感器和电压互感器
    【电力】为什么高电压传输时线路损耗小
    【办公】pdf转ppt的方法
    【EMC】电压暂降、短时中断和电压变化
    js 下的 split
  • 原文地址:https://www.cnblogs.com/stephen-java/p/15067180.html
Copyright © 2011-2022 走看看