Skip to content

Commit 684839b

Browse files
committed
Create temp .env file during compose deployments
1 parent 225bb29 commit 684839b

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

pkg/dockerapi/compose.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -195,27 +195,43 @@ func ComposeLogs(req *DockerComposeLogs, ws *websocket.Conn) error {
195195
return nil
196196
}
197197

198-
func createTempComposeFile(projectName string, definition string) (string, string, error) {
198+
func createTempComposeFile(projectName string, definition string, variables map[string]store.VariableValue) (string, string, string, error) {
199199
dir, err := os.MkdirTemp("", projectName)
200200
if err != nil {
201201
log.Error().Err(err).Msg("Error while creating temp directory for compose")
202-
return "", "", err
202+
return "", "", "", err
203203
}
204204

205-
filename := filepath.Join(dir, "compose.yaml")
206-
composeFile, err := os.Create(filename)
205+
composeFilename := filepath.Join(dir, "compose.yaml")
206+
composeFile, err := os.Create(composeFilename)
207207
if err != nil {
208208
log.Error().Err(err).Msg("Error while creating temp compose file")
209-
return "", "", err
209+
return "", "", "", err
210210
}
211211

212212
_ , err = composeFile.WriteString(definition)
213213
if err != nil {
214214
log.Error().Err(err).Msg("Error while writing to temp compose file")
215-
return "", "", err
215+
return "", "", "", err
216216
}
217217

218-
return dir, filename, nil
218+
envFilename := filepath.Join(dir, ".env")
219+
envFile, err := os.Create(envFilename)
220+
if err != nil {
221+
log.Error().Err(err).Msg("Error while creating temp compose file")
222+
return "", "", "", err
223+
}
224+
225+
envVars := toEnvFormat(variables)
226+
for _, v := range envVars {
227+
_ , err = envFile.WriteString(v + "\r\n")
228+
if err != nil {
229+
log.Error().Err(err).Msg("Error while writing to temp .env file")
230+
return "", "", "", err
231+
}
232+
}
233+
234+
return dir, composeFilename, envFilename, nil
219235
}
220236

221237
func toEnvFormat(variables map[string]store.VariableValue) ([]string) {
@@ -258,24 +274,24 @@ func processVars(cmd *exec.Cmd, variables map[string]store.VariableValue, ws *we
258274
}
259275

260276
func performComposeAction(action string, projectName string, definition string, variables map[string]store.VariableValue, ws *websocket.Conn, printVars bool) error {
261-
dir, file, err := createTempComposeFile(projectName, definition)
262-
log.Debug().Str("fileName", file).Msg("Created temporary compose file")
277+
dir, composefile, envfile, err := createTempComposeFile(projectName, definition, variables)
278+
log.Debug().Str("composeFileName", composefile).Str("envFileName", envfile).Msg("Created temporary compose file and .env file")
263279
if err != nil {
264280
return err
265281
}
266282
defer func() {
267-
log.Debug().Str("fileName", file).Msg("Deleting temporary compose file")
283+
log.Debug().Str("fileName", composefile).Msg("Deleting temporary compose file and .env file")
268284
os.RemoveAll(dir)
269285
}()
270286

271287
var cmd *exec.Cmd
272288
switch action {
273289
case "up":
274-
cmd = exec.Command("docker-compose", "-p", projectName, "-f", file, action, "-d")
290+
cmd = exec.Command("docker-compose", "-p", projectName, "-f", composefile, action, "-d")
275291
case "down":
276292
cmd = exec.Command("docker-compose", "-p", projectName, action)
277293
case "pull":
278-
cmd = exec.Command("docker-compose", "-p", projectName, "-f", file, action)
294+
cmd = exec.Command("docker-compose", "-p", projectName, "-f", composefile, action)
279295
default:
280296
panic(fmt.Errorf("unknown compose action %s", action))
281297
}

0 commit comments

Comments
 (0)