-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
124 lines (107 loc) · 3.92 KB
/
deploy.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import random
from subprocess import run
from pathlib import Path
ON_CF = os.getenv("CF_PAGES") == "1"
if ON_CF:
print("Running on Cloudflare Pages, trying to git fetch --all")
result = run(["git", "fetch", "--all"], check=False)
print(f"git fetch --all: {result}")
# 提前尝试输出一遍下面这堆信息
run(["git", "branch", "--show-current"], check=False)
run(["git", "rev-parse", "HEAD"], check=False)
run(["git", "describe", "--tags"], check=False)
run(["git", "log", "-1", "--pretty=%B"], check=False)
def get_env_info() -> dict[str, str]:
# 读取环境变量
env_info = {}
# git branch
if ON_CF:
branch = os.getenv("CF_PAGES_BRANCH") or "unknown"
else:
branch = run(
["git", "branch", "--show-current"],
capture_output=True,
text=True,
encoding="utf-8",
).stdout
env_info["branch"] = branch.strip()
# git commit hash
if ON_CF:
commit = os.getenv("CF_PAGES_COMMIT_SHA") or "unknown"
else:
commit = run(
["git", "rev-parse", "HEAD"],
capture_output=True,
text=True,
encoding="utf-8",
).stdout
env_info["commit"] = commit.strip()
# git commit message
message = run(
["git", "log", "-1", "--pretty=%B"],
capture_output=True,
text=True,
encoding="utf-8",
)
env_info["message"] = message.stdout.strip()
# git tag
if ON_CF:
tag = run(
["git", "describe", "--tags"], capture_output=True, text=True, encoding="utf-8"
).stdout.split("-")[0] or "cf_pages"
else:
tag = run(
["git", "describe", "--tags"], capture_output=True, text=True, encoding="utf-8"
).stdout.split("-")[0]
env_info["tag"] = tag.strip()
return env_info
if __name__ == "__main__":
# 虽然但是, 我还是决定用 python 写这个脚本
border_raw = "/* border: 2px solid marker_color */"
border_template = "border: 2px solid {};"
marker_raw = '<div id="version-marker" style="display: none;"></div>'
marker_template = '<div id="version-marker">{}</div>'
# 读取环境变量
env_info = get_env_info()
tag = env_info["tag"]
branch = env_info["branch"]
commit = env_info["commit"]
message = env_info["message"]
for file in Path.cwd().rglob("index.html"):
try:
with open(file, "r", encoding="utf-8") as f:
raw_content = f.read()
except Exception as e:
print(f"Error: {e}")
continue
print(f"Reading: {file}")
# 替换内容
# 首先判断是否是 /branch 目录下的 index.html
if "branch" in str(file):
# 如果是, 则将颜色替换为 random(这里是为了区分不同的分支, 并且颜色相对固定)
file_branch_name = file.parent.name
randomer = random.Random(file_branch_name)
hash_color = randomer.randint(0, 0xFFFFFF)
border = border_template.format(f"#{hash_color:06x}")
# git 信息:
version_info = f"{file_branch_name}/{branch}:{tag}-{commit[:6]}<br/>{message}"
marker = marker_template.format(version_info)
print(f"Branch: {file_branch_name}\n{border}\n{marker}\n")
else:
# 淡绿色!
border = border_template.format("greenyellow")
# git 信息:
version_info = f"{branch}:{tag}-{commit[:6]}"
marker = marker_template.format(version_info)
print(f"Master: {border}\n{marker}\n")
raw_content = raw_content.replace(border_raw, border).replace(
marker_raw, marker
)
# 写入文件
try:
with open(file, "w", encoding="utf-8") as f:
f.write(raw_content)
except Exception as e:
print(f"Error writing file: {e}")
continue