Skip to content

Commit

Permalink
Merge pull request #16 from railwayapp/jr/nextjs-caches
Browse files Browse the repository at this point in the history
add nextjs and node_modules caches
  • Loading branch information
coffee-cup authored Feb 11, 2025
2 parents 93222e2 + dbc061d commit 85ccb32
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
3 changes: 1 addition & 2 deletions buildkit/build_llb/build_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
59 changes: 58 additions & 1 deletion core/__snapshots__/core_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@
"mise": {
"directory": "/mise/cache",
"type": "shared"
},
"node-modules": {
"directory": "/app/node_modules/.cache",
"type": "shared"
}
},
"start": {
Expand Down Expand Up @@ -615,6 +619,9 @@
"src": "."
},
{
"caches": [
"node-modules"
],
"cmd": "bun run build"
}
],
Expand Down Expand Up @@ -643,6 +650,10 @@
"directory": "/mise/cache",
"type": "shared"
},
"node-modules": {
"directory": "/app/node_modules/.cache",
"type": "shared"
},
"pnpm-install": {
"directory": "/root/.local/share/pnpm/store/v3",
"type": "shared"
Expand Down Expand Up @@ -800,6 +811,9 @@
"src": "."
},
{
"caches": [
"node-modules"
],
"cmd": "pnpm run build"
}
],
Expand Down Expand Up @@ -2121,6 +2135,10 @@
"directory": "/mise/cache",
"type": "shared"
},
"node-modules": {
"directory": "/app/node_modules/.cache",
"type": "shared"
},
"npm-install": {
"directory": "/root/.npm",
"type": "shared"
Expand Down Expand Up @@ -2259,6 +2277,9 @@
"src": "."
},
{
"caches": [
"node-modules"
],
"cmd": "npm run build"
}
],
Expand Down Expand Up @@ -2599,6 +2620,10 @@
"directory": "/mise/cache",
"type": "shared"
},
"node-modules": {
"directory": "/app/node_modules/.cache",
"type": "shared"
},
"npm-install": {
"directory": "/root/.npm",
"type": "shared"
Expand Down Expand Up @@ -2782,6 +2807,9 @@
"src": "."
},
{
"caches": [
"node-modules"
],
"cmd": "npm run build"
}
],
Expand Down Expand Up @@ -2855,7 +2883,7 @@
"steps": [
{
"name": "packages:image",
"startingImage": "php:8.4.4-fpm",
"startingImage": "php:8.4.3-fpm",
"useSecrets": false
},
{
Expand Down Expand Up @@ -3042,6 +3070,14 @@
"directory": "/mise/cache",
"type": "shared"
},
"next-": {
"directory": "/app/.next/cache",
"type": "shared"
},
"node-modules": {
"directory": "/app/node_modules/.cache",
"type": "shared"
},
"npm-install": {
"directory": "/root/.npm",
"type": "shared"
Expand Down Expand Up @@ -3179,6 +3215,10 @@
"src": "."
},
{
"caches": [
"node-modules",
"next-"
],
"cmd": "npm run build"
}
],
Expand Down Expand Up @@ -3207,6 +3247,18 @@
"directory": "/mise/cache",
"type": "shared"
},
"next-apps-docs": {
"directory": "/app/apps/docs/.next/cache",
"type": "shared"
},
"next-apps-web": {
"directory": "/app/apps/web/.next/cache",
"type": "shared"
},
"node-modules": {
"directory": "/app/node_modules/.cache",
"type": "shared"
},
"npm-install": {
"directory": "/root/.npm",
"type": "shared"
Expand Down Expand Up @@ -3387,6 +3439,11 @@
"src": "."
},
{
"caches": [
"node-modules",
"next-apps-docs",
"next-apps-web"
],
"cmd": "npm run build"
}
],
Expand Down
19 changes: 16 additions & 3 deletions core/generate/cache_context.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Expand All @@ -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, "/", "-")
}
19 changes: 18 additions & 1 deletion core/providers/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package node

import (
"fmt"
"path"
"strings"

"github.com/railwayapp/railpack/core/app"
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion core/providers/php/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
DEFAULT_PHP_VERSION = "8.4"
DEFAULT_PHP_VERSION = "8.4.3"
)

type PhpProvider struct{}
Expand Down

0 comments on commit 85ccb32

Please sign in to comment.