This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
seedBuilds.groovy
266 lines (227 loc) · 8.21 KB
/
seedBuilds.groovy
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import io.fabric8.repo.git.resteasy.ResteasyGitRepoClient;
// lets define the organsations and projects to include/exclude
def excludedProjectNames = []
def includedProjectNames = []
def username = "${JENKINS_GOGS_USER}"
def password = "${JENKINS_GOGS_PASSWORD}"
def address = "http://${GOGS_SERVICE_HOST}:${GOGS_SERVICE_PORT}/"
println "Using git api url: ${address}"
// lets try run a maven command
def callCommand(String command) {
def mvnProc = command.execute()
println ">>> ${command}"
println mvnProc.text
}
def mvnCall(goals) {
def mavenVersion = '3.3.1'
def mavenPath = "/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/${mavenVersion}/apache-maven-${mavenVersion}"
def mavenBin = "${mavenPath}/bin/mvn"
callCommand "${mavenBin} ${goals}"
}
def mvnFabric8CreateBuildConfig(options) {
def fabric8Version = '2.2.6'
def command = "io.fabric8:fabric8-maven-plugin:${fabric8Version}:devops ${options}"
println "Updating the DevOps configuration"
mvnCall command
}
def mvnFabric8CreateGerritRepo(options) {
def fabric8Version = '2.2.5'
def command = "io.fabric8:fabric8-maven-plugin:${fabric8Version}:create-gitrepo ${options}"
println "Creating a Gerrit Repository"
mvnCall command
}
def escape(token){
return token.replace('$', '\\$')
}
mavenJob('base-maven-build') {
keepDependencies(false)
environmentVariables {
env("JENKINS_GOGS_USERNAME", username)
env("JENKINS_GOGS_PASSWORD", escape(password))
}
logRotator(
1, // days to keep
5, // num to keep
-1, // artifact days to keep
-1 // artifact num to keep
)
wrappers {
timestamps()
colorizeOutput()
maskPasswords()
timeout {
elastic(
450, // Build will timeout when it take 3 time longer than the reference build duration, default = 150
5, // Number of builds to consider for average calculation
120 // 30 minutes default timeout (no successful builds available as reference)
)
failBuild()
}
}
mavenInstallation('3.3.1')
localRepository(LocalRepositoryLocation.LOCAL_TO_WORKSPACE)
}
freeStyleJob('base-freestyle-build') {
wrappers {
timestamps()
colorizeOutput()
maskPasswords()
golang('1.4.2')
timeout {
elastic(
150, // Build will timeout when it take 3 time longer than the reference build duration, default = 150
3, // Number of builds to consider for average calculation
30 // 30 minutes default timeout (no successful builds available as reference)
)
failBuild()
}
}
}
def client = ResteasyGitRepoClient.createWithContextClassLoader(address, username, password)
repos = client.listRepositories()
repos.each { repo ->
def fullName = repo.getFullName()
def gitUrl = repo.getCloneUrl()
def repoName = fullName.substring(fullName.indexOf("/") + 1)
println "Found repo name: ${repoName}, full: ${fullName}, clone url: ${gitUrl}"
if (!excludedProjectNames.contains(repoName) && (includedProjectNames.contains(repoName) || includedProjectNames.isEmpty())) {
println "Adding repo ${repoName} to jenkins build"
createJobs(repoName, fullName, gitUrl, username, password)
}
}
def createJobs(repoName, fullName, gitUrl, username, password) {
def firstJobName = "${repoName}-ci"
def monitorViewName = "${repoName}-cd-monitor"
def pipelineViewName = "${repoName}-cd-pipeline"
/**
* CI Build
*/
mavenJob(firstJobName) {
using('base-maven-build')
description('Run the build and the unit tests with a specific version. If they pass, move it to the next step.')
blockOnDownstreamProjects()
scm {
git(gitUrl, '*/master') {
clean(true)
createTag(false)
cloneTimeout(30)
}
}
publishers {
downstreamParameterized {
trigger(repoName + "-it", 'SUCCESS', false){
predefinedProp('RELEASE_NUMBER', '1.0.$BUILD_NUMBER')
}
}
}
preBuildSteps {
shell("git checkout -b ${repoName}-1.0.\$BUILD_NUMBER")
maven {
mavenInstallation('3.3.1')
goals('versions:set')
goals('-DnewVersion=1.0.$BUILD_NUMBER')
}
}
goals('clean deploy')
}
/**
* Integration Test Build
*/
mavenJob("${repoName}-it") {
using('base-maven-build')
description('Run the itests for this module (expected to have a maven profile named itests. If they pass, move it to the next step.')
parameters {
stringParam(
'RELEASE_NUMBER', // the release we pushed to nexus and git repo
'', // no good default for this at the moment
'The release we pushed to nexus and git repo'
)
}
scm {
git(gitUrl, '*/master') {
clean(true)
createTag(false)
cloneTimeout(30)
}
}
publishers {
downstreamParameterized {
trigger("${repoName}-dev-deploy", 'SUCCESS', false){
predefinedProp('RELEASE_NUMBER', '$RELEASE_NUMBER')
}
}
}
goals('verify -Pitests')
}
/**
* Deploy to DEV
*/
mavenJob("${repoName}-dev-deploy") {
using('base-maven-build')
parameters {
stringParam(
'RELEASE_NUMBER', // the release we pushed to nexus and git repo
'', // no good default for this at the moment
'The release we pushed to nexus and git repo'
)
}
scm {
git(gitUrl, '*/master') {
clean(true)
createTag(false)
cloneTimeout(30)
}
}
publishers {
downstreamParameterized {
trigger("${repoName}-dev-accept", 'SUCCESS', false){
predefinedProp('RELEASE_NUMBER', '$RELEASE_NUMBER')
}
}
}
goals('package docker:build fabric8:json fabric8:apply -Dfabric8.namespace=development')
}
/**
* Run Acceptance tests against DEV
*/
mavenJob("${repoName}-dev-accept") {
using('base-maven-build')
parameters {
stringParam(
'RELEASE_NUMBER', // the release we pushed to nexus and git repo
'', // no good default for this at the moment
'The release we pushed to nexus and git repo'
)
}
scm {
git(gitUrl, '*/master') {
clean(true)
createTag(false)
cloneTimeout(30)
}
}
goals('clean install')
}
buildPipelineView(pipelineViewName) {
selectedJob("${repoName}-ci")
title("Continuous Delivery pipeline for ${repoName}")
refreshFrequency(30)
showPipelineDefinitionHeader(true)
showPipelineParameters(true)
showPipelineParametersInHeaders(true)
displayedBuilds(10)
consoleOutputLinkStyle(OutputStyle.NewWindow)
}
buildMonitorView(monitorViewName) {
description("All jobs for the ${repoName} CD pipeline")
jobs {
name("${repoName}-ci")
name("${repoName}-it")
name("${repoName}-dev-deploy")
name("${repoName}-dev-accept")
}
}
// now lets create an OpenShift BuildConfig for the CI / CD pipeline and passing in details of the Jenkins jobs and views:
mvnFabric8CreateBuildConfig "-Dfabric8.repoName=${repoName} -Dfabric8.fullName=${fullName} -Dfabric8.gitUrl=${gitUrl} -Dfabric8.username=${username} -Dfabric8.password=${password} -Dfabric8.jenkinsMonitorView=${monitorViewName} -Dfabric8.jenkinsPipelineView=${pipelineViewName} -Dfabric8.jenkinsJob=${firstJobName}"
mvnFabric8CreateGerritRepo "-Drepo=${repoName} -DgerritAdminUsername=admin -DgerritAdminPassword=secret"
}