Skip to content

Commit 2739690

Browse files
dmtrTmclaude
andcommitted
fix: resolve environment variables on application rollback
rollbackApplication() called prepareEnvironmentVariables() without the environment env, so every ${{environment.*}} reference threw "Invalid environment variable: environment.X" and the rollback failed with an opaque 400 before the swarm service was updated. fb749cd added the third argument to every other call site but left services/rollbacks.ts, which had been calling the helper since 24bff96. The value is already captured in the snapshot by createRollback(); both fullContext declarations simply typed environment as { project: Project }, so the missing argument was invisible to the compiler. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent d768adb commit 2739690

3 files changed

Lines changed: 113 additions & 2 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { prepareEnvironmentVariables } from "@dokploy/server/index";
2+
import { describe, expect, it } from "vitest";
3+
4+
const projectEnv = `
5+
ENVIRONMENT=staging
6+
DATABASE_URL=postgres://postgres:postgres@localhost:5432/project_db
7+
`;
8+
9+
const environmentEnv = `
10+
NODE_ENV=production
11+
POSTGRES_HOST=postgres.internal
12+
POSTGRES_PORT=5432
13+
REDIS_URL=redis://redis.internal:6379
14+
`;
15+
16+
const serviceEnv = `
17+
NODE_ENV=\${{environment.NODE_ENV}}
18+
REDIS_URL=\${{environment.REDIS_URL}}
19+
PORT=3000
20+
`;
21+
22+
/**
23+
* A rollback replays the snapshot stored in `rollbacks.fullContext`, which keeps
24+
* the service env, the environment env and the project env captured at deploy time.
25+
*/
26+
const fullContext = {
27+
env: serviceEnv,
28+
environment: {
29+
env: environmentEnv,
30+
project: {
31+
env: projectEnv,
32+
},
33+
},
34+
};
35+
36+
describe("prepareEnvironmentVariables for application rollback", () => {
37+
it("resolves environment variables from the rollback snapshot", () => {
38+
const result = prepareEnvironmentVariables(
39+
fullContext.env,
40+
fullContext.environment.project.env,
41+
fullContext.environment.env,
42+
);
43+
44+
expect(result).toEqual([
45+
"NODE_ENV=production",
46+
"REDIS_URL=redis://redis.internal:6379",
47+
"PORT=3000",
48+
]);
49+
});
50+
51+
it("resolves project and environment variables together on rollback", () => {
52+
const rollbackEnv = `
53+
DATABASE_URL=\${{project.DATABASE_URL}}
54+
POSTGRES_URL=postgres://\${{environment.POSTGRES_HOST}}:\${{environment.POSTGRES_PORT}}/app
55+
ENVIRONMENT=\${{project.ENVIRONMENT}}
56+
`;
57+
58+
const result = prepareEnvironmentVariables(
59+
rollbackEnv,
60+
projectEnv,
61+
environmentEnv,
62+
);
63+
64+
expect(result).toEqual([
65+
"DATABASE_URL=postgres://postgres:postgres@localhost:5432/project_db",
66+
"POSTGRES_URL=postgres://postgres.internal:5432/app",
67+
"ENVIRONMENT=staging",
68+
]);
69+
});
70+
71+
it("throws when the environment env of the snapshot is not passed", () => {
72+
expect(() =>
73+
prepareEnvironmentVariables(fullContext.env, projectEnv),
74+
).toThrow("Invalid environment variable: environment.NODE_ENV");
75+
});
76+
77+
it("maintains precedence: service > environment > project on rollback", () => {
78+
const conflictingProjectEnv = `
79+
NODE_ENV=project
80+
API_URL=https://project.api.com
81+
`;
82+
83+
const conflictingEnvironmentEnv = `
84+
NODE_ENV=environment
85+
API_URL=https://environment.api.com
86+
`;
87+
88+
const rollbackEnv = `
89+
NODE_ENV=service
90+
PROJECT_API_URL=\${{project.API_URL}}
91+
ENVIRONMENT_API_URL=\${{environment.API_URL}}
92+
SELF_REFERENCE=\${{NODE_ENV}}
93+
`;
94+
95+
const result = prepareEnvironmentVariables(
96+
rollbackEnv,
97+
conflictingProjectEnv,
98+
conflictingEnvironmentEnv,
99+
);
100+
101+
expect(result).toEqual([
102+
"NODE_ENV=service",
103+
"PROJECT_API_URL=https://project.api.com",
104+
"ENVIRONMENT_API_URL=https://environment.api.com",
105+
"SELF_REFERENCE=service",
106+
]);
107+
});
108+
});

‎packages/server/src/db/schema/rollbacks.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Application } from "@dokploy/server/services/application";
2+
import type { Environment } from "@dokploy/server/services/environment";
23
import type { Mount } from "@dokploy/server/services/mount";
34
import type { Port } from "@dokploy/server/services/port";
45
import type { Project } from "@dokploy/server/services/project";
@@ -27,7 +28,7 @@ export const rollbacks = pgTable("rollback", {
2728
.$defaultFn(() => new Date().toISOString()),
2829
fullContext: jsonb("fullContext").$type<
2930
Application & {
30-
environment: {
31+
environment: Environment & {
3132
project: Project;
3233
};
3334
mounts: Mount[];

‎packages/server/src/services/rollbacks.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { execAsync, execAsyncRemote } from "../utils/process/execAsync";
1919
import { getRemoteDocker } from "../utils/servers/remote-docker";
2020
import { type Application, findApplicationById } from "./application";
2121
import { findDeploymentById } from "./deployment";
22+
import type { Environment } from "./environment";
2223
import type { Mount } from "./mount";
2324
import { resolveServiceNetworks } from "./network";
2425
import type { Port } from "./port";
@@ -214,7 +215,7 @@ const rollbackApplication = async (
214215
image: string,
215216
serverId?: string | null,
216217
fullContext?: Application & {
217-
environment: {
218+
environment: Environment & {
218219
project: Project;
219220
};
220221
mounts: Mount[];
@@ -279,6 +280,7 @@ const rollbackApplication = async (
279280
const envVariables = prepareEnvironmentVariables(
280281
env,
281282
fullContext.environment.project.env,
283+
fullContext.environment.env,
282284
);
283285

284286
let rollbackImage = image;

0 commit comments

Comments
 (0)