Magpie/scripts/wiki.py
2025-08-10 20:33:02 +08:00

52 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import os
import tempfile
import glob
import shutil
import subprocess
import argparse
try:
# https://docs.github.com/en/actions/learn-github-actions/variables
if os.environ["GITHUB_ACTIONS"].lower() == "true":
# 不知为何在 Github Actions 中运行时默认编码为 ANSI并且 print 需刷新流才能正常显示
for stream in [sys.stdout, sys.stderr]:
stream.reconfigure(encoding="utf-8")
except:
pass
argParser = argparse.ArgumentParser()
argParser.add_argument("access_token")
args = argParser.parse_args()
wikiRepoUrl = f'https://{args.access_token}@github.com/{os.environ["GITHUB_REPOSITORY"]}.wiki.git'
# 创建临时目录
wikiRepoDir = tempfile.mkdtemp()
os.chdir(wikiRepoDir)
p = subprocess.run("git init")
if p.returncode != 0:
raise Exception("git init 失败")
actor = os.environ["GITHUB_ACTOR"]
subprocess.run("git config user.name " + actor)
subprocess.run(f"git config user.email {actor}@users.noreply.github.com")
# 拉取
p = subprocess.run(f'git pull "{wikiRepoUrl}"')
if p.returncode != 0:
raise Exception("git pull 失败")
# 将文档拷贝到临时目录
docsDir = os.path.normpath(os.path.dirname(__file__) + "\\..\\docs")
for file in glob.glob(docsDir + "\\*.md"):
shutil.copy(file, wikiRepoDir)
print("已拷贝 " + file, flush=True)
# 推送
subprocess.run("git add .")
subprocess.run('git commit -m "Published by CI"')
p = subprocess.run(f'git push --set-upstream "{wikiRepoUrl}" master')
if p.returncode != 0:
raise Exception("git push 失败")