@@ -27,7 +27,6 @@ type engineGolang struct {
2727 Scm scm.Interface //Interface
2828 CurrentMetadata * metadata.GolangMetadata
2929 NextMetadata * metadata.GolangMetadata
30- GoPath string
3130}
3231
3332func (g * engineGolang ) Init (pipelineData * pipeline.Data , config config.Interface , sourceScm scm.Interface ) error {
@@ -60,8 +59,18 @@ func (g *engineGolang) Init(pipelineData *pipeline.Data, config config.Interface
6059 // to run, and hit the default deadline limit ( --deadline=30s).
6160 // we can have multiple workspaces in the gopath by separating them with colon (:), but this timeout is nasty if not required.
6261 //TODO: g.GoPath root will not be deleted (its the parent of GitParentPath), figure out if we can do this automatically.
63- g .GoPath = g .PipelineData .GitParentPath
64- os .Setenv ("GOPATH" , fmt .Sprintf ("%s:%s" , os .Getenv ("GOPATH" ), g .GoPath ))
62+ g .PipelineData .GolangGoPath = g .PipelineData .GitParentPath
63+ os .Setenv ("GOPATH" , fmt .Sprintf ("%s:%s" , os .Getenv ("GOPATH" ), g .PipelineData .GolangGoPath ))
64+
65+ // A proper gopath has a bin and src directory.
66+ goPathBin := path .Join (g .PipelineData .GitParentPath , "bin" )
67+ goPathSrc := path .Join (g .PipelineData .GitParentPath , "src" )
68+ os .MkdirAll (goPathBin , 0666 )
69+ os .MkdirAll (goPathSrc , 0666 )
70+
71+ // the gopath bin directory should aslo be added to Path
72+ os .Setenv ("PATH" , fmt .Sprintf ("%s:%s" , os .Getenv ("PATH" ), goPathBin ))
73+
6574
6675 packagePathPrefix := path .Dir (g .Config .GetString ("engine_golang_package_path" )) //strip out the repo name.
6776 // customize the git parent path for Golang Engine
@@ -128,7 +137,7 @@ func (g *engineGolang) CompileStep() error {
128137
129138 if terr := g .ExecuteCmdList ("engine_cmd_compile" ,
130139 g .PipelineData .GitLocalPath ,
131- nil ,
140+ g . customGopathEnv () ,
132141 "" ,
133142 "Compile command (%s) failed. Check log for more details." ,
134143 ); terr != nil {
@@ -152,7 +161,7 @@ func (g *engineGolang) TestStep() error {
152161 //run lint command
153162 if terr := g .ExecuteCmdList ("engine_cmd_lint" ,
154163 g .PipelineData .GitLocalPath ,
155- nil ,
164+ g . customGopathEnv () ,
156165 "" ,
157166 "Lint command (%s) failed. Check log for more details." ,
158167 ); terr != nil {
@@ -163,7 +172,7 @@ func (g *engineGolang) TestStep() error {
163172 //code formatter
164173 if terr := g .ExecuteCmdList ("engine_cmd_fmt" ,
165174 g .PipelineData .GitLocalPath ,
166- nil ,
175+ g . customGopathEnv () ,
167176 "" ,
168177 "Format command (%s) failed. Check log for more details." ,
169178 ); terr != nil {
@@ -175,7 +184,7 @@ func (g *engineGolang) TestStep() error {
175184 //run test command
176185 if terr := g .ExecuteCmdList ("engine_cmd_test" ,
177186 g .PipelineData .GitLocalPath ,
178- nil ,
187+ g . customGopathEnv () ,
179188 "" ,
180189 "Test command (%s) failed. Check log for more details." ,
181190 ); terr != nil {
@@ -187,7 +196,7 @@ func (g *engineGolang) TestStep() error {
187196 //run security check command
188197 if terr := g .ExecuteCmdList ("engine_cmd_security_check" ,
189198 g .PipelineData .GitLocalPath ,
190- nil ,
199+ g . customGopathEnv () ,
191200 "" ,
192201 "Dependency vulnerability check command (%s) failed. Check log for more details." ,
193202 ); terr != nil {
@@ -217,13 +226,20 @@ func (g *engineGolang) PackageStep() error {
217226
218227func (g * engineGolang ) customGopathEnv () []string {
219228 currentEnv := os .Environ ()
220- updatedEnv := []string {fmt .Sprintf ("GOPATH=%s" , g .GoPath )}
229+ updatedEnv := []string {fmt .Sprintf ("GOPATH=%s" , g .PipelineData . GolangGoPath )}
221230
222231 for i := range currentEnv {
223- if ! strings .HasPrefix (currentEnv [i ], "GOPATH=" ) { //add all environmental variables that are not GOPATH
232+ if strings .HasPrefix (currentEnv [i ], "GOPATH=" ){
233+ //skip
234+ continue
235+ } else if strings .HasPrefix (currentEnv [i ], "PATH=" ) {
236+ updatedEnv = append (updatedEnv , fmt .Sprintf ("PATH=%s/bin:%s" , g .PipelineData .GolangGoPath , currentEnv [i ]))
237+ } else {
238+ //add all environmental variables that are not GOPATH
224239 updatedEnv = append (updatedEnv , currentEnv [i ])
225240 }
226241 }
242+
227243 return updatedEnv
228244}
229245
0 commit comments