-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_ar.go
94 lines (78 loc) · 2.55 KB
/
cmd_ar.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
package main
import (
"fmt"
"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 cmdAR = &cobra.Command{
Use: "ar",
Short: "Build a container from a predefined folder structure deploying to Artifact Registry.",
Example: "wave ar foo",
Args: cobra.ExactArgs(1),
}
func init() {
var flagProject, flagRepo, flagSource string
cmdAR.Flags().StringVar(&flagProject, "project", "", "Google Cloud project where the container will be stored. Defaults to the GOOGLE_PROJECT environment variable.")
cmdAR.Flags().StringVar(&flagRepo, "repo", "", "Artifact Registry repository name where the container will be stored.")
cmdAR.Flags().StringVar(&flagSource, "source", "", "Source folder. Defaults to a folder with the name of the app.")
cmdAR.MarkFlagRequired("repo")
cmdAR.RunE = func(cmd *cobra.Command, args []string) error {
app := args[0]
if flagProject == "" {
flagProject = env.GoogleProject()
}
version := query.VersionImageTag(cmd.Context())
logger := log.WithFields(log.Fields{
"name": app,
"version": version,
})
logger.Info("Build app")
source := app
if flagSource != "" {
source = flagSource
}
image := fmt.Sprintf("europe-west1-docker.pkg.dev/%s/%s/%s", flagProject, flagRepo, app)
docker := []string{
"build",
"--cache-from", image + ":latest",
"-f", source + "/Dockerfile",
"-t", image + ":latest",
"-t", image + ":" + version,
}
home, err := os.UserHomeDir()
if err != nil {
return errors.Trace(err)
}
if _, err := os.Stat(filepath.Join(home, ".npmrc")); err != nil && !os.IsNotExist(err) {
} else if err == nil {
docker = append(docker, "--secret", "id=npmrc,src="+filepath.Join(home, ".npmrc"))
}
docker = append(docker, ".") // build context
build := exec.CommandContext(cmd.Context(), "docker", docker...)
build.Stdout = os.Stdout
build.Stderr = os.Stderr
if err := build.Run(); err != nil {
return errors.Trace(err)
}
logger.Info("Push to Artifact Registry")
push := exec.CommandContext(cmd.Context(), "docker", "push", image+":latest")
push.Stdout = os.Stdout
push.Stderr = os.Stderr
if err := push.Run(); err != nil {
return errors.Trace(err)
}
push = exec.CommandContext(cmd.Context(), "docker", "push", image+":"+version)
push.Stdout = os.Stdout
push.Stderr = os.Stderr
if err := push.Run(); err != nil {
return errors.Trace(err)
}
return nil
}
}