refactor: 从环境变量读取 REGISTRY_IMAGE / REPOSITORY_URL,去除硬编码
This commit is contained in:
@@ -13,3 +13,6 @@ AI_MODEL=GLM-4.7-Flash
|
||||
# 版本更新配置
|
||||
# 在 OneDev 中创建 Access Token (Settings → Access Tokens → New Token, scope: Read code)
|
||||
ONEDEV_ACCESS_TOKEN=Gtn1krR7tmv4x2c7xLupvW6OqgwWbmanpKonknpQ
|
||||
REGISTRY_IMAGE=recipe_tool:latest
|
||||
REPOSITORY_URL=https://git.hanhan.ltd/recipe_tool
|
||||
|
||||
|
||||
@@ -20,4 +20,6 @@ services:
|
||||
- AI_BASE_URL=${AI_BASE_URL:-https://api.openai.com/v1}
|
||||
- AI_MODEL=${AI_MODEL:-gpt-3.5-turbo}
|
||||
- ONEDEV_ACCESS_TOKEN=${ONEDEV_ACCESS_TOKEN}
|
||||
- REGISTRY_IMAGE=${REGISTRY_IMAGE:-recipe_tool:latest}
|
||||
- REPOSITORY_URL=${REPOSITORY_URL:-https://git.hanhan.ltd/recipe_tool}
|
||||
- GIT_REMOTE_URL=https://git.hanhan.ltd/recipe_tool.git
|
||||
|
||||
+15
-5
@@ -333,16 +333,24 @@ async function checkDockerAvailable(): Promise<{ ok: boolean; message: string }>
|
||||
* 执行 Docker 容器更新
|
||||
* @param onLog 日志回调
|
||||
*/
|
||||
const REGISTRY_IMAGE = "git.hanhan.ltd/recipe_tool/recipe_tool:latest";
|
||||
|
||||
export async function executeUpdate(
|
||||
onLog: LogCallback = noopLog
|
||||
): Promise<{ success: boolean; message: string }> {
|
||||
const cwd = process.cwd(); // /app(容器内工作目录,含 docker-compose.yml)
|
||||
const token = process.env.ONEDEV_ACCESS_TOKEN || "";
|
||||
const repoUrl = process.env.REPOSITORY_URL || "https://git.hanhan.ltd/recipe_tool";
|
||||
const registryImage = process.env.REGISTRY_IMAGE || "recipe_tool:latest";
|
||||
|
||||
// 从 REPOSITORY_URL 提取 registry host 和 project path 以拼出完整镜像地址
|
||||
// 例如: https://git.hanhan.ltd/recipe_tool → host=git.hanhan.ltd, path=recipe_tool
|
||||
// 完整镜像: git.hanhan.ltd/recipe_tool/recipe_tool:latest
|
||||
const repoUrlObj = new URL(repoUrl);
|
||||
const registryHost = repoUrlObj.host;
|
||||
const projectPath = repoUrlObj.pathname.replace(/^\/|\/$/g, "");
|
||||
const fullImage = `${registryHost}/${projectPath}/${registryImage}`;
|
||||
|
||||
consoleLog("执行", "=== 开始执行更新 ===");
|
||||
consoleLog("执行", `工作目录: ${cwd}, 镜像: ${REGISTRY_IMAGE}`);
|
||||
consoleLog("执行", `工作目录: ${cwd}, 镜像: ${fullImage}`);
|
||||
|
||||
const logLine = (line: string) => onLog(line);
|
||||
|
||||
@@ -366,7 +374,7 @@ export async function executeUpdate(
|
||||
if (token) {
|
||||
await execAsync(
|
||||
"sh",
|
||||
["-c", `echo "${token}" | docker login git.hanhan.ltd -u access-token --password-stdin`],
|
||||
["-c", `echo "${token}" | docker login ${registryHost} -u access-token --password-stdin`],
|
||||
"docker-login",
|
||||
{ timeout: 15000, onLine: logLine }
|
||||
);
|
||||
@@ -377,7 +385,7 @@ export async function executeUpdate(
|
||||
|
||||
onLog("[1/4] 拉取新镜像");
|
||||
consoleLog("执行", "步骤 1/4 — 拉取镜像");
|
||||
await execAsync("docker", ["pull", REGISTRY_IMAGE], "docker-pull", {
|
||||
await execAsync("docker", ["pull", fullImage], "docker-pull", {
|
||||
timeout: 300000,
|
||||
onLine: logLine,
|
||||
});
|
||||
@@ -396,6 +404,8 @@ export async function executeUpdate(
|
||||
`AI_BASE_URL=${process.env.AI_BASE_URL || "https://api.openai.com/v1"}`,
|
||||
`AI_MODEL=${process.env.AI_MODEL || "gpt-3.5-turbo"}`,
|
||||
`ONEDEV_ACCESS_TOKEN=${token}`,
|
||||
`REGISTRY_IMAGE=${registryImage}`,
|
||||
`REPOSITORY_URL=${repoUrl}`,
|
||||
`GIT_REMOTE_URL=${process.env.GIT_REMOTE_URL || "https://git.hanhan.ltd/recipe_tool.git"}`,
|
||||
].join("\n");
|
||||
fs.writeFileSync(path.join(cwd, ".env"), envContent, "utf-8");
|
||||
|
||||
Reference in New Issue
Block a user