Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: respect existing RAILS_ENV and SECRET_KEY_BASE #342

Merged
merged 5 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion precompile_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,35 @@ func (p PrecompileProcess) Execute(workingDir string) error {
Args: args,
Stdout: p.logger.ActionWriter,
Stderr: p.logger.ActionWriter,
Env: append(os.Environ(), "RAILS_ENV=production", "SECRET_KEY_BASE=dummy"),
Env: processPrecompileEnv(os.Environ()),
})
if err != nil {
return fmt.Errorf("failed to execute bundle exec output:\n%s\nerror: %s", buffer.String(), err)
}

return nil
}

func processPrecompileEnv(env []string) []string {
sophiewigmore marked this conversation as resolved.
Show resolved Hide resolved
hasRailsEnv := false
hasSecretKeyBase := false
for _, pair := range env {
if strings.HasPrefix(pair, "RAILS_ENV=") {
hasRailsEnv = true
}

if strings.HasPrefix(pair, "SECRET_KEY_BASE=") {
hasSecretKeyBase = true
}
}

if !hasRailsEnv {
env = append(env, "RAILS_ENV=production")
}

if !hasSecretKeyBase {
env = append(env, "SECRET_KEY_BASE=dummy")
}

return env
}
29 changes: 29 additions & 0 deletions precompile_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ func testPrecompileProcess(t *testing.T, context spec.G, it spec.S) {
Expect(executions[0].Env).To(ContainElement("SECRET_KEY_BASE=dummy"))
})

context("when a user sets their own RAILS_ENV", func() {
it.Before(func() {
Expect(os.Setenv("RAILS_ENV", "staging")).To(Succeed())
})
it.After(func() {
Expect(os.Unsetenv("RAILS_ENV")).To(Succeed())
})
it("runs the bundle exec assets:precompile process while respecting RAILS_ENV", func() {
err := precompileProcess.Execute(workingDir)
Expect(err).NotTo(HaveOccurred())

Expect(executions).To(HaveLen(1))
Expect(executions[0].Args).To(Equal([]string{"exec", "rails", "assets:precompile", "assets:clean"}))
Expect(executions[0].Env).To(ContainElement("RAILS_ENV=staging"))
Expect(executions[0].Env).To(ContainElement("SECRET_KEY_BASE=dummy"))
})

it("runs the bundle exec assets:precompile process while respecting SECRET_KEY_BASE", func() {
os.Setenv("SECRET_KEY_BASE", "dummy2")
err := precompileProcess.Execute(workingDir)
Expect(err).NotTo(HaveOccurred())

Expect(executions).To(HaveLen(1))
Expect(executions[0].Args).To(Equal([]string{"exec", "rails", "assets:precompile", "assets:clean"}))
Expect(executions[0].Env).To(ContainElement("RAILS_ENV=production"))
Expect(executions[0].Env).To(ContainElement("SECRET_KEY_BASE=dummy2"))
})
sophiewigmore marked this conversation as resolved.
Show resolved Hide resolved
})

context("failure cases", func() {
context("when bundle exec fails", func() {
it.Before(func() {
Expand Down