-
Notifications
You must be signed in to change notification settings - Fork 242
/
build_golang.go
134 lines (113 loc) · 3.88 KB
/
build_golang.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package pipeline
import (
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/gaia-pipeline/gaia/helper/filehelper"
"github.com/gaia-pipeline/gaia/helper/pipelinehelper"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
"github.com/gofrs/uuid"
)
const (
golangBinaryName = "go"
)
// BuildPipelineGolang is the real implementation of BuildPipeline for golang
type BuildPipelineGolang struct {
Type gaia.PipelineType
}
// PrepareEnvironment prepares the environment before we start the build process.
func (b *BuildPipelineGolang) PrepareEnvironment(p *gaia.CreatePipeline) error {
// create uniqueName for destination folder
v4, err := uuid.NewV4()
if err != nil {
return err
}
uniqueName := uuid.Must(v4, nil)
// Create local temp folder for clone
goPath := filepath.Join(gaia.Cfg.HomePath, gaia.TmpFolder, gaia.TmpGoFolder)
cloneFolder := filepath.Join(goPath, srcFolder, uniqueName.String())
err = os.MkdirAll(cloneFolder, 0700)
if err != nil {
return err
}
// Set new generated path in pipeline obj for later usage
if p.Pipeline.Repo == nil {
p.Pipeline.Repo = &gaia.GitRepo{}
}
p.Pipeline.Repo.LocalDest = cloneFolder
p.Pipeline.UUID = uniqueName.String()
return nil
}
// ExecuteBuild executes the golang build process
func (b *BuildPipelineGolang) ExecuteBuild(p *gaia.CreatePipeline) error {
// Look for golang executable
path, err := exec.LookPath(golangBinaryName)
if err != nil {
gaia.Cfg.Logger.Debug("cannot find go executable", "error", err.Error())
return err
}
goPath := filepath.Join(gaia.Cfg.HomePath, gaia.TmpFolder, gaia.TmpGoFolder)
// Set command args for get dependencies
args := []string{
"get",
"-d",
"./...",
}
env := append(os.Environ(), "GOPATH="+goPath)
// Set local destination
localDest := ""
if p.Pipeline.Repo != nil {
localDest = p.Pipeline.Repo.LocalDest
}
// Execute and wait until finish or timeout
output, err := executeCmd(path, args, env, localDest)
if err != nil {
gaia.Cfg.Logger.Debug("cannot get dependencies", "error", err.Error(), "output", string(output))
p.Output = string(output)
return err
}
// Set command args for build
args = []string{
"build",
"-o",
pipelinehelper.AppendTypeToName(p.Pipeline.Name, p.Pipeline.Type),
}
// Execute and wait until finish or timeout
output, err = executeCmd(path, args, env, localDest)
p.Output = string(output)
if err != nil {
gaia.Cfg.Logger.Debug("cannot build pipeline", "error", err.Error(), "output", string(output))
return err
}
// Build has been finished. Set execution path to the build result archive.
// This will be used during pipeline verification phase which will happen after this step.
p.Pipeline.ExecPath = filepath.Join(localDest, pipelinehelper.AppendTypeToName(p.Pipeline.Name, p.Pipeline.Type))
return nil
}
// CopyBinary copies the final compiled archive to the
// destination folder.
func (b *BuildPipelineGolang) CopyBinary(p *gaia.CreatePipeline) error {
// Define src and destination
src := filepath.Join(p.Pipeline.Repo.LocalDest, pipelinehelper.AppendTypeToName(p.Pipeline.Name, p.Pipeline.Type))
dest := filepath.Join(gaia.Cfg.PipelinePath, pipelinehelper.AppendTypeToName(p.Pipeline.Name, p.Pipeline.Type))
// Copy binary
if err := filehelper.CopyFileContents(src, dest); err != nil {
return err
}
// Set +x (execution right) for pipeline
return os.Chmod(dest, gaia.ExecutablePermission)
}
// SavePipeline saves the current pipeline configuration.
func (b *BuildPipelineGolang) SavePipeline(p *gaia.Pipeline) error {
dest := filepath.Join(gaia.Cfg.PipelinePath, pipelinehelper.AppendTypeToName(p.Name, p.Type))
p.ExecPath = dest
p.Type = gaia.PTypeGolang
p.Name = strings.TrimSuffix(filepath.Base(dest), typeDelimiter+gaia.PTypeGolang.String())
p.Created = time.Now()
// Our pipeline is finished constructing. Save it.
storeService, _ := services.StorageService()
return storeService.PipelinePut(p)
}