diff --git a/backend/src/main/java/run/webup/platform/HostingController.java b/backend/src/main/java/run/webup/platform/HostingController.java index 51382a3..48367bc 100644 --- a/backend/src/main/java/run/webup/platform/HostingController.java +++ b/backend/src/main/java/run/webup/platform/HostingController.java @@ -43,7 +43,8 @@ public class HostingController { public record Deploy(@NotBlank @Size(max = 200) String idempotencyKey, UUID rollbackId) {} - public record SourceDeploy(@NotBlank @Size(max = 200) String idempotencyKey) {} + public record SourceDeploy( + @NotBlank @Size(max = 200) String idempotencyKey, Map environment) {} public record ServiceSpec( @NotBlank @Pattern(regexp = "[a-z][a-z0-9-]{0,62}") String name, @@ -199,10 +200,15 @@ public class HostingController { @Valid @RequestBody SourceDeploy request) { var existing = hosting.beforeSourceSnapshot(who.id(), project, request.idempotencyKey()); if (existing != null) return existing; + HostingService.validateSourceEnvironment(request.environment()); if (!runtime.request("/runtime/source-ready", null).path("ready").asBoolean(false)) throw new ApiError.Failure(503, "源码发布运行环境尚未启用,请联系管理员"); return hosting.deploySource( - who.id(), project, request.idempotencyKey(), sandbox.releaseSnapshot(project)); + who.id(), + project, + request.idempotencyKey(), + sandbox.releaseSnapshot(project), + request.environment()); } @GetMapping("/projects/{project}/deployments/{id}/events") diff --git a/backend/src/main/java/run/webup/platform/HostingService.java b/backend/src/main/java/run/webup/platform/HostingService.java index 4ae4e2f..0719367 100644 --- a/backend/src/main/java/run/webup/platform/HostingService.java +++ b/backend/src/main/java/run/webup/platform/HostingService.java @@ -163,15 +163,7 @@ public class HostingService { id, project) != 0)) throw new ApiError.Failure(409, "源码服务由平台管理,请使用「从源码发布」"); - if (s.environment() != null - && (s.environment().size() > 100 - || s.environment().entrySet().stream() - .anyMatch( - e -> - !e.getKey().matches("[A-Za-z_][A-Za-z0-9_]{0,100}") - || e.getValue() == null - || e.getValue().length() > 10000))) - throw new ApiError.Failure(400, "环境变量格式或数量无效"); + validateEnvironment(s.environment()); if (s.command().size() > 30 || s.command().stream().anyMatch(v -> v == null || v.length() > 5000)) throw new ApiError.Failure(400, "启动命令过长"); @@ -233,6 +225,26 @@ public class HostingService { : ((Timestamp) value).toInstant(); } + private static void validateEnvironment(Map environment) { + if (environment != null + && (environment.size() > 100 + || environment.entrySet().stream() + .anyMatch( + entry -> + !entry.getKey().matches("[A-Za-z_][A-Za-z0-9_]{0,100}") + || entry.getValue() == null + || entry.getValue().length() > 10000 + || entry.getValue().indexOf(0) >= 0))) + throw new ApiError.Failure(400, "环境变量格式或数量无效"); + } + + static void validateSourceEnvironment(Map environment) { + validateEnvironment(environment); + if (environment != null + && !Collections.disjoint(environment.keySet(), Set.of("HOST", "PORT", "HOME", "DATA_DIR"))) + throw new ApiError.Failure(400, "HOST、PORT、HOME、DATA_DIR 由平台配置"); + } + @Transactional public Map deploy(UUID user, UUID project, String key, UUID rollback) { own(user, project); @@ -280,7 +292,8 @@ public class HostingService { } @Transactional - public Map deploySource(UUID user, UUID project, String key, JsonNode snapshot) { + public Map deploySource( + UUID user, UUID project, String key, JsonNode snapshot, Map environment) { db.queryForList("SELECT id FROM projects WHERE id=? AND owner_id=? FOR UPDATE", project, user); var replay = beforeSourceSnapshot(user, project, key); if (replay != null) return replay; @@ -307,7 +320,7 @@ public class HostingService { UUID.randomUUID(), project, SOURCE_SERVICE, - vault.encrypt("{}"), + vault.encrypt(encode(environment == null ? Map.of() : environment)), cpu, memory); } else { @@ -318,6 +331,11 @@ public class HostingService { cpu, memory, existing.getFirst().get("id")); + if (environment != null) + db.update( + "UPDATE project_services SET encrypted_environment=? WHERE id=?", + vault.encrypt(encode(environment)), + existing.getFirst().get("id")); } return enqueueDeployment( user, diff --git a/backend/src/test/java/run/webup/platform/HostingIntegrationTest.java b/backend/src/test/java/run/webup/platform/HostingIntegrationTest.java index a2aca87..a5123e6 100644 --- a/backend/src/test/java/run/webup/platform/HostingIntegrationTest.java +++ b/backend/src/test/java/run/webup/platform/HostingIntegrationTest.java @@ -76,21 +76,64 @@ class HostingIntegrationTest { .put("runtime", "NODE") .put("install", false) .put("build", false); - var first = hosting.deploySource(user, project, "source", snapshot); + assertThrows( + ApiError.Failure.class, + () -> HostingService.validateSourceEnvironment(Map.of("PORT", "9999"))); + var first = + hosting.deploySource( + user, project, "source", snapshot, Map.of("ADMIN_PASSWORD", "fixture-secret")); assertEquals("SOURCE", first.get("release_type")); assertFalse(first.containsKey("source_json")); assertEquals(first.get("id"), hosting.beforeSourceSnapshot(user, project, "source").get("id")); assertEquals(true, hosting.services(user, project).getFirst().get("source_managed")); + assertFalse(hosting.services(user, project).getFirst().containsKey("encrypted_environment")); + assertFalse( + users + .db() + .queryForObject( + "SELECT spec_json FROM project_deployments WHERE id=?", + String.class, + first.get("id")) + .contains("fixture-secret")); var claimed = deployments.claim(); var payload = deployments.payload(claimed); assertEquals(snapshot, payload.path("source")); assertTrue(payload.path("services").get(0).path("source_managed").asBoolean()); + assertEquals( + "fixture-secret", + payload.path("services").get(0).path("environment").path("ADMIN_PASSWORD").asText()); deployments.finish( (UUID) first.get("id"), claimed.get("lease_token").toString(), json.createObjectNode().put("ready", true)); + var retained = hosting.deploySource(user, project, "retain-env", snapshot, null); + var next = deployments.claim(); + assertEquals( + "fixture-secret", + deployments + .payload(next) + .path("services") + .get(0) + .path("environment") + .path("ADMIN_PASSWORD") + .asText()); + deployments.finish( + (UUID) retained.get("id"), + next.get("lease_token").toString(), + json.createObjectNode().put("ready", true)); + var cleared = hosting.deploySource(user, project, "clear-env", snapshot, Map.of()); + var last = deployments.claim(); + assertTrue(deployments.payload(last).path("services").get(0).path("environment").isEmpty()); + deployments.finish( + (UUID) cleared.get("id"), + last.get("lease_token").toString(), + json.createObjectNode().put("ready", true)); hosting.deploy(user, project, "source-rollback", (UUID) first.get("id")); - assertEquals(snapshot, deployments.payload(deployments.claim()).path("source")); + var rollback = deployments.payload(deployments.claim()); + assertEquals(snapshot, rollback.path("source")); + assertEquals( + "fixture-secret", + rollback.path("services").get(0).path("environment").path("ADMIN_PASSWORD").asText()); } @Test diff --git a/frontend/src/views/Hosting.vue b/frontend/src/views/Hosting.vue index 18195c2..8d7a351 100644 --- a/frontend/src/views/Hosting.vue +++ b/frontend/src/views/Hosting.vue @@ -26,7 +26,8 @@ const form = reactive({ enabled: true, }); const command = ref("[]"), - environment = ref(""); + environment = ref(""), + sourceEnvironment = ref(""); const publication = ref<{ reviewStatus: string; archived: boolean }>(); const reviewLabels: Record = { PENDING: "待管理员审核", @@ -81,6 +82,7 @@ function selectProject() { output.value = ""; error.value = ""; success.value = ""; + sourceEnvironment.value = ""; void load(); } function edit(s?: any) { @@ -164,6 +166,19 @@ const sourceKeys = new Map(); async function deploy(rollbackId?: string, fromSource = false) { if (!canPublish.value || (!rollbackId && !fromSource && (!hasService.value || hasManagedSource.value)) || busy.value) return; const id = project.value; + let variables: Record | undefined; + if (fromSource && sourceEnvironment.value.trim()) { + try { + const value = JSON.parse(sourceEnvironment.value); + if (!value || Array.isArray(value) || typeof value !== "object" || + Object.values(value).some(item => typeof item !== "string")) + throw new Error(); + variables = value; + } catch { + error.value = "源码运行环境变量必须是 JSON 对象,所有值须为字符串。"; + return; + } + } if (!confirm(rollbackId ? "确认重新发布历史版本?" : fromSource ? "确认从当前 AI 工作区源码构建并发布?" : "确认发布当前服务配置?")) return; busy.value = true; @@ -175,10 +190,13 @@ async function deploy(rollbackId?: string, fromSource = false) { ...(fromSource ? { signal: AbortSignal.timeout(170000) } : {}), body: body({ idempotencyKey: fromSource ? sourceKeys.get(id) : crypto.randomUUID(), - ...(fromSource ? {} : { rollbackId: rollbackId || null }), + ...(fromSource ? { environment: variables } : { rollbackId: rollbackId || null }), }), }); - if (fromSource) sourceKeys.delete(id); + if (fromSource) { + sourceKeys.delete(id); + sourceEnvironment.value = ""; + } success.value = fromSource ? "已保存源码快照,等待自动构建与发布" : "部署已入队"; await load(); } catch (e) { @@ -272,6 +290,13 @@ onUnmounted(() => clearInterval(timer));

可直接从 AI 工作区发布源码。使用已有镜像时,需要先添加服务。

已有发布任务正在处理。

+
+ 源码运行环境变量 + +