add nextjs and node_modules caches

This commit is contained in:
Jake Runzer
2025-02-11 12:01:38 -07:00
parent 08e5fec01b
commit 0fed8be4d0
3 changed files with 35 additions and 6 deletions
+1 -2
View File
@@ -402,6 +402,7 @@ func (g *BuildGraph) convertFileCommandToLLB(cmd plan.FileCommand, state llb.Sta
// getCacheMountOptions returns the llb.RunOption slice for the given cache keys
func (g *BuildGraph) getCacheMountOptions(cacheKeys []string) ([]llb.RunOption, error) {
var opts []llb.RunOption
for _, cacheKey := range cacheKeys {
if planCache, ok := g.Plan.Caches[cacheKey]; ok {
cache := g.CacheStore.GetCache(cacheKey, planCache)
@@ -413,8 +414,6 @@ func (g *BuildGraph) getCacheMountOptions(cacheKeys []string) ([]llb.RunOption,
opts = append(opts,
llb.AddMount(planCache.Directory, *cache.cacheState, llb.AsPersistentCacheDir(cache.cacheKey, cacheType)),
)
return opts, nil
} else {
return nil, fmt.Errorf("cache with key %q not found", cacheKey)
}
+16 -3
View File
@@ -1,6 +1,10 @@
package generate
import "github.com/railwayapp/railpack/core/plan"
import (
"strings"
"github.com/railwayapp/railpack/core/plan"
)
const (
APT_CACHE_KEY = "apt"
@@ -18,8 +22,9 @@ func NewCacheContext() *CacheContext {
}
func (c *CacheContext) AddCache(name string, directory string) string {
c.Caches[name] = plan.NewCache(directory)
return name
sanitizedName := sanitizeCacheName(name)
c.Caches[sanitizedName] = plan.NewCache(directory)
return sanitizedName
}
func (c *CacheContext) SetCache(name string, cache *plan.Cache) {
@@ -46,3 +51,11 @@ func (c *CacheContext) GetAptCaches() []string {
return []string{APT_CACHE_KEY, aptListsKey}
}
func sanitizeCacheName(name string) string {
if len(name) > 0 && name[0] == '/' {
name = name[1:]
}
name = strings.TrimRight(name, "/")
return strings.ReplaceAll(name, "/", "-")
}
+18 -1
View File
@@ -2,6 +2,7 @@ package node
import (
"fmt"
"path"
"strings"
"github.com/railwayapp/railpack/core/app"
@@ -74,11 +75,27 @@ func (p *NodeProvider) Build(ctx *generate.GenerateContext, install *generate.Co
packageManager := p.getPackageManager(ctx.App)
_, ok := packageJson.Scripts["build"]
if ok {
buildCaches := []string{}
// Generic node_modules cache
buildCaches = append(buildCaches, ctx.Caches.AddCache("node-modules", "/app/node_modules/.cache"))
// Add caches for Next.JS apps
if nextApps, err := p.getNextApps(ctx); err == nil {
for _, nextApp := range nextApps {
nextCacheDir := path.Join("/app", nextApp, ".next/cache")
nextCache := ctx.Caches.AddCache(fmt.Sprintf("next-%s", nextApp), nextCacheDir)
buildCaches = append(buildCaches, nextCache)
}
}
build := ctx.NewCommandStep("build")
build.AddCommands([]plan.Command{
plan.NewCopyCommand("."),
plan.NewExecCommand(packageManager.RunCmd("build")),
plan.NewExecCommand(packageManager.RunCmd("build"), plan.ExecOptions{
Caches: buildCaches,
}),
})
build.DependsOn = []string{install.DisplayName}