Skip to content

Commit 5ec3ed5

Browse files
committed
r4r
1 parent 627e5cf commit 5ec3ed5

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

pkg/docker/image.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package docker
55

66
import (
77
"fmt"
8+
"os"
89
"path/filepath"
910
"strings"
1011

@@ -40,17 +41,31 @@ func parseDockerImageListOutput(output []byte) []string {
4041
return strings.Split(string(output), "\n")
4142
}
4243

44+
func parseRemoteGoModFile(path string, host *models.Host) (string, error) {
45+
goMod := filepath.Join(path, "go.mod")
46+
// download and parse go.mod
47+
tmpFile, err := os.CreateTemp("", "go-mod-*.txt")
48+
if err != nil {
49+
return "", err
50+
}
51+
defer os.Remove(tmpFile.Name())
52+
if err := host.Download(goMod, tmpFile.Name(), constants.SSHFileOpsTimeout); err != nil {
53+
return "", err
54+
}
55+
return utils.ReadGoVersion(tmpFile.Name())
56+
}
57+
4358
// BuildDockerImage builds a docker image on a remote host.
4459
func BuildDockerImage(host *models.Host, image string, path string, dockerfile string) error {
45-
// expectation is to have go.mod next to Dockerfile
46-
goVersion, err := utils.ReadGoVersion(filepath.Join(filepath.Dir(dockerfile), "go.mod"))
60+
goVersion, err := parseRemoteGoModFile(path, host)
4761
if err != nil {
48-
//fall back to default
49-
//return err
62+
// fall back to default
63+
ux.Logger.Info("failed to read go version from go.mod: %s. falling back to default", err)
5064
goVersion = constants.BuildEnvGolangVersion
5165
}
52-
_, err = host.Command(fmt.Sprintf("cd %s && docker build -q --build-arg GO_VERSION=%s -t %s -f %s .", path, goVersion, image, dockerfile), nil, constants.SSHLongRunningScriptTimeout)
53-
return err
66+
cmd := fmt.Sprintf("cd %s && docker build -q --build-arg GO_VERSION=%s -t %s -f %s .", path, goVersion, image, dockerfile)
67+
_, err = host.Command(cmd, nil, constants.SSHLongRunningScriptTimeout)
68+
return fmt.Errorf("failed building docker image: %s %w", cmd, err)
5469
}
5570

5671
// BuildDockerImageFromGitRepo builds a docker image from a git repo on a remote host.
@@ -68,7 +83,7 @@ func BuildDockerImageFromGitRepo(host *models.Host, image string, gitRepo string
6883
}
6984
}()
7085
// clone the repo and checkout commit
71-
if _, err := host.Command(fmt.Sprintf("git clone %s %s && cd %s && git checkout %s ", gitRepo, tmpDir, tmpDir, commit), nil, constants.SSHLongRunningScriptTimeout); err != nil {
86+
if _, err := host.Command(fmt.Sprintf("git clone %s %s && cd %s && git checkout %s && sleep 2", gitRepo, tmpDir, tmpDir, commit), nil, constants.SSHLongRunningScriptTimeout); err != nil {
7287
return err
7388
}
7489
// build the image

pkg/utils/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func ReadGoVersion(filePath string) (string, error) {
135135
if err != nil {
136136
return "", err
137137
}
138-
modFile, err := modfile.Parse("go.mod", data, nil)
138+
modFile, err := modfile.Parse(filePath, data, nil)
139139
if err != nil {
140140
return "", err
141141
}

pkg/utils/file_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ func createTempGoMod(t *testing.T, content string) string {
6767
// TestReadGoVersion tests all scenarios in one function using sub-tests.
6868
func TestReadGoVersion(t *testing.T) {
6969
t.Run("Success", func(t *testing.T) {
70-
tempFile := createTempGoMod(t, "module example.com/test\n\ngo 1.18\n")
70+
tempFile := createTempGoMod(t, "module example.com/test\n\ngo 1.23\n")
7171
defer os.Remove(tempFile) // Clean up the temp file
7272

7373
version, err := ReadGoVersion(tempFile)
7474
if err != nil {
7575
t.Fatalf("expected no error, got %v", err)
7676
}
7777

78-
expectedVersion := "1.18"
78+
expectedVersion := "1.23"
7979
if version != expectedVersion {
8080
t.Errorf("expected version %s, got %s", expectedVersion, version)
8181
}

0 commit comments

Comments
 (0)