Skip to content

Commit 5964eb6

Browse files
authored
custom_build: add EXPECTED_{IMAGE,TAG,REGISTRY} env vars (#5743)
Currently, we pass `EXPECTED_REF`, which is the full image reference including registry, path, and tag. Some build scripts want to consume these independently, so in addition to passing the full reference, this adds the individual components as well. There's a slight behavior change here in that previously, the registry was passed as `REGISTRY_HOST` only in the case that `outputs_image_ref_to` mode was being used. Now, it's _always_ passed (if there is a local registry!) regardless of custom build mode along with `EXPECTED_REGISTRY`, which is new and duplicative but consistent with the other names. This should be harmless, however. Closes #5433.
1 parent db7dce0 commit 5964eb6

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

internal/build/custom_builder.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/exec"
99
"strings"
1010

11+
"github.com/docker/distribution/reference"
1112
"github.com/opencontainers/go-digest"
1213
"github.com/pkg/errors"
1314
ktypes "k8s.io/apimachinery/pkg/types"
@@ -37,9 +38,9 @@ func (b *CustomBuilder) Build(ctx context.Context, refs container.RefSet,
3738
imageMaps map[ktypes.NamespacedName]*v1alpha1.ImageMap) (container.TaggedRefs, error) {
3839
expectedTag := spec.OutputTag
3940
outputsImageRefTo := spec.OutputsImageRefTo
41+
registryHost := refs.Registry().Host
4042

4143
var expectedBuildRefs container.TaggedRefs
42-
var registryHost string
4344
var err error
4445

4546
// There are 3 modes for determining the output tag.
@@ -49,10 +50,6 @@ func (b *CustomBuilder) Build(ctx context.Context, refs container.RefSet,
4950

5051
// Remove the output file, ignoring any errors.
5152
_ = os.Remove(outputsImageRefTo)
52-
53-
// Inform the user script about the registry host
54-
registryHost = refs.Registry().Host
55-
5653
} else if expectedTag != "" {
5754
// If the tag is coming from the user script, we expect that the user script
5855
// also doesn't know about the local registry. So we have to strip off
@@ -83,10 +80,18 @@ func (b *CustomBuilder) Build(ctx context.Context, refs container.RefSet,
8380
if expectedBuildResult != nil {
8481
extraEnvVars = append(extraEnvVars,
8582
fmt.Sprintf("EXPECTED_REF=%s", container.FamiliarString(expectedBuildResult)))
83+
extraEnvVars = append(extraEnvVars,
84+
fmt.Sprintf("EXPECTED_IMAGE=%s", reference.Path(expectedBuildResult)))
85+
extraEnvVars = append(extraEnvVars,
86+
fmt.Sprintf("EXPECTED_TAG=%s", expectedBuildResult.Tag()))
8687
}
8788
if registryHost != "" {
89+
// kept for backwards compatibility
8890
extraEnvVars = append(extraEnvVars,
8991
fmt.Sprintf("REGISTRY_HOST=%s", registryHost))
92+
// for consistency with other EXPECTED_* vars
93+
extraEnvVars = append(extraEnvVars,
94+
fmt.Sprintf("EXPECTED_REGISTRY=%s", registryHost))
9095
}
9196

9297
extraEnvVars = append(extraEnvVars, b.dCli.Env().AsEnviron()...)

internal/build/custom_builder_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,66 @@ func TestCustomBuildImageDep(t *testing.T) {
223223
assert.Equal(f.t, "base:tilt-12345", strings.TrimSpace(f.ReadFile("image-0.txt")))
224224
}
225225

226+
func TestEnvVars(t *testing.T) {
227+
if runtime.GOOS == "windows" {
228+
t.Skip("no sh on windows")
229+
}
230+
231+
expectedVars := map[string]string{
232+
"EXPECTED_REF": "localhost:1234/foo_bar:tilt-build-1551202573",
233+
"EXPECTED_REGISTRY": "localhost:1234",
234+
"EXPECTED_IMAGE": "foo_bar",
235+
"EXPECTED_TAG": "tilt-build-1551202573",
236+
"REGISTRY_HOST": "localhost:1234",
237+
}
238+
var script []string
239+
for k, v := range expectedVars {
240+
script = append(script, fmt.Sprintf(
241+
`if [ "${%s}" != "%s" ]; then >&2 printf "%s:\n\texpected: %s\n\tactual: ${%s}\n"; exit 1; fi`,
242+
k, v, k, v, k))
243+
}
244+
245+
f := newFakeCustomBuildFixture(t)
246+
sha := digest.Digest("sha256:11cd0eb38bc3ceb958ffb2f9bd70be3fb317ce7d255c8a4c3f4af30e298aa1aab")
247+
f.dCli.Images["localhost:1234/foo_bar:tilt-build-1551202573"] = types.ImageInspect{ID: string(sha)}
248+
cb := f.customBuild(strings.Join(script, "\n"))
249+
_, err := f.cb.Build(f.ctx, refSetWithRegistryFromString("foo/bar", TwoURLRegistry), cb.CmdImageSpec, nil)
250+
require.NoError(t, err)
251+
}
252+
253+
func TestEnvVars_ConfigRefWithLocalRegistry(t *testing.T) {
254+
if runtime.GOOS == "windows" {
255+
t.Skip("no sh on windows")
256+
}
257+
258+
// generally, config refs (value in Tiltfile) are $prod_registry/$image:$tag
259+
// and Tilt rewrites it to $local_registry/$sanitized_prod_registry_$image
260+
// however, some users explicitly use the $local_registry in their Tiltfile
261+
// refs, so instead of producing a redudant and confusing ref like
262+
// $local_registry/$sanitized_local_registry_$image, it just gets passed
263+
// through
264+
expectedVars := map[string]string{
265+
"EXPECTED_REF": "localhost:1234/foo/bar:tilt-build-1551202573",
266+
"EXPECTED_REGISTRY": "localhost:1234",
267+
"EXPECTED_IMAGE": "foo/bar",
268+
"EXPECTED_TAG": "tilt-build-1551202573",
269+
"REGISTRY_HOST": "localhost:1234",
270+
}
271+
var script []string
272+
for k, v := range expectedVars {
273+
script = append(script, fmt.Sprintf(
274+
`if [ "${%s}" != "%s" ]; then >&2 printf "%s:\n\texpected: %s\n\tactual: ${%s}\n"; exit 1; fi`,
275+
k, v, k, v, k))
276+
}
277+
278+
f := newFakeCustomBuildFixture(t)
279+
sha := digest.Digest("sha256:11cd0eb38bc3ceb958ffb2f9bd70be3fb317ce7d255c8a4c3f4af30e298aa1aab")
280+
f.dCli.Images["localhost:1234/foo/bar:tilt-build-1551202573"] = types.ImageInspect{ID: string(sha)}
281+
cb := f.customBuild(strings.Join(script, "\n"))
282+
_, err := f.cb.Build(f.ctx, refSetWithRegistryFromString("localhost:1234/foo/bar", TwoURLRegistry), cb.CmdImageSpec, nil)
283+
require.NoError(t, err)
284+
}
285+
226286
type fakeCustomBuildFixture struct {
227287
*tempdir.TempDirFixture
228288

0 commit comments

Comments
 (0)