部署!

This commit is contained in:
shenjack 2024-04-20 22:43:46 +08:00
parent 6962063c69
commit 71347c5484
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -1,4 +1,5 @@
import os import os
import random
from subprocess import run from subprocess import run
from pathlib import Path from pathlib import Path
@ -10,15 +11,10 @@ if ON_CF:
run(["git", "fetch", "--all"], check=False) run(["git", "fetch", "--all"], check=False)
def get_env_info() -> dict[str, str]: def get_env_info() -> dict[str, str]:
# 读取环境变量 # 读取环境变量
env_info = {} env_info = {}
# git branch # git branch
# if on cf, read it from env: CF_PAGES_BRANCH
if ON_CF:
branch = os.getenv("CF_PAGES_BRANCH") or "unknown"
else:
branch = run( branch = run(
["git", "branch", "--show-current"], ["git", "branch", "--show-current"],
capture_output=True, capture_output=True,
@ -27,10 +23,6 @@ def get_env_info() -> dict[str, str]:
).stdout ).stdout
env_info["branch"] = branch.strip() env_info["branch"] = branch.strip()
# git commit hash # git commit hash
# on cf -> get from CF_PAGES_COMMIT_SHA
# if ON_CF:
# commit = os.getenv("CF_PAGES_COMMIT_SHA") or "unknown"
# else:
commit = run( commit = run(
["git", "rev-parse", "HEAD"], ["git", "rev-parse", "HEAD"],
capture_output=True, capture_output=True,
@ -47,9 +39,6 @@ def get_env_info() -> dict[str, str]:
) )
env_info["message"] = message.stdout.strip() env_info["message"] = message.stdout.strip()
# git tag # git tag
if ON_CF:
tag = os.getenv("CF_PAGES_COMMIT_SHA") or "cf_deploy"
else:
tag = run( tag = run(
["git", "describe", "--tags"], capture_output=True, text=True, encoding="utf-8" ["git", "describe", "--tags"], capture_output=True, text=True, encoding="utf-8"
).stdout ).stdout
@ -84,9 +73,10 @@ if __name__ == "__main__":
# 替换内容 # 替换内容
# 首先判断是否是 /branch 目录下的 index.html # 首先判断是否是 /branch 目录下的 index.html
if "branch" in str(file): if "branch" in str(file):
# 如果是, 则将颜色替换为 hash(这里是为了区分不同的分支) # 如果是, 则将颜色替换为 random(这里是为了区分不同的分支, 并且颜色相对固定)
file_branch_name = file.parent.name file_branch_name = file.parent.name
hash_color = hash(file_branch_name) & 0xFFFFFF randomer = random.Random(file_branch_name)
hash_color = randomer.randint(0, 0xFFFFFF)
border = border_template.format(f"#{hash_color:06x}") border = border_template.format(f"#{hash_color:06x}")
# git 信息: # git 信息:
@ -96,10 +86,11 @@ if __name__ == "__main__":
print(f"Branch: {file_branch_name}\n{border}\n{marker}\n") print(f"Branch: {file_branch_name}\n{border}\n{marker}\n")
else: else:
border = border_template.format("#000") # 淡绿色!
border = border_template.format("greenyellow")
# git 信息: # git 信息:
version_info = f"{branch}:{tag.split('-')[0]}" version_info = f"{branch}:{tag}"
marker = marker_template.format(version_info) marker = marker_template.format(version_info)
print(f"Master: {border}\n{marker}\n") print(f"Master: {border}\n{marker}\n")