forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkinsfiles.go
146 lines (119 loc) · 2.95 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
package builds
import (
"bufio"
"bytes"
"fmt"
"strings"
"github.com/jenkins-x/jx/pkg/config"
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")
for _, branchBuild := range projectConfig.Builds {
kind := branchBuild.Kind
build := &branchBuild.Build
name := branchBuild.Name
if name == "" {
name = strings.Title(kind)
}
// TODO hack!
branchPattern := "PR-*"
j.startBlock(fmt.Sprintf(`stage '%s'`, name))
if branchPattern != "" {
j.startBlock("when")
j.println(fmt.Sprintf(`branch '%s'`, branchPattern))
j.endBlock()
}
j.environmentBlock(branchBuild.Env)
j.startBlock("step")
j.startContainer()
for _, step := range build.Steps {
cmd := strings.Join(step.Args, " ")
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) printf(format string, args ...interface{}) {
j.writeIndent()
j.writeString(fmt.Sprintf(format, args...))
}
func (j *JenkinsConverter) String() string {
j.writer.Flush()
return j.buffer.String()
}
func (j *JenkinsConverter) writeString(text string) {
j.writer.WriteString(text)
}
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()
}
}