-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_build.go
97 lines (81 loc) · 2.56 KB
/
cmd_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
package main
import (
"os"
"os/exec"
"path/filepath"
"github.com/altipla-consulting/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/altipla-consulting/wave/internal/env"
"github.com/altipla-consulting/wave/internal/query"
)
var cmdBuild = &cobra.Command{
Use: "build",
Short: "Build a container from a predefined folder structure.",
Example: "wave build foo",
Args: cobra.ExactArgs(1),
}
func init() {
var flagProject, flagSource string
cmdBuild.PersistentFlags().StringVar(&flagProject, "project", "", "Google Cloud project where the container will be stored. Defaults to the GOOGLE_PROJECT environment variable.")
cmdBuild.PersistentFlags().StringVar(&flagSource, "source", "", "Source folder. Defaults to a folder with the name of the app.")
cmdBuild.RunE = func(command *cobra.Command, args []string) error {
app := args[0]
if flagProject == "" {
flagProject = env.GoogleProject()
}
logger := log.WithFields(log.Fields{
"name": app,
"version": query.Version(command.Context()),
})
source := app
if flagSource != "" {
source = flagSource
}
imageTag := query.VersionImageTag(command.Context())
logger.Info("Build app")
buildArgs := []string{
"build",
"--cache-from", "eu.gcr.io/" + flagProject + "/" + app + ":latest",
"-f", source + "/Dockerfile",
"-t", "eu.gcr.io/" + flagProject + "/" + app + ":latest",
"-t", "eu.gcr.io/" + flagProject + "/" + app + ":" + imageTag,
}
npmrc := os.Getenv("NPM_CONFIG_USERCONFIG")
if npmrc == "" {
home, err := os.UserHomeDir()
if err != nil {
return errors.Trace(err)
}
if _, err := os.Stat(filepath.Join(home, ".npmrc")); err != nil && !os.IsNotExist(err) {
return errors.Trace(err)
} else if err == nil {
npmrc = filepath.Join(home, ".npmrc")
}
}
if npmrc != "" {
buildArgs = append(buildArgs, "--secret", "id=npmrc,src="+npmrc)
}
buildArgs = append(buildArgs, ".")
build := exec.Command("docker", buildArgs...)
build.Stdout = os.Stdout
build.Stderr = os.Stderr
if err := build.Run(); err != nil {
return errors.Trace(err)
}
logger.Info("Push to Container Registry")
push := exec.Command("docker", "push", "eu.gcr.io/"+flagProject+"/"+app+":latest")
push.Stdout = os.Stdout
push.Stderr = os.Stderr
if err := push.Run(); err != nil {
return errors.Trace(err)
}
push = exec.Command("docker", "push", "eu.gcr.io/"+flagProject+"/"+app+":"+imageTag)
push.Stdout = os.Stdout
push.Stderr = os.Stderr
if err := push.Run(); err != nil {
return errors.Trace(err)
}
return nil
}
}