Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions buildkit/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type BuildWithBuildkitClientOptions struct {
ExportCache string
CacheKey string
GitHubToken string
NoCache bool
}

func BuildWithBuildkitClient(appDir string, plan *plan.BuildPlan, opts BuildWithBuildkitClientOptions) error {
Expand Down Expand Up @@ -97,6 +98,7 @@ func BuildWithBuildkitClient(appDir string, plan *plan.BuildPlan, opts BuildWith
SecretsHash: opts.SecretsHash,
CacheKey: opts.CacheKey,
GitHubToken: opts.GitHubToken,
NoCache: opts.NoCache,
})
if err != nil {
return fmt.Errorf("error converting plan to LLB: %w", err)
Expand Down
8 changes: 7 additions & 1 deletion buildkit/build_llb/build_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type BuildGraph struct {
Plan *plan.BuildPlan
Platform *specs.Platform
LocalState *llb.State
NoCache bool

githubToken string
secretsFile *llb.State
Expand All @@ -37,7 +38,7 @@ type BuildGraphOutput struct {
GraphEnv BuildEnvironment
}

func NewBuildGraph(plan *plan.BuildPlan, localState *llb.State, cacheStore *BuildKitCacheStore, secretsHash string, platform *specs.Platform, githubToken string) (*BuildGraph, error) {
func NewBuildGraph(plan *plan.BuildPlan, localState *llb.State, cacheStore *BuildKitCacheStore, secretsHash string, platform *specs.Platform, githubToken string, noCache bool) (*BuildGraph, error) {
var secretsFile *llb.State
if secretsHash != "" {
st := llb.Scratch().File(llb.Mkfile("/secrets-hash", 0644, []byte(secretsHash)), llb.WithCustomName("[railpack] secrets hash"))
Expand All @@ -51,6 +52,7 @@ func NewBuildGraph(plan *plan.BuildPlan, localState *llb.State, cacheStore *Buil
Plan: plan,
Platform: platform,
LocalState: localState,
NoCache: noCache,

githubToken: githubToken,
secretsFile: secretsFile,
Expand Down Expand Up @@ -254,6 +256,10 @@ func (g *BuildGraph) convertExecCommandToLLB(node *StepNode, cmd plan.ExecComman
opts = append(opts, llb.WithCustomName(cmd.CustomName))
}

if g.NoCache {
opts = append(opts, llb.IgnoreCache)
}

// These options mount all secrets as environments variables
// We want to add all secrets to all commands, even if they are not specified in the step
// Note: This does mean that if the number of secrets change, then the cache for every step will be invalidated
Expand Down
49 changes: 49 additions & 0 deletions buildkit/build_llb/build_graph_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package build_llb

import (
"testing"

"github.com/moby/buildkit/client/llb"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/railwayapp/railpack/core/plan"
"github.com/stretchr/testify/require"
)

func TestBuildGraphNoCache(t *testing.T) {
p := &plan.BuildPlan{
Steps: []plan.Step{
{
Name: "test",
Caches: []string{"test-cache"},
},
},
Caches: map[string]*plan.Cache{
"test-cache": {
Directory: "/root/.cache",
},
},
}

localState := llb.Local("context")
cacheStore := NewBuildKitCacheStore("")
platform := specs.Platform{OS: "linux", Architecture: "amd64"}

t.Run("with NoCache=false", func(t *testing.T) {
g, err := NewBuildGraph(p, &localState, cacheStore, "", &platform, "", false)
require.NoError(t, err)
require.False(t, g.NoCache)
require.False(t, isCacheDisabled("test-cache"))
})

t.Run("with NoCache=true", func(t *testing.T) {
g, err := NewBuildGraph(p, &localState, cacheStore, "", &platform, "", true)
require.NoError(t, err)
require.True(t, g.NoCache)
// NoCache should NOT affect if caches are enabled or not, it just affects the layer cache
require.False(t, isCacheDisabled("test-cache"))

opts, err := g.getCacheMountOptions([]string{"test-cache"})
require.NoError(t, err)
require.Len(t, opts, 1)
})
}
5 changes: 4 additions & 1 deletion buildkit/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type ConvertPlanOptions struct {

// Token used to make authenticated API requests to GitHub to increase rate limits
GitHubToken string

// Do not use cache when building
NoCache bool
}

const (
Expand All @@ -45,7 +48,7 @@ func ConvertPlanToLLB(plan *p.BuildPlan, opts ConvertPlanOptions) (*llb.State, *
)

cacheStore := build_llb.NewBuildKitCacheStore(opts.CacheKey)
graph, err := build_llb.NewBuildGraph(plan, &localState, cacheStore, opts.SecretsHash, &platform, opts.GitHubToken)
graph, err := build_llb.NewBuildGraph(plan, &localState, cacheStore, opts.SecretsHash, &platform, opts.GitHubToken, opts.NoCache)
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions buildkit/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
secretsHash = "secrets-hash"
cacheKey = "cache-key"
githubToken = "github-token"
noCache = "no-cache"
)

func StartFrontend() {
Expand All @@ -50,6 +51,7 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
cacheKey := buildArgs[cacheKey]
secretsHash := buildArgs[secretsHash]
githubToken := buildArgs[githubToken]
noCacheVal := buildArgs[noCache] == "true"

// TODO: Support building for multiple platforms
buildPlatform, err := validatePlatform(opts)
Expand All @@ -73,6 +75,7 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
CacheKey: cacheKey,
SessionID: c.BuildOpts().SessionID,
GitHubToken: githubToken,
NoCache: noCacheVal,
})
if err != nil {
return nil, fmt.Errorf("error converting plan to LLB: %w", err)
Expand Down
6 changes: 6 additions & 0 deletions cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ var BuildCommand = &cli.Command{
Name: "cache-key",
Usage: "Unique id to prefix to cache keys",
},
&cli.BoolFlag{
Name: "no-cache",
Usage: "Do not use cache when building",
Value: false,
},
&cli.BoolFlag{
Name: "dump-llb",
Hidden: true,
Expand Down Expand Up @@ -102,6 +107,7 @@ var BuildCommand = &cli.Command{
Secrets: env.Variables,
Platform: platformStr,
GitHubToken: os.Getenv("GITHUB_TOKEN"),
NoCache: cmd.Bool("no-cache"),
})
if err != nil {
return cli.Exit(err, 1)
Expand Down
1 change: 1 addition & 0 deletions docs/src/content/docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ railpack build [options] DIRECTORY
| `--progress` | BuildKit progress output mode (auto, plain, tty) | `auto` |
| `--show-plan` | Show the build plan before building | `false` |
| `--cache-key` | Unique id to prefix to cache keys | |
| `--no-cache` | Do not use cache when building (boolean flag) | `false` |

### prepare

Expand Down
1 change: 1 addition & 0 deletions docs/src/content/docs/reference/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ You can pass advanced options to the frontend using the `--opt` flag (for BuildK
| `--cache-key` | Unique ID to prefix to cache keys for cache invalidation. | |
| `--secrets-hash` | Hash of all secret values, used to invalidate cache when secrets change. | |
| `--github-token` | GitHub token to increase API rate limits for private repositories or package installs. | |
| `--no-cache` | Set to `true` as a build-arg to disable layer cache during the build. | `false` |

### Example

Expand Down
Loading