-
Notifications
You must be signed in to change notification settings - Fork 336
/
prebuild.go
97 lines (80 loc) · 2.66 KB
/
prebuild.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
package devcontainer
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/loft-sh/devpod/pkg/devcontainer/config"
"github.com/loft-sh/devpod/pkg/driver"
"github.com/loft-sh/devpod/pkg/driver/docker"
"github.com/loft-sh/devpod/pkg/image"
"github.com/loft-sh/devpod/pkg/provider"
"github.com/pkg/errors"
)
func (r *runner) Build(ctx context.Context, options provider.BuildOptions) (string, error) {
dockerDriver, ok := r.Driver.(driver.DockerDriver)
if !ok {
return "", fmt.Errorf("building only supported with docker driver")
}
substitutedConfig, substitutionContext, err := r.prepare(options.CLIOptions)
if err != nil {
return "", err
}
prebuildRepo := getPrebuildRepository(substitutedConfig)
if !options.SkipPush && options.Repository == "" && prebuildRepo == "" {
return "", fmt.Errorf("repository needs to be specified")
}
// remove build information
defer func() {
contextPath := config.GetContextPath(substitutedConfig.Config)
_ = os.RemoveAll(filepath.Join(contextPath, config.DevPodContextFeatureFolder))
}()
// check if we need to build container
buildInfo, err := r.build(ctx, substitutedConfig, substitutionContext, options)
if err != nil {
return "", errors.Wrap(err, "build image")
}
// have a fallback value for PrebuildHash
// in some cases it may be empty, and this would lead to
// invalid reference format during image pushing.
if buildInfo.PrebuildHash == "" {
buildInfo.PrebuildHash = "latest"
}
// prebuild already exists
var prebuildImage string
if options.Repository != "" {
prebuildImage = options.Repository + ":" + buildInfo.PrebuildHash
} else if prebuildRepo != "" {
prebuildImage = prebuildRepo + ":" + buildInfo.PrebuildHash
} else {
prebuildImage = docker.GetImageName(r.LocalWorkspaceFolder, buildInfo.PrebuildHash)
}
if buildInfo.ImageName == prebuildImage {
return buildInfo.ImageName, nil
}
// should we push?
if options.SkipPush {
return prebuildImage, nil
}
// check if we can push image
err = image.CheckPushPermissions(prebuildImage)
if err != nil {
return "", fmt.Errorf(
"cannot push to repository %s. Please make sure you are logged into the registry and credentials are available. (Error: %w)",
prebuildImage,
err,
)
}
// push the image to the registry
err = dockerDriver.PushDevContainer(ctx, prebuildImage)
if err != nil {
return "", errors.Wrap(err, "push image")
}
return prebuildImage, nil
}
func getPrebuildRepository(substitutedConfig *config.SubstitutedConfig) string {
if len(config.GetDevPodCustomizations(substitutedConfig.Config).PrebuildRepository) > 0 {
return config.GetDevPodCustomizations(substitutedConfig.Config).PrebuildRepository[0]
}
return ""
}