Skip to content

Commit

Permalink
Merge pull request #15 from railwayapp/jr/custom-frontend-uses-build-…
Browse files Browse the repository at this point in the history
…args

custom frontend uses `--build-arg:name=value`
  • Loading branch information
coffee-cup authored Feb 11, 2025
2 parents 90ad4e5 + 0e49a7e commit 6b4513b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
22 changes: 20 additions & 2 deletions buildkit/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func StartFrontend() {

func Build(ctx context.Context, c client.Client) (*client.Result, error) {
opts := c.BuildOpts().Opts
cacheKey := opts[cacheKey]
secretsHash := opts[secretsHash]
buildArgs := parseBuildArgs(opts)

cacheKey := buildArgs[cacheKey]
secretsHash := buildArgs[secretsHash]

buildPlatform, err := validatePlatform(opts)
if err != nil {
Expand Down Expand Up @@ -176,3 +178,19 @@ func readFile(ctx context.Context, c client.Client, filename string) (string, er

return fileContents, nil
}

func parseBuildArgs(opts map[string]string) map[string]string {
buildArgs := make(map[string]string)

for key, arg := range opts {
if !strings.HasPrefix(key, "build-arg:") {
continue
}

// Remove the "build-arg:" prefix
name := strings.TrimPrefix(key, "build-arg:")
buildArgs[name] = arg
}

return buildArgs
}
31 changes: 31 additions & 0 deletions buildkit/frontend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package buildkit

import (
"testing"
)

func TestParseBuildArgs(t *testing.T) {
opts := map[string]string{
"build-arg:FOO": "bar",
"platform": "linux/amd64",
"build-arg:BAZ": "qux",
"filename": "Dockerfile",
}

got := parseBuildArgs(opts)

want := map[string]string{
"FOO": "bar",
"BAZ": "qux",
}

if len(got) != len(want) {
t.Errorf("got %d build args, want %d", len(got), len(want))
}

for k, v := range want {
if got[k] != v {
t.Errorf("build arg %q = %q, want %q", k, got[k], v)
}
}
}

0 comments on commit 6b4513b

Please sign in to comment.