-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_post_run.go
122 lines (106 loc) · 2.74 KB
/
step_post_run.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
116
117
118
119
120
121
122
package cmd
import (
"fmt"
"io"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// GetOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type StepPostRunOptions struct {
StepOptions
DisableImport bool
OutDir string
}
var ()
var (
StepPostRunLong = templates.LongDesc(`
This pipeline step executes any post build actions added during Pipeline execution
`)
StepPostRunExample = templates.Examples(`
jx step post run
`)
)
// NewCmdStep Steps a command object for the "step" command
func NewCmdStepPostRun(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &StepPostRunOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "run",
Short: "Runs any post build actions",
Long: StepPostRunLong,
Example: StepPostRunExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.Verbose, "verbose", "", false, "Enables verbose logging")
return cmd
}
// Run implements this command
func (o *StepPostRunOptions) Run() (err error) {
// TODO Support for conditions other than Always
f := o.Factory
client, ns, err := f.CreateJXClient()
if err != nil {
return errors.Wrap(err, "cannot create the JX client")
}
apisClient, err := o.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterPipelineActivityCRD(apisClient)
if err != nil {
return err
}
activities := client.JenkinsV1().PipelineActivities(ns)
if err != nil {
return err
}
gitInfo, err := o.FindGitInfo("")
appName := ""
if gitInfo != nil {
appName = gitInfo.Name
}
pipeline := ""
build := o.getBuildNumber()
pipeline, build = o.getPipelineName(gitInfo, pipeline, build, appName)
if pipeline != "" && build != "" {
name := kube.ToValidName(pipeline + "-" + build)
key := &kube.PromoteStepActivityKey{
PipelineActivityKey: kube.PipelineActivityKey{
Name: name,
Pipeline: pipeline,
Build: build,
},
}
a, _, err := key.GetOrCreate(activities)
if err != nil {
return err
}
for _, pe := range a.Spec.PostExtensions {
log.Infof("Running Extension %s\n", util.ColorInfo(fmt.Sprintf("%s.%s", pe.Namespace, pe.Name)))
err = pe.Execute(o.Verbose)
if err != nil {
return err
}
}
}
return nil
}