Skip to content

Commit

Permalink
tiltfile: add custom_build(dir)
Browse files Browse the repository at this point in the history
sets the current working directory for the custom_build command

fixes #6145

Signed-off-by: Nick Santos <[email protected]>
  • Loading branch information
nicks committed Aug 30, 2023
1 parent 33fde0f commit d8e4fd8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
4 changes: 3 additions & 1 deletion internal/tiltfile/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,8 @@ def custom_build(
outputs_image_ref_to: str = "",
command_bat: Union[str, List[str]] = "",
image_deps: List[str] = [],
env: Dict[str, str] = {}):
env: Dict[str, str] = {},
dir: string = ""):
"""Provide a custom command that will build an image.
Example ::
Expand Down Expand Up @@ -986,6 +987,7 @@ def custom_build(
`TILT_IMAGE_MAP_i` - The name of the image map #i (0-based) with the current status of the image.
env: Environment variables to pass to the executed ``command``. Values specified here will override any variables passed to the Tilt parent process.
dir: Working directory of the executed ``command``. Defaults to the Tiltfile directory.
"""
pass

Expand Down
44 changes: 44 additions & 0 deletions internal/tiltfile/custom_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
)

func TestCustomBuildImageDeps(t *testing.T) {
Expand Down Expand Up @@ -70,5 +72,47 @@ k8s_yaml('fe.yaml')
cb := m.ImageTargets[0].CustomBuildInfo()
expected := []string{"SETTING=value"}
assert.Equal(t, expected, cb.CmdImageSpec.Env)
assert.Equal(t, f.Path(), cb.CmdImageSpec.Dir)
}
}

func TestCustomBuildImageWithDir(t *testing.T) {
f := newFixture(t)

f.file("Tiltfile", `
custom_build('custom', 'build.sh', ['.'], dir='./subdir')
k8s_yaml('fe.yaml')
`)
f.file("Dockerfile", `FROM alpine`)
f.yaml("fe.yaml", deployment("fe", image("custom")))

f.load()

m := f.assertNextManifest("fe")
if assert.Equal(t, 1, len(m.ImageTargets)) {
cb := m.ImageTargets[0].CustomBuildInfo()
assert.Equal(t, f.JoinPath("subdir"), cb.CmdImageSpec.Dir)
}
}

func TestCustomBuildIgnoresContextDirNotWorkDir(t *testing.T) {
f := newFixture(t)

f.file("Tiltfile", `
custom_build('custom', 'build.sh', ['./context-dir'], dir='./work-dir', ignore=['./context-dir/ignore.txt'])
k8s_yaml('fe.yaml')
`)
f.file("Dockerfile", `FROM alpine`)
f.yaml("fe.yaml", deployment("fe", image("custom")))

f.load()

m := f.assertNextManifest("fe")
if assert.Equal(t, 1, len(m.ImageTargets)) {
cb := m.ImageTargets[0].CustomBuildInfo()
assert.Equal(t, f.JoinPath("work-dir"), cb.CmdImageSpec.Dir)
assert.Equal(t, []v1alpha1.IgnoreDef{}, m.ImageTargets[0].FileWatchIgnores)
}
}
8 changes: 3 additions & 5 deletions internal/tiltfile/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var cacheObsoleteWarning = "docker_build(cache=...) is obsolete, and currently a

type dockerImage struct {
buildType dockerImageBuildType
workDir string
configurationRef container.RefSelector
matchInEnvVars bool
sshSpecs []string
Expand Down Expand Up @@ -238,7 +237,6 @@ func (s *tiltfileState) dockerBuild(thread *starlark.Thread, fn *starlark.Builti

r := &dockerImage{
buildType: DockerBuild,
workDir: starkit.CurrentExecPath(thread),
dbDockerfilePath: dockerfilePath,
dbDockerfile: dockerfile.Dockerfile(dockerfileContents),
dbBuildPath: context,
Expand Down Expand Up @@ -299,6 +297,7 @@ func (s *tiltfileState) customBuild(thread *starlark.Thread, fn *starlark.Builti
var skipsLocalDocker bool
var imageDeps value.ImageList
var env value.StringStringMap
var dir starlark.Value
outputsImageRefTo := value.NewLocalPathUnpacker(thread)

err := s.unpackArgs(fn.Name(), args, kwargs,
Expand All @@ -322,6 +321,7 @@ func (s *tiltfileState) customBuild(thread *starlark.Thread, fn *starlark.Builti

"image_deps", &imageDeps,
"env?", &env,
"dir?", &dir,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -360,7 +360,7 @@ func (s *tiltfileState) customBuild(thread *starlark.Thread, fn *starlark.Builti
commandBat = commandBatVal
}

command, err := value.ValueGroupToCmdHelper(thread, commandVal, commandBat, nil, env)
command, err := value.ValueGroupToCmdHelper(thread, commandVal, commandBat, dir, env)
if err != nil {
return nil, fmt.Errorf("Argument 2 (command): %v", err)
} else if command.Empty() {
Expand All @@ -373,7 +373,6 @@ func (s *tiltfileState) customBuild(thread *starlark.Thread, fn *starlark.Builti

img := &dockerImage{
buildType: CustomBuild,
workDir: starkit.AbsWorkingDir(thread),
configurationRef: container.NewRefSelector(ref),
customCommand: command,
customDeps: deps.Value,
Expand Down Expand Up @@ -476,7 +475,6 @@ func (s *tiltfileState) repoIgnoresForImage(image *dockerImage) []v1alpha1.Ignor
if image.dbBuildPath != "" {
paths = append(paths, image.dbBuildPath)
}
paths = append(paths, image.workDir)
paths = append(paths, image.customDeps...)

return repoIgnoresForPaths(paths)
Expand Down
2 changes: 1 addition & 1 deletion internal/tiltfile/tiltfile_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ func (s *tiltfileState) imgTargetsForDepsHelper(mn model.ManifestName, imageMapD

spec := v1alpha1.CmdImageSpec{
Args: image.customCommand.Argv,
Dir: image.workDir,
Dir: image.customCommand.Dir,
Env: image.customCommand.Env,
OutputTag: image.customTag,
OutputsImageRefTo: image.outputsImageRefTo,
Expand Down

0 comments on commit d8e4fd8

Please sign in to comment.