Skip to content

Commit

Permalink
Add custom_build(outputs_image_ref_to=...) Tiltfile API (#3769)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed Sep 14, 2020
1 parent e5ca125 commit 292eba8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 23 deletions.
40 changes: 25 additions & 15 deletions internal/tiltfile/docker.go
Expand Up @@ -55,9 +55,12 @@ type dockerImage struct {
// Whether this has been matched up yet to a deploy resource.
matched bool

dependencyIDs []model.TargetID
disablePush bool
skipsLocalDocker bool
dependencyIDs []model.TargetID

// Only applicable to custom_build
disablePush bool
skipsLocalDocker bool
outputsImageRefTo string

liveUpdate model.LiveUpdate
}
Expand Down Expand Up @@ -271,6 +274,7 @@ func (s *tiltfileState) customBuild(thread *starlark.Thread, fn *starlark.Builti
var entrypoint starlark.Value
var containerArgsVal starlark.Sequence
var skipsLocalDocker bool
outputsImageRefTo := value.NewLocalPathUnpacker(thread)

err := s.unpackArgs(fn.Name(), args, kwargs,
"ref", &dockerRef,
Expand All @@ -285,6 +289,7 @@ func (s *tiltfileState) customBuild(thread *starlark.Thread, fn *starlark.Builti
"entrypoint?", &entrypoint,
"container_args?", &containerArgsVal,
"command_bat_val", &commandBatVal,
"outputs_image_ref_to", &outputsImageRefTo,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -342,19 +347,24 @@ func (s *tiltfileState) customBuild(thread *starlark.Thread, fn *starlark.Builti
return nil, fmt.Errorf("Argument 2 (command) can't be empty")
}

if tag != "" && outputsImageRefTo.Value != "" {
return nil, fmt.Errorf("Cannot specify both tag= and outputs_image_ref_to=")
}

img := &dockerImage{
workDir: starkit.AbsWorkingDir(thread),
configurationRef: container.NewRefSelector(ref),
customCommand: command,
customDeps: localDeps,
customTag: tag,
disablePush: disablePush,
skipsLocalDocker: skipsLocalDocker,
liveUpdate: liveUpdate,
matchInEnvVars: matchInEnvVars,
ignores: ignores,
entrypoint: entrypointCmd,
containerArgs: containerArgs,
workDir: starkit.AbsWorkingDir(thread),
configurationRef: container.NewRefSelector(ref),
customCommand: command,
customDeps: localDeps,
customTag: tag,
disablePush: disablePush,
skipsLocalDocker: skipsLocalDocker,
liveUpdate: liveUpdate,
matchInEnvVars: matchInEnvVars,
ignores: ignores,
entrypoint: entrypointCmd,
containerArgs: containerArgs,
outputsImageRefTo: outputsImageRefTo.Value,
}

err = s.buildIndex.addImage(img)
Expand Down
37 changes: 37 additions & 0 deletions internal/tiltfile/docker_test.go
Expand Up @@ -102,3 +102,40 @@ custom_build('gcr.io/fe', 'docker build -t $EXPECTED_REF .', ['src'])

assert.Contains(t, localPathStrings, f.JoinPath("src"))
}

func TestCustomBuildOutputsImageRefsTo(t *testing.T) {
f := newFixture(t)
defer f.TearDown()

f.yaml("fe.yaml", deployment("fe", image("gcr.io/fe")))
f.file("Dockerfile", `
FROM alpine
ADD . .
`)
f.file("Tiltfile", `
k8s_yaml('fe.yaml')
custom_build('gcr.io/fe', 'export MY_REF="gcr.io/fe:dev" && docker build -t $MY_REF . && echo $MY_REF > ref.txt',
['src'],
outputs_image_ref_to='ref.txt')
`)

f.load()

m := f.assertNextManifest("fe")
it := m.ImageTargets[0]
assert.Equal(t, f.JoinPath("ref.txt"), it.CustomBuildInfo().OutputsImageRefTo)
}

func TestCustomBuildOutputsImageRefsToIncompatibleWithTag(t *testing.T) {
f := newFixture(t)
defer f.TearDown()

f.file("Tiltfile", `
custom_build('gcr.io/fe', 'export MY_REF="gcr.io/fe:dev" && docker build -t $MY_REF . && echo $MY_REF > ref.txt',
['src'],
tag='dev',
outputs_image_ref_to='ref.txt')
`)

f.loadErrString("Cannot specify both tag= and outputs_image_ref_to=")
}
15 changes: 8 additions & 7 deletions internal/tiltfile/tiltfile_state.go
Expand Up @@ -1398,13 +1398,14 @@ func (s *tiltfileState) imgTargetsForDependencyIDsHelper(ids []model.TargetID, c
})
case CustomBuild:
r := model.CustomBuild{
WorkDir: image.workDir,
Command: image.customCommand,
Deps: image.customDeps,
Tag: image.customTag,
DisablePush: image.disablePush,
SkipsLocalDocker: image.skipsLocalDocker,
LiveUpdate: lu,
WorkDir: image.workDir,
Command: image.customCommand,
Deps: image.customDeps,
Tag: image.customTag,
DisablePush: image.disablePush,
SkipsLocalDocker: image.skipsLocalDocker,
OutputsImageRefTo: image.outputsImageRefTo,
LiveUpdate: lu,
}
iTarget = iTarget.WithBuildDetails(r).
MaybeIgnoreRegistry()
Expand Down
26 changes: 26 additions & 0 deletions internal/tiltfile/value/path.go
@@ -0,0 +1,26 @@
package value

import (
"go.starlark.net/starlark"
)

type LocalPath struct {
t *starlark.Thread
Value string
}

func NewLocalPathUnpacker(t *starlark.Thread) LocalPath {
return LocalPath{
t: t,
}
}

func (p *LocalPath) Unpack(v starlark.Value) error {
str, err := ValueToAbsPath(p.t, v)
if err != nil {
return err
}

p.Value = str
return nil
}
2 changes: 1 addition & 1 deletion internal/tiltfile/value/value.go
Expand Up @@ -37,7 +37,7 @@ func ValueToAbsPath(thread *starlark.Thread, v starlark.Value) (string, error) {
return pathMaker.MakeLocalPath("."), nil
}

str, ok := v.(starlark.String)
str, ok := starlark.AsString(v)
if ok {
return starkit.AbsPath(thread, string(str)), nil
}
Expand Down

0 comments on commit 292eba8

Please sign in to comment.