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 = '
' marker_template = '