From d2ee77434e0a37576cd3013dcb54696eebe5b9f2 Mon Sep 17 00:00:00 2001 From: Ralf Pannemans Date: Fri, 27 Sep 2024 13:41:08 +0200 Subject: [PATCH] Introduce BP_KEEP_NODE_BUILD_CACHE to safeguard existing behaviour --- README.md | 7 ++++--- build.go | 21 ++++++++++++--------- buildpack.toml | 6 ++++++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5af3b5d0b..827a2b132 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,10 @@ file that looks like the following: ## Configuration -| Environment Variable | Description | -|-------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `$BP_NPM_VERSION` | If set, this custom version of `npm` will be used instead of the one provided by the `nodejs` installation. | +| Environment Variable | Description | +|-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `$BP_NPM_VERSION` | If set, this custom version of `npm` will be used instead of the one provided by the `nodejs` installation. | +| `$BP_KEEP_NODE_BUILD_CACHE` | If set to `true` (default `false`), the folder `node_modules/.cache` will not be removed after the build, but will be readonly at runtime. | ## Usage diff --git a/build.go b/build.go index 760d11a74..e43e8f47a 100644 --- a/build.go +++ b/build.go @@ -284,16 +284,19 @@ func Build(entryResolver EntryResolver, return packit.BuildResult{}, err } - linkName := filepath.Join(layer.Path, "node_modules", ".cache") - err = os.RemoveAll(linkName) - if err != nil { - return packit.BuildResult{}, err - } + keepBuildCache, _ := environment.Lookup("BP_KEEP_NODE_BUILD_CACHE") + if keepBuildCache != "true" { + linkName := filepath.Join(layer.Path, "node_modules", ".cache") + err = os.RemoveAll(linkName) + if err != nil { + return packit.BuildResult{}, err + } - cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE) - err = os.Symlink(cacheFolder, linkName) - if err != nil { - return packit.BuildResult{}, err + cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE) + err = os.Symlink(cacheFolder, linkName) + if err != nil { + return packit.BuildResult{}, err + } } if build { diff --git a/buildpack.toml b/buildpack.toml index b18193c9b..9f9d8dcbf 100644 --- a/buildpack.toml +++ b/buildpack.toml @@ -32,6 +32,11 @@ api = "0.7" name = "BP_NPM_VERSION" description = "configures a custom npm version" + [[metadata.configurations]] + name = "BP_KEEP_NODE_BUILD_CACHE" + default = "false" + description = "keep the 'node_modules/.cache' folder after the build (will be readonly at runtime)'" + [[metadata.configurations]] name = "NODE_HOME" description = "path the Node.js installation" @@ -45,5 +50,6 @@ api = "0.7" default = "error" description = "configures npm output verbosity" + [[stacks]] id = "*"