Skip to content

Commit

Permalink
feat: respect existing RAILS_ENV and SECRET_KEY_BASE
Browse files Browse the repository at this point in the history
This change allows users to set alternative values for RAILS_ENV and SECRET_KEY_BASE when precompiling assets.

Closes paketo-buildpacks#200
  • Loading branch information
josegonzalez committed Jun 20, 2023
1 parent d7e9a6a commit ff9acca
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
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 {
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
}
38 changes: 38 additions & 0 deletions precompile_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func testPrecompileProcess(t *testing.T, context spec.G, it spec.S) {
executable *fakes.Executable

precompileProcess railsassets.PrecompileProcess

hasRailsEnv bool
hasSecretKeyBase bool
railsEnv string
secretKeyBase string
)

it.Before(func() {
Expand All @@ -43,9 +48,20 @@ func testPrecompileProcess(t *testing.T, context spec.G, it spec.S) {
logger := scribe.NewEmitter(bytes.NewBuffer(nil))

precompileProcess = railsassets.NewPrecompileProcess(executable, logger)

railsEnv, hasRailsEnv = os.LookupEnv("RAILS_ENV")
secretKeyBase, hasSecretKeyBase = os.LookupEnv("SECRET_KEY_BASE")
})

it.After(func() {
if hasRailsEnv {
os.Setenv("RAILS_ENV", railsEnv)
}

if hasSecretKeyBase {
os.Setenv("SECRET_KEY_BASE", secretKeyBase)
}

Expect(os.RemoveAll(workingDir)).To(Succeed())
})

Expand All @@ -59,6 +75,28 @@ func testPrecompileProcess(t *testing.T, context spec.G, it spec.S) {
Expect(executions[0].Env).To(ContainElement("SECRET_KEY_BASE=dummy"))
})

it("runs the bundle exec assets:precompile process while respecting RAILS_ENV", func() {
os.Setenv("RAILS_ENV", "staging")
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"))
})

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

0 comments on commit ff9acca

Please sign in to comment.