-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.go
More file actions
221 lines (208 loc) · 6.02 KB
/
build.go
File metadata and controls
221 lines (208 loc) · 6.02 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package build
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
"github.com/apppackio/codebuild-image/builder/containers"
"github.com/docker/docker/api/types/container"
"github.com/google/go-containerregistry/pkg/crane"
cp "github.com/otiai10/copy"
"github.com/rs/zerolog"
)
const (
DockerHubMirror = "registry.apppackcdn.net"
CacheDirectory = "/tmp/apppack-cache"
)
func stripParamPrefix(params map[string]string, prefix string, final *map[string]string) {
for k, v := range params {
// strip prefix from k and add to final
(*final)[strings.TrimPrefix(k, prefix)] = v
}
}
func (b *Build) LoadBuildEnv() (map[string]string, error) {
paths := b.ConfigParameterPaths()
env := map[string]string{
"CI": "true",
}
// pass ALLOW_EOL_SHIMMED_BUILDER to pack if it is in the environment
// this facilitates testing of the eol shimmed builder
if val, ok := os.LookupEnv("ALLOW_EOL_SHIMMED_BUILDER"); ok {
env["ALLOW_EOL_SHIMMED_BUILDER"] = val
}
params, err := b.aws.GetParametersByPath(paths[0])
stripParamPrefix(params, paths[0], &env)
if err != nil {
return nil, err
}
if len(paths) > 1 {
// overlay vars from additional paths (for review apps)
for _, path := range paths[1:] {
params, err := b.aws.GetParametersByPath(path)
if err != nil {
return nil, err
}
stripParamPrefix(params, path, &env)
}
}
envOverride, err := b.state.ReadEnvFile()
if err != nil {
b.Log().Debug().Err(err).Msg("cannot read env file")
} else {
for k, v := range *envOverride {
env[k] = v
}
}
return env, nil
}
func (b *Build) BuildpackBuilders() []string {
if b.AppPackToml.Build.Builder != "" {
return []string{b.AppPackToml.Build.Builder}
}
return b.AppJSON.GetBuilders()
}
func (b *Build) RunBuild() error {
skipBuild, _ := b.state.ShouldSkipBuild(b.CodebuildBuildId)
if skipBuild {
b.Log().Info().Msg("skipping build")
return nil
}
logFileName := "build.log"
logFile, err := os.CreateTemp("", logFileName)
if err != nil {
return err
}
defer b.state.EndLogging(logFile, logFileName)
b.Log().Debug().Msg("loading build environment variables")
appEnv, err := b.LoadBuildEnv()
if err != nil {
return err
}
imageName, err := b.ImageName()
if err != nil {
return err
}
buildConfig := containers.NewBuildConfig(imageName, b.CodebuildBuildNumber, appEnv, logFile, CacheDirectory)
PrintStartMarker("build")
defer PrintEndMarker("build")
if b.System() == DockerBuildSystemKeyword {
err = b.buildWithDocker(buildConfig)
} else {
err = b.buildWithPack(buildConfig)
}
if err != nil {
return err
}
fmt.Println("===> PUBLISHING")
var wg sync.WaitGroup
wg.Add(1)
var cacheArchiveError error
go func() {
defer wg.Done()
cacheArchiveError = b.archiveCache()
}()
if err = b.pushImages(buildConfig); err != nil {
return err
}
wg.Wait()
if cacheArchiveError != nil {
return cacheArchiveError
}
if err = cp.Copy(logFile.Name(), "build.log"); err != nil {
return err
}
return b.state.WriteCommitTxt()
}
func (b *Build) buildWithDocker(config *containers.BuildConfig) error {
defer b.containers.Close()
defer config.LogFile.Close()
dockerfile := b.AppPackToml.Build.Dockerfile
if dockerfile == "" {
dockerfile = "Dockerfile"
}
return b.containers.BuildImage(dockerfile, config)
}
func (b *Build) buildWithPack(config *containers.BuildConfig) error {
b.Log().Debug().Msg("pack config registry-mirrors")
builder := b.BuildpackBuilders()[0]
packBinary := "pack"
if builder == "heroku/buildpacks:20" {
// use legacy pack for heroku/buildpacks:20
packBinary = "pack-legacy"
b.Log().Debug().Msg(fmt.Sprintf("using legacy pack version for %s", builder))
}
cmd := exec.Command(packBinary, "config", "registry-mirrors", "add", "index.docker.io", "--mirror", DockerHubMirror)
if err := cmd.Run(); err != nil {
return err
}
buildpacks := strings.Join(b.AppJSON.GetBuildpacks(), ",")
packArgs := []string{
"build",
"--builder", builder,
"--buildpack", buildpacks,
"--cache", fmt.Sprintf("type=build;format=bind;source=%s", CacheDirectory),
"--pull-policy", "if-not-present",
}
for k, v := range config.Env {
packArgs = append(packArgs, "--env", fmt.Sprintf("%s=%s", k, v))
}
if b.Log().GetLevel() <= zerolog.DebugLevel {
packArgs = append(packArgs, "--verbose", "--timestamps")
}
packArgs = append(packArgs, config.Image)
b.Log().Debug().Str("builder", builder).Str("buildpacks", buildpacks).Msg("building image")
cmd = exec.Command(packBinary, packArgs...)
out := io.MultiWriter(os.Stdout, config.LogFile)
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return err
}
fmt.Println("Extracting buildpack metadata")
defer b.containers.Close()
containerID := fmt.Sprintf("%s-%s", b.Appname, strings.ReplaceAll(b.CodebuildBuildId, ":", "-"))
cid, err := b.containers.CreateContainer(containerID, &container.Config{Image: config.Image})
if err != nil {
return err
}
defer b.containers.DeleteContainer(*cid)
reader, err := b.containers.GetContainerFile(*cid, "/layers/config/metadata.toml")
if err != nil {
return err
}
defer reader.Close()
if err := b.state.UnpackTarArchive(reader); err != nil {
return err
}
b.Log().Debug().Err(err).Msg("converting metadata.toml processes to apppack.toml services")
metadataToml, err := ParseBuildpackMetadataToml(b.Ctx)
if err != nil {
return err
}
if b.AppPackToml == nil {
b.AppPackToml = &AppPackToml{}
}
metadataToml.UpdateAppPackToml(b.AppPackToml)
return b.AppPackToml.Write(b.Ctx)
}
func (b *Build) pushImages(config *containers.BuildConfig) error {
fmt.Println("Pushing image tag", strings.Split(config.Image, ":")[1])
err := b.containers.PushImage(config.Image)
if err != nil {
return err
}
// once the first image is pushed, tag the other images
for _, tag := range []string{config.BuildTag, config.LatestTag} {
if err = crane.Tag(config.Image, tag); err != nil {
return err
}
}
return nil
}
func (b *Build) archiveCache() error {
fmt.Println("Archiving build cache to S3 ...")
quiet := b.Log().GetLevel() > zerolog.DebugLevel
return b.aws.SyncToS3(CacheDirectory, b.ArtifactBucket, "cache", quiet)
}