3v324v23 commited on
Commit
7701804
·
1 Parent(s): f1bb04f
Files changed (2) hide show
  1. Dockerfile +3 -0
  2. clone_and_run.py +7 -8
Dockerfile CHANGED
@@ -16,5 +16,8 @@ COPY clone_and_run.py .
16
  RUN useradd -m appuser && chown -R appuser:appuser /app
17
  USER appuser
18
 
 
 
 
19
  # 启动命令
20
  CMD ["python", "clone_and_run.py"]
 
16
  RUN useradd -m appuser && chown -R appuser:appuser /app
17
  USER appuser
18
 
19
+ # 设置环境变量
20
+ ENV PATH="/home/appuser/.local/bin:${PATH}"
21
+
22
  # 启动命令
23
  CMD ["python", "clone_and_run.py"]
clone_and_run.py CHANGED
@@ -3,17 +3,13 @@ import subprocess
3
  import shutil
4
 
5
  def clone_repo(token, repo_url, target_dir):
6
- """常规方式克隆私有仓库"""
7
  try:
8
- # 设置 Git 凭证助手(临时存储 Token)
9
- subprocess.run(["git", "config", "--global", "credential.helper", "store"], check=True)
10
-
11
- # 将 Token 写入 Git 凭证文件
12
- with open(os.path.expanduser("~/.git-credentials"), "w") as f:
13
- f.write(f"https://{token}:x-oauth-basic@github.com\n")
14
 
15
  # 克隆仓库
16
- subprocess.run(["git", "clone", repo_url, target_dir], check=True)
17
 
18
  # 清理 .git 目录
19
  shutil.rmtree(os.path.join(target_dir, ".git"))
@@ -41,5 +37,8 @@ if __name__ == "__main__":
41
  # 安装依赖
42
  subprocess.run(["pip", "install", "--no-cache-dir", "-r", "requirements.txt"], check=True)
43
 
 
 
 
44
  # 启动 FastAPI 服务
45
  subprocess.run(["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"])
 
3
  import shutil
4
 
5
  def clone_repo(token, repo_url, target_dir):
6
+ """安全克隆私有仓库"""
7
  try:
8
+ # 构造认证 URL
9
+ auth_url = repo_url.replace("https://", f"https://x-access-token:{token}@")
 
 
 
 
10
 
11
  # 克隆仓库
12
+ subprocess.run(["git", "clone", auth_url, target_dir], check=True)
13
 
14
  # 清理 .git 目录
15
  shutil.rmtree(os.path.join(target_dir, ".git"))
 
37
  # 安装依赖
38
  subprocess.run(["pip", "install", "--no-cache-dir", "-r", "requirements.txt"], check=True)
39
 
40
+ # 将用户二进制目录添加到 PATH
41
+ os.environ["PATH"] = f"/home/appuser/.local/bin:{os.environ['PATH']}"
42
+
43
  # 启动 FastAPI 服务
44
  subprocess.run(["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"])