test 5
Browse files- Dockerfile +3 -0
- 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 |
-
#
|
| 9 |
-
|
| 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",
|
| 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"])
|