-
Notifications
You must be signed in to change notification settings - Fork 786
/
common_pipelines.go
130 lines (122 loc) · 3.55 KB
/
common_pipelines.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
123
124
125
126
127
128
129
130
package cmd
import (
"strconv"
"strings"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetLatestPipelineBuildByCRD returns the latest pipeline build
func (o *CommonOptions) GetLatestPipelineBuildByCRD(pipeline string) (string, error) {
// lets find the latest build number
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return "", err
}
pipelines, err := jxClient.JenkinsV1().PipelineActivities(ns).List(metav1.ListOptions{})
if err != nil {
return "", err
}
buildNumber := 0
for _, p := range pipelines.Items {
if p.Spec.Pipeline == pipeline {
b := p.Spec.Build
if b != "" {
n, err := strconv.Atoi(b)
if err == nil {
if n > buildNumber {
buildNumber = n
}
}
}
}
}
if buildNumber > 0 {
return strconv.Itoa(buildNumber), nil
}
return "1", nil
}
// GetPipelineName return the pipeline name
func (o *CommonOptions) GetPipelineName(gitInfo *gits.GitRepository, pipeline string, build string, appName string) (string, string) {
if pipeline == "" {
pipeline = o.getJobName()
}
if build == "" {
build = o.getBuildNumber()
}
if gitInfo != nil && pipeline == "" {
// lets default the pipeline name from the Git repo
branch, err := o.Git().Branch(".")
if err != nil {
log.Warnf("Could not find the branch name: %s\n", err)
}
if branch == "" {
branch = "master"
}
pipeline = util.UrlJoin(gitInfo.Organisation, gitInfo.Name, branch)
}
if pipeline == "" && appName != "" {
suffix := appName + "/master"
// lets try deduce the pipeline name via the app name
jxClient, ns, err := o.JXClientAndDevNamespace()
if err == nil {
pipelineList, err := jxClient.JenkinsV1().PipelineActivities(ns).List(metav1.ListOptions{})
if err == nil {
for _, pipelineResource := range pipelineList.Items {
pipelineName := pipelineResource.Spec.Pipeline
if strings.HasSuffix(pipelineName, suffix) {
pipeline = pipelineName
break
}
}
}
}
}
if pipeline == "" {
// lets try find
log.Warnf("No $JOB_NAME environment variable found so cannot record promotion activities into the PipelineActivity resources in kubernetes\n")
} else if build == "" {
// lets validate and determine the current active pipeline branch
p, b, err := o.getLatestPipelineBuild(pipeline)
if err != nil {
log.Warnf("Failed to try detect the current Jenkins pipeline for %s due to %s\n", pipeline, err)
build = "1"
} else {
pipeline = p
build = b
}
}
return pipeline, build
}
// getLatestPipelineBuild for the given pipeline name lets try find the Jenkins Pipeline and the latest build
func (o *CommonOptions) getLatestPipelineBuild(pipeline string) (string, string, error) {
log.Infof("pipeline %s\n", pipeline)
build := ""
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return pipeline, build, err
}
kubeClient, err := o.KubeClient()
if err != nil {
return pipeline, build, err
}
devEnv, err := kube.GetEnrichedDevEnvironment(kubeClient, jxClient, ns)
webhookEngine := devEnv.Spec.WebHookEngine
if webhookEngine == v1.WebHookEngineProw {
return pipeline, build, nil
}
jenkins, err := o.JenkinsClient()
if err != nil {
return pipeline, build, err
}
paths := strings.Split(pipeline, "/")
job, err := jenkins.GetJobByPath(paths...)
if err != nil {
return pipeline, build, err
}
build = strconv.Itoa(job.LastBuild.Number)
return pipeline, build, nil
}