-
Notifications
You must be signed in to change notification settings - Fork 783
/
jenkinsfiles.go
157 lines (130 loc) · 3.35 KB
/
jenkinsfiles.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package builds
import (
"bufio"
"bytes"
"fmt"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/jenkinsfile"
corev1 "k8s.io/api/core/v1"
)
type JenkinsConverter struct {
Indentation string
KubernetesPluginMode bool
ProjectConfig *config.ProjectConfig
indentCount int
buffer bytes.Buffer
writer *bufio.Writer
}
// NewJenkinsConverter creates a new JenkinsConverter instance
func NewJenkinsConverter(projectConfig *config.ProjectConfig) *JenkinsConverter {
answer := &JenkinsConverter{
ProjectConfig: projectConfig,
Indentation: " ",
}
answer.writer = bufio.NewWriter(&answer.buffer)
return answer
}
func (j *JenkinsConverter) ToJenkinsfile() (string, error) {
projectConfig := j.ProjectConfig
pack := projectConfig.BuildPack
j.startBlock("pipeline")
j.startBlock("agent")
if pack != "" {
j.println(fmt.Sprintf(`agent 'jenkins-%s'`, pack))
}
j.endBlock()
j.environmentBlock(projectConfig.Env)
j.startBlock("stages")
pipelines := projectConfig.PipelineConfig
if pipelines != nil {
for pipelineKind, pipeline := range pipelines.Pipelines.AllMap() {
if pipeline == nil {
continue
}
for _, branchBuild := range pipeline.All() {
build := branchBuild.Lifecycle
if build == nil {
continue
}
branchPattern := ""
switch pipelineKind {
case jenkinsfile.PipelineKindRelease:
branchPattern = "master"
case jenkinsfile.PipelineKindPullRequest:
branchPattern = "PR-*"
case jenkinsfile.PipelineKindFeature:
branchPattern = "feature*"
default:
return "", fmt.Errorf("unknown pipeline kind %s", pipelineKind)
}
j.startBlock(fmt.Sprintf(`stage '%s'`, pipelineKind))
if branchPattern != "" {
j.startBlock("when")
j.println(fmt.Sprintf(`branch '%s'`, branchPattern))
j.endBlock()
}
j.environmentBlock(pipelines.Env)
j.startBlock("step")
j.startContainer()
for _, step := range build.Steps {
cmd := step.GetFullCommand()
j.println(fmt.Sprintf(`sh "%s"`, cmd))
}
j.endContainer()
j.endBlock()
j.endBlock()
}
}
}
j.endBlock()
j.endBlock()
return j.String(), nil
}
func (j *JenkinsConverter) startBlock(blockHeader string) {
j.println(blockHeader + " {")
j.indentCount++
}
func (j *JenkinsConverter) endBlock() {
j.indentCount--
j.println("}")
}
func (j *JenkinsConverter) writeIndent() {
for i := 0; i < j.indentCount; i++ {
j.writeString(j.Indentation)
}
}
func (j *JenkinsConverter) println(text string) {
j.writeIndent()
j.writeString(text)
j.writeString("\n")
}
func (j *JenkinsConverter) String() string {
j.writer.Flush() //nolint:errcheck
return j.buffer.String()
}
func (j *JenkinsConverter) writeString(text string) {
j.writer.WriteString(text) //nolint:errcheck
}
func (j *JenkinsConverter) startContainer() {
pack := j.ProjectConfig.BuildPack
if j.KubernetesPluginMode && pack != "" {
j.startBlock(fmt.Sprintf(`container('%s')`, pack))
}
}
func (j *JenkinsConverter) endContainer() {
pack := j.ProjectConfig.BuildPack
if j.KubernetesPluginMode && pack != "" {
j.endBlock()
}
}
func (j *JenkinsConverter) environmentBlock(envVars []corev1.EnvVar) {
if len(envVars) > 0 {
j.startBlock("environment")
for _, env := range envVars {
if env.Value != "" {
j.println(fmt.Sprintf(`%s = "%s"`, env.Name, env.Value))
}
}
j.endBlock()
}
}