From d7c948db871baf8d928b4b3ce59daee0d5096eeb Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Tue, 10 Sep 2019 20:02:41 -0700 Subject: [PATCH 1/3] Update README.md --- Gopkg.toml | 3 ++- README.md | 1 + capsule.yml | 7 ++++--- cmd/capsulecd/capsulecd.go | 10 ++++------ pkg/engine/engine_golang.go | 17 +++++++++++++---- pkg/metadata/golang_metadata.go | 2 +- pkg/mgr/mgr_golang_dep.go | 8 +++++++- pkg/pipeline/pipeline_data.go | 4 ++++ 8 files changed, 36 insertions(+), 16 deletions(-) diff --git a/Gopkg.toml b/Gopkg.toml index 68d888c..6b6c29a 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -30,7 +30,8 @@ version = "v1.1.0" [[constraint]] - name = "gopkg.in/urfave/cli.v2" + name = "github.com/urfave/cli" + version = "=v1.19.1" [[constraint]] name = "github.com/google/go-github" diff --git a/README.md b/README.md index 11d4569..19acdbb 100644 --- a/README.md +++ b/README.md @@ -293,3 +293,4 @@ CapsuleCD is licensed under the MIT License - see the - https://opencredo.com/why-i-dont-like-error-handling-in-go/ - https://godoc.org/github.com/pkg/errors - https://blog.strapi.io/testing-npm-package-before-releasing-it-using-verdaccio-and-ngrok/ + diff --git a/capsule.yml b/capsule.yml index 8529f9d..583b99b 100644 --- a/capsule.yml +++ b/capsule.yml @@ -1,12 +1,13 @@ --- +engine_disable_cleanup: true engine_enable_code_mutation: true +#- cp /usr/local/osx-ndk-x86/macports/pkgs/opt/local/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc +# - '. /scripts/toolchains/osx/osx-build-env.sh && go build -ldflags "-X main.goos=darwin -X main.goarch=amd64" -o capsulecd-darwin-amd64 -tags "static" cmd/capsulecd/capsulecd.go' engine_cmd_compile: - mkdir -p vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/ - - cp /usr/local/osx-ndk-x86/macports/pkgs/opt/local/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc - - '. /scripts/toolchains/osx/osx-build-env.sh && go build -ldflags "-X main.goos=darwin -X main.goarch=amd64" -o capsulecd-darwin-amd64 -tags "static" $(go list ./cmd/...)' - cp /usr/local/linux/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc - - '. /scripts/toolchains/linux/linux-build-env.sh && go build -ldflags "-X main.goos=linux -X main.goarch=amd64" -o capsulecd-linux-amd64 -tags "static" $(go list ./cmd/...)' + - '. /scripts/toolchains/linux/linux-build-env.sh && go build -ldflags "-X main.goos=linux -X main.goarch=amd64" -o capsulecd-linux-amd64 -tags "static" cmd/capsulecd/capsulecd.go' - 'echo "listing linked libraries" && ldd capsulecd-linux-amd64' engine_cmd_test: 'go test -v -tags "static" ./...' engine_cmd_lint: 'gometalinter.v2 --vendor --config=gometalinter.json ./...' diff --git a/cmd/capsulecd/capsulecd.go b/cmd/capsulecd/capsulecd.go index 617a0b4..a22bd8f 100644 --- a/cmd/capsulecd/capsulecd.go +++ b/cmd/capsulecd/capsulecd.go @@ -10,7 +10,7 @@ import ( "github.com/analogj/capsulecd/pkg/errors" "github.com/analogj/capsulecd/pkg/utils" "github.com/analogj/capsulecd/pkg/version" - "gopkg.in/urfave/cli.v2" + "github.com/urfave/cli" "path/filepath" ) @@ -23,8 +23,8 @@ func main() { Usage: "Continuous Delivery scripts for automating package releases", Version: version.VERSION, Compiled: time.Now(), - Authors: []*cli.Author{ - &cli.Author{ + Authors: []cli.Author{ + cli.Author{ Name: "Jason Kulatunga", Email: "jason@thesparktree.com", }, @@ -49,7 +49,7 @@ func main() { return nil }, - Commands: []*cli.Command{ + Commands: []cli.Command{ { Name: "start", Usage: "Start a new CapsuleCD package pipeline", @@ -104,7 +104,6 @@ func main() { &cli.StringFlag{ Name: "package_type", - Aliases: []string{"package-type"}, Value: "default", Usage: "The type of package being built.", }, @@ -119,7 +118,6 @@ func main() { &cli.StringFlag{ Name: "config_file", - Aliases: []string{"config-file", "c"}, Usage: "Specifies the location of the config file", }, }, diff --git a/pkg/engine/engine_golang.go b/pkg/engine/engine_golang.go index ae50449..483fd13 100644 --- a/pkg/engine/engine_golang.go +++ b/pkg/engine/engine_golang.go @@ -27,7 +27,6 @@ type engineGolang struct { Scm scm.Interface //Interface CurrentMetadata *metadata.GolangMetadata NextMetadata *metadata.GolangMetadata - GoPath string } func (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 // to run, and hit the default deadline limit ( --deadline=30s). // we can have multiple workspaces in the gopath by separating them with colon (:), but this timeout is nasty if not required. //TODO: g.GoPath root will not be deleted (its the parent of GitParentPath), figure out if we can do this automatically. - g.GoPath = g.PipelineData.GitParentPath - os.Setenv("GOPATH", fmt.Sprintf("%s:%s", os.Getenv("GOPATH"), g.GoPath)) + g.PipelineData.GolangGoPath = g.PipelineData.GitParentPath + os.Setenv("GOPATH", fmt.Sprintf("%s:%s", os.Getenv("GOPATH"), g.PipelineData.GolangGoPath)) + + // A proper gopath has a bin and src directory. + goPathBin := path.Join(g.PipelineData.GitParentPath, "bin") + goPathSrc := path.Join(g.PipelineData.GitParentPath, "src") + os.MkdirAll(goPathBin, 0666) + os.MkdirAll(goPathSrc, 0666) + + // the gopath bin directory should aslo be added to Path + os.Setenv("PATH", fmt.Sprintf("%s:%s", os.Getenv("PATH"), goPathBin)) + packagePathPrefix := path.Dir(g.Config.GetString("engine_golang_package_path")) //strip out the repo name. // customize the git parent path for Golang Engine @@ -217,7 +226,7 @@ func (g *engineGolang) PackageStep() error { func (g *engineGolang) customGopathEnv() []string { currentEnv := os.Environ() - updatedEnv := []string{fmt.Sprintf("GOPATH=%s", g.GoPath)} + updatedEnv := []string{fmt.Sprintf("GOPATH=%s", g.PipelineData.GolangGoPath)} for i := range currentEnv { if !strings.HasPrefix(currentEnv[i], "GOPATH=") { //add all environmental variables that are not GOPATH diff --git a/pkg/metadata/golang_metadata.go b/pkg/metadata/golang_metadata.go index 7aa6a8e..19678f0 100644 --- a/pkg/metadata/golang_metadata.go +++ b/pkg/metadata/golang_metadata.go @@ -2,4 +2,4 @@ package metadata type GolangMetadata struct { Version string -} \ No newline at end of file +} diff --git a/pkg/mgr/mgr_golang_dep.go b/pkg/mgr/mgr_golang_dep.go index 1ca364d..0133bf7 100644 --- a/pkg/mgr/mgr_golang_dep.go +++ b/pkg/mgr/mgr_golang_dep.go @@ -1,6 +1,7 @@ package mgr import ( + "fmt" "github.com/analogj/capsulecd/pkg/config" "github.com/analogj/capsulecd/pkg/errors" "github.com/analogj/capsulecd/pkg/pipeline" @@ -54,7 +55,12 @@ func (m *mgrGolangDep) MgrAssembleStep() error { func (m *mgrGolangDep) MgrDependenciesStep(currentMetadata interface{}, nextMetadata interface{}) error { // the go source has already been downloaded. lets make sure all its dependencies are available. - if cerr := utils.BashCmdExec("dep ensure -v", m.PipelineData.GitLocalPath, nil, ""); cerr != nil { + updatedEnv := []string{ + fmt.Sprintf("GOPATH=%s:/go", m.PipelineData.GolangGoPath), + fmt.Sprintf("PATH=%s/bin:/go/bin", m.PipelineData.GolangGoPath), + + } + if cerr := utils.BashCmdExec("dep ensure -v", m.PipelineData.GitLocalPath, updatedEnv, ""); cerr != nil { return errors.EngineTestDependenciesError("dep ensure failed. Check dep dependencies") } diff --git a/pkg/pipeline/pipeline_data.go b/pkg/pipeline/pipeline_data.go index a6bc16b..8a08b98 100644 --- a/pkg/pipeline/pipeline_data.go +++ b/pkg/pipeline/pipeline_data.go @@ -13,4 +13,8 @@ type Data struct { ReleaseVersion string ReleaseCommit string ReleaseAssets []ScmReleaseAsset + + //Engine specific pipeline data + GolangGoPath string + GolangGoRoot string } From eecdb2b322229166da902f920ff7c0cc9fc90087 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Wed, 11 Sep 2019 21:42:59 -0700 Subject: [PATCH 2/3] new attempt at setting gopath. --- pkg/mgr/mgr_golang_dep.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/mgr/mgr_golang_dep.go b/pkg/mgr/mgr_golang_dep.go index 0133bf7..51c717e 100644 --- a/pkg/mgr/mgr_golang_dep.go +++ b/pkg/mgr/mgr_golang_dep.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "path" + "strings" ) func DetectGolangDep(pipelineData *pipeline.Data, myconfig config.Interface, client *http.Client) bool { @@ -55,11 +56,21 @@ func (m *mgrGolangDep) MgrAssembleStep() error { func (m *mgrGolangDep) MgrDependenciesStep(currentMetadata interface{}, nextMetadata interface{}) error { // the go source has already been downloaded. lets make sure all its dependencies are available. - updatedEnv := []string{ - fmt.Sprintf("GOPATH=%s:/go", m.PipelineData.GolangGoPath), - fmt.Sprintf("PATH=%s/bin:/go/bin", m.PipelineData.GolangGoPath), - + currentEnv := os.Environ() + updatedEnv := []string{fmt.Sprintf("GOPATH=%s", m.PipelineData.GolangGoPath)} + + for i := range currentEnv { + if strings.HasPrefix(currentEnv[i], "GOPATH="){ + //skip + continue + } else if (strings.HasPrefix(currentEnv[i], "PATH=")) { + updatedEnv = append(updatedEnv, fmt.Sprintf("PATH=%s/bin:%s", m.PipelineData.GolangGoPath, currentEnv[i])) + } else { + //add all environmental variables that are not GOPATH + updatedEnv = append(updatedEnv, currentEnv[i]) + } } + if cerr := utils.BashCmdExec("dep ensure -v", m.PipelineData.GitLocalPath, updatedEnv, ""); cerr != nil { return errors.EngineTestDependenciesError("dep ensure failed. Check dep dependencies") } From 04835db3b4a632cf4d580fdb1498de7d9bba8a5c Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Wed, 11 Sep 2019 22:01:11 -0700 Subject: [PATCH 3/3] set the GOPATH correctly for test, compile steps. --- pkg/engine/engine_golang.go | 19 +++++++++++++------ pkg/mgr/mgr_golang_dep.go | 4 +++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/engine/engine_golang.go b/pkg/engine/engine_golang.go index 483fd13..614c0b3 100644 --- a/pkg/engine/engine_golang.go +++ b/pkg/engine/engine_golang.go @@ -137,7 +137,7 @@ func (g *engineGolang) CompileStep() error { if terr := g.ExecuteCmdList("engine_cmd_compile", g.PipelineData.GitLocalPath, - nil, + g.customGopathEnv(), "", "Compile command (%s) failed. Check log for more details.", ); terr != nil { @@ -161,7 +161,7 @@ func (g *engineGolang) TestStep() error { //run lint command if terr := g.ExecuteCmdList("engine_cmd_lint", g.PipelineData.GitLocalPath, - nil, + g.customGopathEnv(), "", "Lint command (%s) failed. Check log for more details.", ); terr != nil { @@ -172,7 +172,7 @@ func (g *engineGolang) TestStep() error { //code formatter if terr := g.ExecuteCmdList("engine_cmd_fmt", g.PipelineData.GitLocalPath, - nil, + g.customGopathEnv(), "", "Format command (%s) failed. Check log for more details.", ); terr != nil { @@ -184,7 +184,7 @@ func (g *engineGolang) TestStep() error { //run test command if terr := g.ExecuteCmdList("engine_cmd_test", g.PipelineData.GitLocalPath, - nil, + g.customGopathEnv(), "", "Test command (%s) failed. Check log for more details.", ); terr != nil { @@ -196,7 +196,7 @@ func (g *engineGolang) TestStep() error { //run security check command if terr := g.ExecuteCmdList("engine_cmd_security_check", g.PipelineData.GitLocalPath, - nil, + g.customGopathEnv(), "", "Dependency vulnerability check command (%s) failed. Check log for more details.", ); terr != nil { @@ -229,10 +229,17 @@ func (g *engineGolang) customGopathEnv() []string { updatedEnv := []string{fmt.Sprintf("GOPATH=%s", g.PipelineData.GolangGoPath)} for i := range currentEnv { - if !strings.HasPrefix(currentEnv[i], "GOPATH=") { //add all environmental variables that are not GOPATH + if strings.HasPrefix(currentEnv[i], "GOPATH="){ + //skip + continue + } else if strings.HasPrefix(currentEnv[i], "PATH=") { + updatedEnv = append(updatedEnv, fmt.Sprintf("PATH=%s/bin:%s", g.PipelineData.GolangGoPath, currentEnv[i])) + } else { + //add all environmental variables that are not GOPATH updatedEnv = append(updatedEnv, currentEnv[i]) } } + return updatedEnv } diff --git a/pkg/mgr/mgr_golang_dep.go b/pkg/mgr/mgr_golang_dep.go index 51c717e..317f347 100644 --- a/pkg/mgr/mgr_golang_dep.go +++ b/pkg/mgr/mgr_golang_dep.go @@ -63,7 +63,7 @@ func (m *mgrGolangDep) MgrDependenciesStep(currentMetadata interface{}, nextMeta if strings.HasPrefix(currentEnv[i], "GOPATH="){ //skip continue - } else if (strings.HasPrefix(currentEnv[i], "PATH=")) { + } else if strings.HasPrefix(currentEnv[i], "PATH=") { updatedEnv = append(updatedEnv, fmt.Sprintf("PATH=%s/bin:%s", m.PipelineData.GolangGoPath, currentEnv[i])) } else { //add all environmental variables that are not GOPATH @@ -71,6 +71,8 @@ func (m *mgrGolangDep) MgrDependenciesStep(currentMetadata interface{}, nextMeta } } + print(updatedEnv) + if cerr := utils.BashCmdExec("dep ensure -v", m.PipelineData.GitLocalPath, updatedEnv, ""); cerr != nil { return errors.EngineTestDependenciesError("dep ensure failed. Check dep dependencies") }