-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
107 lines (84 loc) · 2.63 KB
/
build.py
File metadata and controls
107 lines (84 loc) · 2.63 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
"""
跨平台构建脚本
仅使用 Python 标准库,无需 pip install 任何依赖
前置要求(需用户自行安装):
- Python 3.7+
- Docker
- rclone
"""
import subprocess
import sys
from pathlib import Path
# 配置
DIST_DIR = Path("./dist")
IMAGE_NAME = "static-server"
REMOTE_PATH = "tos:muelsyse/static-server/static-server"
def run(cmd: str, capture_output: bool = False) -> subprocess.CompletedProcess:
"""
执行 shell 命令,失败时自动退出
Args:
cmd: 要执行的命令
capture_output: 是否捕获输出
Returns:
CompletedProcess 对象
"""
print(f">>> {cmd}", file=sys.stderr)
result = subprocess.run(
cmd,
shell=True,
capture_output=capture_output,
text=True,
encoding='utf-8'
)
if result.returncode != 0:
print(f"错误: 命令失败 (exit {result.returncode})", file=sys.stderr)
if result.stderr:
print(result.stderr, file=sys.stderr)
sys.exit(result.returncode)
return result
def build():
"""执行 Docker 构建并提取产物"""
# 1. 创建 dist 目录(如果不存在)
DIST_DIR.mkdir(parents=True, exist_ok=True)
print(f"确保目录存在: {DIST_DIR}")
# 2. 构建 Docker 镜像
print("\n构建 Docker 镜像...")
run(f"docker build -t {IMAGE_NAME} .")
# 3. 创建临时容器并获取 ID
print("\n提取构建产物...")
result = run(f"docker create {IMAGE_NAME}", capture_output=True)
container = result.stdout.strip()
try:
# 4. 复制构建产物
source = f"{container}:/root/static-server"
dest = DIST_DIR / "static-server"
run(f'docker cp "{source}" "{dest}"')
print(f"已提取到: {dest}")
finally:
# 5. 清理临时容器(无论成功与否都执行)
print(f"清理临时容器: {container[:12]}...")
run(f'docker rm "{container}"')
def upload():
"""上传构建产物到远程存储"""
print(f"\n上传到远程存储...")
local = DIST_DIR / "static-server"
run(f'rclone copyto "{local}" {REMOTE_PATH}')
print(f"上传完成: {REMOTE_PATH}")
def main():
"""主入口"""
print("=" * 50)
print("开始构建流程")
print("=" * 50)
# 执行构建和上传
build()
upload()
print("\n" + "=" * 50)
print(f"全部完成!构建产物: {DIST_DIR}/static-server")
print("=" * 50)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n用户中断", file=sys.stderr)
sys.exit(130)