-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
153 lines (132 loc) · 3.84 KB
/
build.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package common
import (
"errors"
"fmt"
"net/url"
"os"
"path"
"strconv"
"strings"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
)
type BuildState string
const (
Pending BuildState = "pending"
Running = "running"
Failed = "failed"
Success = "success"
)
type Build struct {
GetBuildResponse `yaml:",inline"`
Trace BuildTrace
BuildAbort chan os.Signal `json:"-" yaml:"-"`
RootDir string `json:"-" yaml:"-"`
BuildDir string `json:"-" yaml:"-"`
CacheDir string `json:"-" yaml:"-"`
Hostname string `json:"-" yaml:"-"`
Runner *RunnerConfig `json:"runner"`
ExecutorData ExecutorData
// Unique ID for all running builds on this runner
RunnerID int `json:"runner_id"`
// Unique ID for all running builds on this runner and this project
ProjectRunnerID int `json:"project_runner_id"`
}
func (b *Build) ProjectUniqueName() string {
return fmt.Sprintf("runner-%s-project-%d-concurrent-%d",
b.Runner.ShortDescription(), b.ProjectID, b.ProjectRunnerID)
}
func (b *Build) ProjectSlug() (string, error) {
url, err := url.Parse(b.RepoURL)
if err != nil {
return "", err
}
if url.Host == "" {
return "", errors.New("only URI reference supported")
}
slug := url.Path
slug = strings.TrimSuffix(slug, ".git")
slug = path.Clean(slug)
if slug == "." {
return "", errors.New("invalid path")
}
if strings.Contains(slug, "..") {
return "", errors.New("it doesn't look like a valid path")
}
return slug, nil
}
func (b *Build) ProjectUniqueDir(sharedDir bool) string {
dir, err := b.ProjectSlug()
if err != nil {
dir = fmt.Sprintf("project-%d", b.ProjectID)
}
// for shared dirs path is constructed like this:
// <some-path>/runner-short-id/concurrent-id/group-name/project-name/
// ex.<some-path>/01234567/0/group/repo/
if sharedDir {
dir = path.Join(
fmt.Sprintf("%s", b.Runner.ShortDescription()),
fmt.Sprintf("%d", b.ProjectRunnerID),
dir,
)
}
return dir
}
func (b *Build) FullProjectDir() string {
return helpers.ToSlash(b.BuildDir)
}
func (b *Build) StartBuild(rootDir, cacheDir string, sharedDir bool) {
b.RootDir = rootDir
b.BuildDir = path.Join(rootDir, b.ProjectUniqueDir(sharedDir))
b.CacheDir = path.Join(cacheDir, b.ProjectUniqueDir(false))
}
func (b *Build) Run(globalConfig *Config, trace BuildTrace) (err error) {
defer func() {
if err != nil {
trace.Fail(err)
} else {
trace.Success()
}
}()
b.Trace = trace
executor := NewExecutor(b.Runner.Executor)
if executor == nil {
fmt.Fprint(trace, "Executor not found:", b.Runner.Executor)
return errors.New("executor not found")
}
defer executor.Cleanup()
err = executor.Prepare(globalConfig, b.Runner, b)
if err == nil {
err = executor.Start()
}
if err == nil {
err = executor.Wait()
}
executor.Finish(err)
return err
}
func (b *Build) String() string {
return helpers.ToYAML(b)
}
func (b *Build) GetDefaultVariables() BuildVariables {
return BuildVariables{
{"CI", "true", true, true, false},
{"CI_BUILD_REF", b.Sha, true, true, false},
{"CI_BUILD_BEFORE_SHA", b.BeforeSha, true, true, false},
{"CI_BUILD_REF_NAME", b.RefName, true, true, false},
{"CI_BUILD_ID", strconv.Itoa(b.ID), true, true, false},
{"CI_BUILD_REPO", b.RepoURL, true, true, false},
{"CI_PROJECT_ID", strconv.Itoa(b.ProjectID), true, true, false},
{"CI_PROJECT_DIR", b.FullProjectDir(), true, true, false},
{"CI_SERVER", "yes", true, true, false},
{"CI_SERVER_NAME", "GitLab CI", true, true, false},
{"CI_SERVER_VERSION", "", true, true, false},
{"CI_SERVER_REVISION", "", true, true, false},
{"GITLAB_CI", "true", true, true, false},
}
}
func (b *Build) GetAllVariables() BuildVariables {
variables := b.Runner.GetVariables()
variables = append(variables, b.GetDefaultVariables()...)
variables = append(variables, b.Variables...)
return variables.Expand()
}