-
Notifications
You must be signed in to change notification settings - Fork 577
/
Copy pathbuild.go
115 lines (93 loc) · 3.24 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
package docker
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/replicate/cog/pkg/config"
"github.com/replicate/cog/pkg/util"
"github.com/replicate/cog/pkg/util/console"
)
func Build(dir, dockerfileContents, imageName string, secrets []string, noCache bool, progressOutput string, epoch int64) error {
var args []string
userCache, err := UserCache()
if err != nil {
return err
}
args = append(args,
"buildx", "build", "--build-context", "usercache="+userCache,
)
if util.IsAppleSiliconMac(runtime.GOOS, runtime.GOARCH) {
// Fixes "WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested"
args = append(args, "--platform", "linux/amd64", "--load")
}
for _, secret := range secrets {
args = append(args, "--secret", secret)
}
if noCache {
args = append(args, "--no-cache")
}
// Base Images are special, we force timestamp rewriting to epoch. This requires some consideration on the output
// format. It's generally safe to override to --output type=docker,rewrite-timestamp=true as the use of `--load` is
// equivalent to `--output type=docker`
if epoch >= 0 {
args = append(args,
"--build-arg", fmt.Sprintf("SOURCE_DATE_EPOCH=%d", epoch),
"--output", "type=docker,rewrite-timestamp=true")
console.Infof("Forcing timestamp rewriting to epoch %d", epoch)
}
if config.BuildXCachePath != "" {
args = append(
args,
"--cache-from", "type=local,src="+config.BuildXCachePath,
"--cache-to", "type=local,dest="+config.BuildXCachePath,
)
} else {
args = append(args, "--cache-to", "type=inline")
}
args = append(args,
"--file", "-",
"--tag", imageName,
"--progress", progressOutput,
".",
)
cmd := exec.Command("docker", args...)
cmd.Dir = dir
cmd.Stdout = os.Stderr // redirect stdout to stderr - build output is all messaging
cmd.Stderr = os.Stderr
cmd.Stdin = strings.NewReader(dockerfileContents)
console.Debug("$ " + strings.Join(cmd.Args, " "))
return cmd.Run()
}
func BuildAddLabelsAndSchemaToImage(image string, labels map[string]string, bundledSchemaFile string, bundledSchemaPy string) error {
var args []string
args = append(args,
"buildx", "build",
)
if util.IsAppleSiliconMac(runtime.GOOS, runtime.GOARCH) {
// Fixes "WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested"
args = append(args, "--platform", "linux/amd64", "--load")
}
args = append(args,
"--file", "-",
"--tag", image,
)
for k, v := range labels {
// Unlike in Dockerfiles, the value here does not need quoting -- Docker merely
// splits on the first '=' in the argument and the rest is the label value.
args = append(args, "--label", fmt.Sprintf(`%s=%s`, k, v))
}
// We're not using context, but Docker requires we pass a context
args = append(args, ".")
cmd := exec.Command("docker", args...)
dockerfile := "FROM " + image + "\n"
dockerfile += "COPY " + bundledSchemaFile + " .cog\n"
cmd.Stdin = strings.NewReader(dockerfile)
console.Debug("$ " + strings.Join(cmd.Args, " "))
if combinedOutput, err := cmd.CombinedOutput(); err != nil {
console.Info(string(combinedOutput))
return err
}
return nil
}