-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_pre_build.go
83 lines (72 loc) · 2.07 KB
/
step_pre_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
package cmd
import (
"strings"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/cloud/amazon"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
)
// StepPreBuildOptions contains the command line flags
type StepPreBuildOptions struct {
StepOptions
Image string
}
var (
StepPreBuildLong = templates.LongDesc(`
This pipeline step performs pre build actions such as ensuring that a Docker registry is available in the cloud
`)
StepPreBuildExample = templates.Examples(`
jx step pre build ${DOCKER_REGISTRY}/someorg/myapp
`)
)
func NewCmdStepPreBuild(commonOpts *CommonOptions) *cobra.Command {
options := StepPreBuildOptions{
StepOptions: StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "build",
Short: "Performs actions before a build happens in a pipeline",
Long: StepPreBuildLong,
Example: StepPreBuildExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Image, optionImage, "i", "", "The image name that is about to be built")
return cmd
}
func (o *StepPreBuildOptions) Run() error {
imageName := o.Image
if imageName == "" {
args := o.Args
if len(args) == 0 {
return util.MissingOption(optionImage)
} else {
imageName = args[0]
}
}
paths := strings.Split(imageName, "/")
l := len(paths)
if l > 2 {
dockerRegistry := paths[0]
orgName := paths[l-2]
appName := paths[l-1]
log.Infof("Docker registry host: %s app name %s/%s\n", util.ColorInfo(dockerRegistry), util.ColorInfo(orgName), util.ColorInfo(appName))
kubeClient, err := o.KubeClient()
if err != nil {
return err
}
region, _ := kube.ReadRegion(kubeClient, o.currentNamespace)
if strings.HasSuffix(dockerRegistry, ".amazonaws.com") && strings.Index(dockerRegistry, ".ecr.") > 0 {
return amazon.LazyCreateRegistry(kubeClient, o.currentNamespace, region, dockerRegistry, orgName, appName)
}
}
return nil
}