forked from checkr/codeflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_builder.go
125 lines (105 loc) · 3.1 KB
/
docker_builder.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
package docker_build
import (
"bytes"
"fmt"
"github.com/checkr/codeflow/server/plugins"
docker "github.com/fsouza/go-dockerclient"
"github.com/spf13/viper"
)
type DockerBuilder struct {
dockerClient *docker.Client
rsaPrivateKey string
rsaPublicKey string
outputBuffer *bytes.Buffer
}
func NewDockerBuilder(
dockerClient *docker.Client,
rsaPrivateKey string,
rsaPublicKey string,
outputBuffer *bytes.Buffer,
) *DockerBuilder {
return &DockerBuilder{
dockerClient: dockerClient,
rsaPrivateKey: rsaPrivateKey,
rsaPublicKey: rsaPublicKey,
outputBuffer: outputBuffer,
}
}
func (b *DockerBuilder) fetchCode(build *plugins.DockerBuild) error {
_, err := plugins.GitCheckoutCommit(build.Feature.Hash, build.Project, build.Git)
if err != nil {
return err
}
return nil
}
func (b *DockerBuilder) build(build *plugins.DockerBuild) error {
repoPath := fmt.Sprintf("%s/%s", build.Git.Workdir, build.Project.Repository)
name := fmt.Sprintf("%s/%s:%s.%s", build.Registry.Host, build.Project.Repository, build.Feature.Hash, viper.GetString("environment"))
var buildArgs []docker.BuildArg
for _, arg := range build.BuildArgs {
ba := docker.BuildArg{
Name: arg.Key,
Value: arg.Value,
}
buildArgs = append(buildArgs, ba)
}
buildOptions := docker.BuildImageOptions{
Dockerfile: "Dockerfile",
Name: name,
OutputStream: b.outputBuffer,
BuildArgs: buildArgs,
ContextDir: repoPath,
}
if err := b.dockerClient.BuildImage(buildOptions); err != nil {
return err
}
return nil
}
func (b *DockerBuilder) push(build *plugins.DockerBuild) error {
var err error
name := fmt.Sprintf("%s/%s", build.Registry.Host, build.Project.Repository)
tag_hash := fmt.Sprintf("%s.%s", build.Feature.Hash, viper.GetString("environment"))
full_name := fmt.Sprintf("%s:%s", name, tag_hash)
tag_latest := "latest"
if viper.GetString("environment") != "production" {
tag_latest = fmt.Sprintf("%s.%s", "latest", viper.GetString("environment"))
}
b.outputBuffer.Write([]byte(fmt.Sprintf("Pushing %s:%s.%s...", name, build.Feature.Hash, viper.GetString("environment"))))
err = b.dockerClient.PushImage(docker.PushImageOptions{
Name: name,
Tag: tag_hash,
OutputStream: b.outputBuffer,
}, docker.AuthConfiguration{
Username: build.Registry.Username,
Password: build.Registry.Password,
Email: build.Registry.Email,
})
if err != nil {
return err
}
tagOptions := docker.TagImageOptions{
Repo: name,
Tag: tag_latest,
Force: true,
}
if err = b.dockerClient.TagImage(full_name, tagOptions); err != nil {
return err
}
err = b.dockerClient.PushImage(docker.PushImageOptions{
Name: name,
Tag: tag_latest,
OutputStream: b.outputBuffer,
}, docker.AuthConfiguration{
Username: build.Registry.Username,
Password: build.Registry.Password,
Email: build.Registry.Email,
})
if err != nil {
return err
}
build.Image = fmt.Sprintf("%s/%s:%s.%s", build.Registry.Host, build.Project.Repository, build.Feature.Hash, viper.GetString("environment"))
return nil
}
func (b *DockerBuilder) cleanup(build *plugins.DockerBuild) error {
return nil
}