用python做了一个自动更新远程服务器项目代码和同步Git仓库的脚本
提示
最近写博客,因为我搭建的是静态网站,
所以老是写完一篇文章就需要git到两个仓库里,还要同步到自己的服务器上,这就很麻烦因为全手动,
刚开始我是用WinSCP
来上传到服务器,时不时用用宝塔面板,把打包好的静态文件拖上去,而且同步到gitee
还是要输入一堆git命令,就很麻烦。
然后寻思写个sh
,bash
,bat
脚本,但是我都不会,也懒得学,于是就拿python写了一个脚本,特简单,之后只需,打开cmd
,输入python xx.py
运行就完成了所有操作。
以下是源代码
"""
* description: upload
* date: 2022/4/4 19:46
* author: xinyu
* version: 1.0
"""
import os
# 需提前配置好git和 ssh免密连接
print("请输入项目打包commit:")
commit = input()
print("请输入项目commit:")
distcommit = input()
# 打包
os.system("yarn run build")
# 更新项目源代码
print("更新项目源代码")
# 首次运行需要取消注释
# os.system("git init")
os.system("git add .")
os.system("git commit -m {}".format(commit))
# 首次使用仓库运行需要取消注释
# os.system("git remote add origin https://gitee.com/xinyu185/myblog.git") #项目源代码仓库地址
os.system("git push -u origin master")
# 更新gitee静态托管
print("更新gitee静态托管")
# 更改目录
os.chdir('E:\\myblog\myblogV2\\myblog-master\\dist')
# os.system("cd /d E:\\myblog\myblogV2\\myblog-master\\dist") #这样写是不对的
# 首次运行需要取消注释
# os.system("git init")
os.system("git add .")
os.system("git commit -m {}".format(distcommit))
# 首次使用仓库运行需要取消注释
# os.system("git remote add origin https://gitee.com/xinyu185/myblog_dist.git") #项目打包的静态文件地址
os.system("git push -u origin master")
# 更新至服务器
print("更新至服务器")
os.system("scp -r E:\myblog\myblogV2\myblog-master\dist\dist root@8.140.56.185:/root/myblog_koa/static")
#前一个地址是打包后静态文件的目录,后一个是服务器静态文件的目录地址
#路径,自己按着自己的项目看着来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
上次更新: 2022/05/13 21:13:15