diff --git a/cmd/cli/build.go b/cmd/cli/build.go index 1be1559ad284673f42ec3e5a4d20f7cb2409c730..71b69e03aa417c32594e67acfbf4a4d16cdd4236 100644 --- a/cmd/cli/build.go +++ b/cmd/cli/build.go @@ -260,12 +260,13 @@ func parseStaticBuildOpts() (*pb.BuildStatic, error) { func runBuild(ctx context.Context, cli Cli) (string, error) { var ( - err error - content string - dest string - isIsulad bool - imageID string - msg *pb.BuildResponse + isIsulad bool + msg *pb.BuildResponse + err error + content string + dest string + imageID string + imageIDFilePath string ) if dest, isIsulad, err = checkAndProcessOutput(); err != nil { @@ -277,6 +278,12 @@ func runBuild(ctx context.Context, cli Cli) (string, error) { if err = encryptBuildArgs(); err != nil { return "", errors.Wrap(err, "encrypt --build-arg failed") } + imageIDFilePath, err = getAbsPath(buildOpts.imageIDFile) + if err != nil { + return "", err + } + buildOpts.imageIDFile = imageIDFilePath + buildStatic, err := parseStaticBuildOpts() if err != nil { return "", err @@ -457,3 +464,18 @@ func resolveDockerfilePath() (string, error) { } return resolvedPath, nil } + +func getAbsPath(path string) (string, error) { + if path == "" { + return "", nil + } + if filepath.IsAbs(path) { + return path, nil + } + pwd, err := os.Getwd() + if err != nil { + return "", err + } + + return util.MakeAbsolute(path, pwd), nil +} diff --git a/cmd/cli/build_test.go b/cmd/cli/build_test.go index 6ba4b0f9ee589a810e68f4469d2f2a9594fddaf8..8de677250ab6e45f97e484fcc635f31494579dcf 100644 --- a/cmd/cli/build_test.go +++ b/cmd/cli/build_test.go @@ -613,3 +613,41 @@ func TestEncryptBuildArgs(t *testing.T) { }) } } + +func TestGetAbsPath(t *testing.T) { + pwd, _ := os.Getwd() + type args struct { + path string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "TC1 - normal case with relative path", + args: args{path: "./imageID.txt"}, + want: filepath.Join(pwd, "imageID.txt"), + wantErr: false, + }, + { + name: "TC2 - normal case with abs path", + args: args{path: filepath.Join(pwd, "imageID.txt")}, + want: filepath.Join(pwd, "imageID.txt"), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getAbsPath(tt.args.path) + if (err != nil) != tt.wantErr { + t.Errorf("getAbsPath() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("getAbsPath() got = %v, want %v", got, tt.want) + } + }) + } +}