Skip to content

Commit

Permalink
use absolute paths to the container_image.tar in bazel build.
Browse files Browse the repository at this point in the history
  • Loading branch information
reltuk committed Jul 21, 2022
1 parent 12d9a22 commit a8fdc30
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
14 changes: 12 additions & 2 deletions pkg/skaffold/build/bazel/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (b *Builder) buildTar(ctx context.Context, out io.Writer, workspace string,
return "", fmt.Errorf("getting bazel tar path: %w", err)
}

return filepath.Join(workspace, tarPath), nil
return tarPath, nil
}

func (b *Builder) loadImage(ctx context.Context, out io.Writer, tarPath string, a *latest.BazelArtifact, tag string) (string, error) {
Expand Down Expand Up @@ -129,7 +129,17 @@ func bazelTarPath(ctx context.Context, workspace string, a *latest.BazelArtifact
return "", err
}

return strings.TrimSpace(string(buf)), nil
targetPath := strings.TrimSpace(string(buf))

cmd = exec.CommandContext(ctx, "bazel", "info", "execution_root")
buf, err = util.RunCmdOut(ctx, cmd)
if err != nil {
return "", err
}

execRoot := strings.TrimSpace(string(buf))

return filepath.Join(execRoot, targetPath), nil
}

func trimTarget(buildTarget string) string {
Expand Down
24 changes: 19 additions & 5 deletions pkg/skaffold/build/bazel/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestBuildBazel(t *testing.T) {
t.NewTempDir().Mkdir("bin").Chdir()
t.Override(&util.DefaultExecCommand, testutil.CmdRun("bazel build //:app.tar --color=no").AndRunOut(
"bazel cquery //:app.tar --output starlark --starlark:expr target.files.to_list()[0].path",
"bin/app.tar"))
"bin/app.tar").AndRunOut("bazel info execution_root", ""))
testutil.CreateFakeImageTar("bazel:app", "bin/app.tar")

artifact := &latest.Artifact{
Expand All @@ -52,11 +52,11 @@ func TestBuildBazel(t *testing.T) {
})
}

func TestBazelTarPathFRespectWorkspace(t *testing.T) {
func TestBazelTarPathPrependExecutionRoot(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, testutil.CmdRun("bazel build //:app.tar --color=no").AndRunOut(
"bazel cquery //:app.tar --output starlark --starlark:expr target.files.to_list()[0].path",
"app.tar"))
"app.tar").AndRunOut("bazel info execution_root", ".."))
testutil.CreateFakeImageTar("bazel:app", "../app.tar")

artifact := &latest.Artifact{
Expand Down Expand Up @@ -93,11 +93,11 @@ func TestBuildBazelFailInvalidTarget(t *testing.T) {
}

func TestBazelTarPath(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
testutil.Run(t, "EmptyExecutionRoot", func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
"bazel cquery //:skaffold_example.tar --output starlark --starlark:expr target.files.to_list()[0].path --arg1 --arg2",
"/absolute/path/bin\n",
))
).AndRunOut("bazel info execution_root", ""))

bazelBin, err := bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
BuildArgs: []string{"--arg1", "--arg2"},
Expand All @@ -107,6 +107,20 @@ func TestBazelTarPath(t *testing.T) {
t.CheckNoError(err)
t.CheckDeepEqual("/absolute/path/bin", bazelBin)
})
testutil.Run(t, "AbsoluteExecutionRoot", func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
"bazel cquery //:skaffold_example.tar --output starlark --starlark:expr target.files.to_list()[0].path --arg1 --arg2",
"bazel-bin/darwin-fastbuild-ST-confighash/path/to/bin\n",
).AndRunOut("bazel info execution_root", "/var/tmp/bazel-execution-roots/abcdefg/execroot/workspace_name"))

bazelBin, err := bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
BuildArgs: []string{"--arg1", "--arg2"},
BuildTarget: "//:skaffold_example.tar",
})

t.CheckNoError(err)
t.CheckDeepEqual("/var/tmp/bazel-execution-roots/abcdefg/execroot/workspace_name/bazel-bin/darwin-fastbuild-ST-confighash/path/to/bin", bazelBin)
})
}

func TestBuildImageTag(t *testing.T) {
Expand Down

0 comments on commit a8fdc30

Please sign in to comment.