Skip to content

Commit

Permalink
feat: add env var to configure webroot
Browse files Browse the repository at this point in the history
this adds a new env var that overrides the webroot and will also allow
it to pass detection even if there is no `public.html` in any of the
expected places.
  • Loading branch information
ctrox committed Dec 15, 2023
1 parent 4c248d9 commit cb5fdbd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
This buildpack complements the
[`buildpack-static-confgen`](https://github.com/ninech/buildpack-static-confgen)
buildpack, see the `README.md` on why this can't be in a single buildpack.
This simply requires nginx if an `index.html` can be found in the workspace.
The build phase is a noop.
This simply requires nginx if an `index.html` can be found in the workspace or
if the env var `BP_STATIC_WEBROOT` is configured. The build phase is a noop.

To test the build locally, checkout this repository and then build it with:

Expand Down
10 changes: 9 additions & 1 deletion detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/paketo-buildpacks/packit/v2/scribe"
)

const indexFile = "index.html"
const (
indexFile = "index.html"
webRootEnv = "BP_STATIC_WEBROOT"
)

type BuildPlanMetadata struct {
Version string `toml:"version,omitempty"`
Expand Down Expand Up @@ -40,6 +43,11 @@ func Detect(logger scribe.Emitter) packit.DetectFunc {
}

func WebRoot(workingDir string) (string, error) {
// the webroot can be set using an env var
if v, ok := os.LookupEnv(webRootEnv); ok {
return v, nil
}

paths := []string{"./", "./public"}
publicDir := ""
for _, path := range paths {
Expand Down
16 changes: 16 additions & 0 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,20 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
Expect(err).To(MatchError(packit.Fail.WithMessage("no index.html found")))
})
})

context("when webroot is set via env", func() {
it.Before(func() {
t.Setenv(webRootEnv, "custom")
})

it("requires nginx", func() {
result, err := Detect(scribe.NewEmitter(buffer))(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).NotTo(HaveOccurred())
Expect(result.Plan.Requires).To(ContainElement(packit.BuildPlanRequirement{
Name: nginx.NGINX, Metadata: BuildPlanMetadata{Launch: true},
}))
})
})
}
2 changes: 1 addition & 1 deletion init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestUnitBuildpackStatic(t *testing.T) {
suite := spec.New("buildpack-static-require", spec.Report(report.Terminal{}), spec.Parallel())
suite := spec.New("buildpack-static-require", spec.Report(report.Terminal{}))
suite("Detect", testDetect)
suite.Run(t)
}

0 comments on commit cb5fdbd

Please sign in to comment.