Skip to content

Commit 5c862f7

Browse files
committed
fix: add createEnvFile toggle to Compose, mirroring Application
1 parent 1de7b51 commit 5c862f7

8 files changed

Lines changed: 9040 additions & 3 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { getBuildComposeCommand } from "@dokploy/server/utils/builders/compose";
2+
import { describe, expect, it, vi } from "vitest";
3+
4+
// Compose now has a `createEnvFile` toggle (default true), mirroring the
5+
// Application builder's flag: when disabled, Dokploy never writes `.env`,
6+
// so a repo-tracked file survives untouched.
7+
vi.mock("@dokploy/server/utils/docker/domain", () => ({
8+
writeDomainsToCompose: vi.fn().mockResolvedValue(""),
9+
}));
10+
11+
const baseCompose = {
12+
appName: "env-file-toggle",
13+
sourceType: "raw",
14+
command: "",
15+
composePath: "docker-compose.yml",
16+
composeType: "docker-compose",
17+
isolatedDeployment: false,
18+
randomize: false,
19+
suffix: "",
20+
serverId: null,
21+
env: "FOO=bar",
22+
mounts: [],
23+
domains: [],
24+
environment: { project: { env: "" }, env: "" },
25+
} as unknown as Parameters<typeof getBuildComposeCommand>[0];
26+
27+
describe("getBuildComposeCommand createEnvFile toggle", () => {
28+
it("createEnvFile: false never writes the .env file", async () => {
29+
const command = await getBuildComposeCommand({
30+
...baseCompose,
31+
createEnvFile: false,
32+
});
33+
34+
expect(command).not.toContain("base64 -d >");
35+
});
36+
37+
it("createEnvFile: true (default) writes Dokploy's vars", async () => {
38+
const command = await getBuildComposeCommand({
39+
...baseCompose,
40+
createEnvFile: true,
41+
});
42+
43+
expect(command).toContain("base64 -d >");
44+
});
45+
});

‎apps/dokploy/components/dashboard/application/environment/show-environment.tsx‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ import {
1616
import {
1717
Form,
1818
FormControl,
19+
FormDescription,
1920
FormField,
2021
FormItem,
22+
FormLabel,
2123
FormMessage,
2224
} from "@/components/ui/form";
25+
import { Switch } from "@/components/ui/switch";
2326
import { Toggle } from "@/components/ui/toggle";
2427
import { api } from "@/utils/api";
2528
import type { ServiceType } from "../advanced/show-resources";
2629

2730
const addEnvironmentSchema = z.object({
2831
environment: z.string(),
32+
createEnvFile: z.boolean(),
2933
});
3034

3135
type EnvironmentSchema = z.infer<typeof addEnvironmentSchema>;
@@ -71,18 +75,32 @@ export const ShowEnvironment = ({ id, type }: Props) => {
7175
const form = useForm<EnvironmentSchema>({
7276
defaultValues: {
7377
environment: "",
78+
createEnvFile: true,
7479
},
7580
resolver: zodResolver(addEnvironmentSchema),
7681
});
7782

7883
// Watch form value
7984
const currentEnvironment = form.watch("environment");
80-
const hasChanges = currentEnvironment !== (data?.env || "");
85+
const currentCreateEnvFile = form.watch("createEnvFile");
86+
const composeData =
87+
type === "compose"
88+
? (data as { createEnvFile?: boolean; sourceType?: string } | undefined)
89+
: undefined;
90+
91+
const showCreateEnvFileToggle =
92+
type === "compose" &&
93+
(composeData?.sourceType !== "raw" || composeData?.createEnvFile === false);
94+
const hasChanges =
95+
currentEnvironment !== (data?.env || "") ||
96+
(showCreateEnvFileToggle &&
97+
currentCreateEnvFile !== (composeData?.createEnvFile ?? true));
8198

8299
useEffect(() => {
83100
if (data) {
84101
form.reset({
85102
environment: data.env || "",
103+
createEnvFile: composeData?.createEnvFile ?? true,
86104
});
87105
}
88106
}, [data, form]);
@@ -97,6 +115,9 @@ export const ShowEnvironment = ({ id, type }: Props) => {
97115
postgresId: id || "",
98116
redisId: id || "",
99117
env: formData.environment,
118+
...(type === "compose" && {
119+
createEnvFile: formData.createEnvFile,
120+
}),
100121
})
101122
.then(async () => {
102123
toast.success("Environments Added");
@@ -110,6 +131,7 @@ export const ShowEnvironment = ({ id, type }: Props) => {
110131
const handleCancel = () => {
111132
form.reset({
112133
environment: data?.env || "",
134+
createEnvFile: composeData?.createEnvFile ?? true,
113135
});
114136
};
115137

@@ -190,6 +212,33 @@ PORT=3000
190212
)}
191213
/>
192214

215+
{showCreateEnvFileToggle && (
216+
<FormField
217+
control={form.control}
218+
name="createEnvFile"
219+
render={({ field }) => (
220+
<FormItem className="flex flex-row items-center justify-between p-3 border rounded-lg shadow-xs">
221+
<div className="space-y-0.5">
222+
<FormLabel>Create Environment File</FormLabel>
223+
<FormDescription>
224+
When enabled, an .env file will be created in the same
225+
directory as your compose file on every deploy.
226+
Disable this if you don't want to generate an
227+
environment file.
228+
</FormDescription>
229+
</div>
230+
<FormControl>
231+
<Switch
232+
checked={field.value}
233+
onCheckedChange={field.onChange}
234+
disabled={!canWrite}
235+
/>
236+
</FormControl>
237+
</FormItem>
238+
)}
239+
/>
240+
)}
241+
193242
{canWrite && (
194243
<div className="flex flex-row justify-end gap-2">
195244
{hasChanges && (
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "compose" ADD COLUMN "createEnvFile" boolean DEFAULT true NOT NULL;

0 commit comments

Comments
 (0)