@@ -5,6 +5,7 @@ package docker
5
5
6
6
import (
7
7
"fmt"
8
+ "os"
8
9
"path/filepath"
9
10
"strings"
10
11
@@ -40,17 +41,31 @@ func parseDockerImageListOutput(output []byte) []string {
40
41
return strings .Split (string (output ), "\n " )
41
42
}
42
43
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
+
43
58
// BuildDockerImage builds a docker image on a remote host.
44
59
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 )
47
61
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 )
50
64
goVersion = constants .BuildEnvGolangVersion
51
65
}
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 )
54
69
}
55
70
56
71
// 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
68
83
}
69
84
}()
70
85
// 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 {
72
87
return err
73
88
}
74
89
// build the image
0 commit comments