forked from gitlabhq/gitlab-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor_docker_command.go
102 lines (84 loc) · 2.35 KB
/
executor_docker_command.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
package docker
import (
"bytes"
"errors"
"github.com/docker/docker/api/types"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/executors"
)
type commandExecutor struct {
executor
predefinedContainer *types.ContainerJSON
buildContainer *types.ContainerJSON
}
func (s *commandExecutor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
err := s.executor.Prepare(globalConfig, config, build)
if err != nil {
return err
}
s.Debugln("Starting Docker command...")
if len(s.BuildShell.DockerCommand) == 0 {
return errors.New("Script is not compatible with Docker")
}
imageName, err := s.getImageName()
if err != nil {
return err
}
buildImage, err := s.getPrebuiltImage()
if err != nil {
return err
}
// Start pre-build container which will git clone changes
s.predefinedContainer, err = s.createContainer("predefined", buildImage.ID, []string{"gitlab-runner-build"})
if err != nil {
return err
}
// Start build container which will run actual build
s.buildContainer, err = s.createContainer("build", imageName, s.BuildShell.DockerCommand)
if err != nil {
return err
}
return nil
}
func (s *commandExecutor) Run(cmd common.ExecutorCommand) error {
var runOn *types.ContainerJSON
if cmd.Predefined {
runOn = s.predefinedContainer
} else {
runOn = s.buildContainer
}
s.Debugln("Executing on", runOn.Name, "the", cmd.Script)
return s.watchContainer(runOn.ID, bytes.NewBufferString(cmd.Script), cmd.Abort)
}
func init() {
options := executors.ExecutorOptions{
DefaultBuildsDir: "/builds",
DefaultCacheDir: "/cache",
SharedBuildsDir: false,
Shell: common.ShellScriptInfo{
Shell: "bash",
Type: common.NormalShell,
RunnerCommand: "/usr/bin/gitlab-runner-helper",
},
ShowHostname: true,
SupportedOptions: []string{"image", "services"},
}
creator := func() common.Executor {
return &commandExecutor{
executor: executor{
AbstractExecutor: executors.AbstractExecutor{
ExecutorOptions: options,
},
},
}
}
featuresUpdater := func(features *common.FeaturesInfo) {
features.Variables = true
features.Image = true
features.Services = true
}
common.RegisterExecutor("docker", executors.DefaultExecutorProvider{
Creator: creator,
FeaturesUpdater: featuresUpdater,
})
}