Skip to content

Commit b1bd6d9

Browse files
authored
consistent provider step naming (#228)
* docs: light improvements to code comments * fix: use build, not step, in shell provider for consistency * fix: stop using NewLocalLayer in static provider this is the only provider using it * fix: more consistent approach to including the local layer * test: snapshot update
1 parent 4e38ce0 commit b1bd6d9

File tree

8 files changed

+61
-36
lines changed

8 files changed

+61
-36
lines changed

buildkit/build_llb/layers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func hasPathOverlap(paths1, paths2 []string) bool {
179179
// For container paths, the full relative path structure is preserved under /app.
180180
func resolvePaths(include string, isLocal bool) (srcPath, destPath string) {
181181
if isLocal {
182+
// convert a local path reference to fully qualified container path
182183
return include, filepath.Join("/app", filepath.Base(include))
183184
}
184185

core/__snapshots__/TestGenerateBuildPlanForExamples_shell-script_1.snap.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
{
22
"deploy": {
33
"base": {
4-
"step": "setup"
4+
"image": "ghcr.io/railwayapp/railpack-runtime:latest"
55
},
6+
"inputs": [
7+
{
8+
"include": [
9+
"."
10+
],
11+
"step": "build"
12+
}
13+
],
614
"startCommand": "sh start.sh"
715
},
816
"steps": [
@@ -21,7 +29,7 @@
2129
"image": "ghcr.io/railwayapp/railpack-runtime:latest"
2230
}
2331
],
24-
"name": "setup",
32+
"name": "build",
2533
"secrets": [
2634
"*"
2735
]

core/__snapshots__/TestGenerateBuildPlanForExamples_staticfile-config_1.snap.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@
1818
"include": [
1919
"."
2020
],
21-
"step": "setup"
22-
},
23-
{
24-
"include": [
25-
"."
26-
],
27-
"local": true
21+
"step": "build"
2822
}
2923
],
3024
"startCommand": "caddy run --config Caddyfile --adapter caddyfile 2\u003e\u00261"
@@ -68,6 +62,10 @@
6862
"Caddyfile": "{\n\tadmin off\n\tpersist_config off\n\tauto_https off\n\n\tlog {\n\t\tformat json\n\t}\n\n\tservers {\n\t\ttrusted_proxies static private_ranges\n\t}\n}\n\n:{$PORT:80} {\n\tlog {\n\t\tformat json\n\t}\n\n\trespond /health 200\n\n\t# Security headers\n\theader {\n\t\t# Enable cross-site filter (XSS) and tell browsers to block detected attacks\n\t\tX-XSS-Protection \"1; mode=block\"\n\t\t# Prevent some browsers from MIME-sniffing a response away from the declared Content-Type\n\t\tX-Content-Type-Options \"nosniff\"\n\t\t# Keep referrer data off of HTTP connections\n\t\tReferrer-Policy \"strict-origin-when-cross-origin\"\n\t\t# Enable strict Content Security Policy\n\t\tContent-Security-Policy \"default-src 'self'; img-src 'self' data: https: *; style-src 'self' 'unsafe-inline' https: *; script-src 'self' 'unsafe-inline' https: *; font-src 'self' data: https: *; connect-src 'self' https: *; media-src 'self' https: *; object-src 'none'; frame-src 'self' https: *;\"\n\t\t# Remove Server header\n\t\t-Server\n\t}\n\n\troot * hello\n\n\t# Handle static files\n\tfile_server {\n\t\thide .git\n\t\thide .env*\n\t}\n\n\t# Compression with more formats\n\tencode {\n\t\tgzip\n\t\tzstd\n\t}\n\n\t# Try files with HTML extension and handle SPA routing\n\ttry_files {path} {path}.html {path}/index.html /index.html\n\n\t# Handle 404 errors\n\thandle_errors {\n\t\trewrite * /{err.status_code}.html\n\t\tfile_server\n\t}\n}\n"
6963
},
7064
"commands": [
65+
{
66+
"dest": ".",
67+
"src": "."
68+
},
7169
{
7270
"name": "Caddyfile",
7371
"path": "Caddyfile"
@@ -81,7 +79,7 @@
8179
"step": "packages:mise"
8280
}
8381
],
84-
"name": "setup",
82+
"name": "build",
8583
"secrets": [
8684
"*"
8785
]

core/__snapshots__/TestGenerateBuildPlanForExamples_staticfile-index_1.snap.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@
1818
"include": [
1919
"."
2020
],
21-
"step": "setup"
22-
},
23-
{
24-
"include": [
25-
"."
26-
],
27-
"local": true
21+
"step": "build"
2822
}
2923
],
3024
"startCommand": "caddy run --config Caddyfile --adapter caddyfile 2\u003e\u00261"
@@ -68,6 +62,10 @@
6862
"Caddyfile": "{\n\tadmin off\n\tpersist_config off\n\tauto_https off\n\n\tlog {\n\t\tformat json\n\t}\n\n\tservers {\n\t\ttrusted_proxies static private_ranges\n\t}\n}\n\n:{$PORT:80} {\n\tlog {\n\t\tformat json\n\t}\n\n\trespond /health 200\n\n\t# Security headers\n\theader {\n\t\t# Enable cross-site filter (XSS) and tell browsers to block detected attacks\n\t\tX-XSS-Protection \"1; mode=block\"\n\t\t# Prevent some browsers from MIME-sniffing a response away from the declared Content-Type\n\t\tX-Content-Type-Options \"nosniff\"\n\t\t# Keep referrer data off of HTTP connections\n\t\tReferrer-Policy \"strict-origin-when-cross-origin\"\n\t\t# Enable strict Content Security Policy\n\t\tContent-Security-Policy \"default-src 'self'; img-src 'self' data: https: *; style-src 'self' 'unsafe-inline' https: *; script-src 'self' 'unsafe-inline' https: *; font-src 'self' data: https: *; connect-src 'self' https: *; media-src 'self' https: *; object-src 'none'; frame-src 'self' https: *;\"\n\t\t# Remove Server header\n\t\t-Server\n\t}\n\n\troot * .\n\n\t# Handle static files\n\tfile_server {\n\t\thide .git\n\t\thide .env*\n\t}\n\n\t# Compression with more formats\n\tencode {\n\t\tgzip\n\t\tzstd\n\t}\n\n\t# Try files with HTML extension and handle SPA routing\n\ttry_files {path} {path}.html {path}/index.html /index.html\n\n\t# Handle 404 errors\n\thandle_errors {\n\t\trewrite * /{err.status_code}.html\n\t\tfile_server\n\t}\n}\n"
6963
},
7064
"commands": [
65+
{
66+
"dest": ".",
67+
"src": "."
68+
},
7169
{
7270
"name": "Caddyfile",
7371
"path": "Caddyfile"
@@ -81,7 +79,7 @@
8179
"step": "packages:mise"
8280
}
8381
],
84-
"name": "setup",
82+
"name": "build",
8583
"secrets": [
8684
"*"
8785
]

core/core.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ func GenerateConfigFromFile(app *app.App, env *app.Environment, options *Generat
159159
return config, nil
160160
}
161161

162-
// GenerateConfigFromEnvironment generates a config from the environment
163162
func GenerateConfigFromEnvironment(env *app.Environment) *c.Config {
164163
config := c.EmptyConfig()
165164

core/plan/layer.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,19 @@ func (i *Layer) DisplayName() string {
8585
return fmt.Sprintf("input %s", include)
8686
}
8787

88+
// Supports two types of inputs:
89+
//
90+
// Object Notation:
91+
// - Step Layer: {"step": "build", "include": ["src/**/*.go"], "exclude": ["*_test.go"]}
92+
// References the output of a named build step with optional file filtering
93+
// - Image Layer: {"image": "golang:1.21", "include": ["."], "exclude": ["tmp"]}
94+
// Uses a Docker image as input with optional file filtering
95+
// - Local Layer: {"local": true, "include": ["src"], "exclude": ["node_modules"]}
96+
// Uses local files from the build context with filtering
97+
//
98+
// String Shortcuts: ".", "...", "$stepname"
8899
func (i *Layer) UnmarshalJSON(data []byte) error {
89-
// First try normal JSON unmarshal
100+
// First try normal JSON unmarshal for object notation
90101
type Alias Layer
91102
aux := &struct {
92103
*Alias
@@ -97,17 +108,22 @@ func (i *Layer) UnmarshalJSON(data []byte) error {
97108
return nil
98109
}
99110

111+
// If object unmarshaling fails, try string shortcuts
100112
str := string(data)
101113

114+
// Remove quotes from JSON string
102115
str = strings.Trim(str, "\"")
103116
switch str {
104117
case ".":
118+
// "." represents a local layer with current directory
105119
*i = NewLocalLayer(".")
106120
return nil
107121
case "...":
122+
// Creates a spread layer that expands to include all previous layers' files
108123
*i = Layer{Spread: true}
109124
return nil
110125
default:
126+
// "$stepname" represents a reference to another step
111127
if strings.HasPrefix(str, "$") {
112128
stepName := strings.TrimPrefix(str, "$")
113129
*i = NewStepLayer(stepName)

core/providers/shell/shell.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,20 @@ func (p *ShellProvider) Plan(ctx *generate.GenerateContext) error {
3232

3333
ctx.Logger.LogInfo("Using shell script: %s", p.scriptName)
3434

35-
setup := ctx.NewCommandStep("setup")
36-
setup.AddInput(plan.NewImageLayer(plan.RailpackRuntimeImage))
37-
setup.AddCommands(
35+
build := ctx.NewCommandStep("build")
36+
build.AddInput(plan.NewImageLayer(plan.RailpackRuntimeImage))
37+
build.AddCommands(
3838
[]plan.Command{
3939
plan.NewCopyCommand("."),
4040
plan.NewExecCommand("chmod +x " + p.scriptName),
4141
},
4242
)
4343

44-
ctx.Deploy.Base = plan.NewStepLayer(setup.Name())
45-
ctx.Deploy.AddInputs([]plan.Layer{ctx.GetMiseStepBuilder().GetLayer()})
44+
ctx.Deploy.AddInputs([]plan.Layer{
45+
plan.NewStepLayer(build.Name(), plan.Filter{
46+
Include: []string{"."},
47+
}),
48+
})
4649

4750
return nil
4851
}

core/providers/staticfile/staticfile.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// this provider is distinct from the SPA functionality used by node providers
2+
// it is meant to simply serve static files over HTTP
3+
14
package staticfile
25

36
import (
@@ -52,22 +55,25 @@ func (p *StaticfileProvider) Plan(ctx *generate.GenerateContext) error {
5255
miseStep := ctx.GetMiseStepBuilder()
5356
miseStep.Default("caddy", "latest")
5457

55-
setup := ctx.NewCommandStep("setup")
56-
setup.AddInput(plan.NewStepLayer(miseStep.Name()))
57-
err := p.Setup(ctx, setup)
58+
build := ctx.NewCommandStep("build")
59+
build.AddInput(plan.NewStepLayer(miseStep.Name()))
60+
build.AddCommands([]plan.Command{
61+
plan.NewCopyCommand("."),
62+
})
63+
64+
err := p.addCaddyfileToStep(ctx, build)
5865
if err != nil {
5966
return err
6067
}
6168

6269
ctx.Deploy.AddInputs([]plan.Layer{
6370
miseStep.GetLayer(),
64-
plan.NewStepLayer(setup.Name(), plan.Filter{
71+
plan.NewStepLayer(build.Name(), plan.Filter{
6572
Include: []string{"."},
6673
}),
67-
plan.NewLocalLayer("."),
6874
})
6975

70-
ctx.Deploy.StartCmd = p.CaddyStartCommand(ctx)
76+
ctx.Deploy.StartCmd = fmt.Sprintf("caddy run --config %s --adapter caddyfile 2>&1", CaddyfilePath)
7177

7278
return nil
7379
}
@@ -76,11 +82,7 @@ func (p *StaticfileProvider) StartCommandHelp() string {
7682
return ""
7783
}
7884

79-
func (p *StaticfileProvider) CaddyStartCommand(ctx *generate.GenerateContext) string {
80-
return "caddy run --config " + CaddyfilePath + " --adapter caddyfile 2>&1"
81-
}
82-
83-
func (p *StaticfileProvider) Setup(ctx *generate.GenerateContext, setup *generate.CommandStepBuilder) error {
85+
func (p *StaticfileProvider) addCaddyfileToStep(ctx *generate.GenerateContext, setup *generate.CommandStepBuilder) error {
8486
ctx.Logger.LogInfo("Using root dir: %s", p.RootDir)
8587

8688
data := map[string]interface{}{

0 commit comments

Comments
 (0)