-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
267 lines (253 loc) · 6.64 KB
/
Jenkinsfile
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
265
266
267
#!groovy
// Load Jenkins shared libraries common to all projects
def libLazy = [
remote: 'https://github.com/digital-me/jenkins-lib-lazy.git',
branch: 'stable',
credentialsId: null,
]
library(
identifier: "libLazy@${libLazy.branch}",
retriever: modernSCM([
$class: 'GitSCMSource',
remote: libLazy.remote,
credentialsId: libLazy.credentialsId
])
)
// Load Jenkins shared libraries to customize this project
def libCustom = [
remote: 'ssh://git@code.in.digital-me.nl:2222/DEVops/JenkinsLibCustom.git',
branch: 'stable',
credentialsId: 'bot-ci-dgm-rsa',
]
library(
identifier: "libCustom@${libCustom.branch}",
retriever: modernSCM([
$class: 'GitSCMSource',
remote: libCustom.remote,
credentialsId: libCustom.credentialsId
])
)
// Load Jenkins shared libraries for rpmMake utils
def libRpmMake = [
remote: 'https://github.com/digital-me/rpmMake.git',
branch: 'stable',
credentialsId: null,
]
library(
identifier: "libRpmMake@${libRpmMake.branch}",
retriever: modernSCM([
$class: 'GitSCMSource',
remote: libRpmMake.remote,
credentialsId: libRpmMake.credentialsId
])
)
// Define the remotes and the working and deploy branches
def remote = 'origin'
def workingBranch = 'main'
def releaseBranch = 'stable'
// Initialize configuration
lazyConfig(
name: 'pkgmake',
inLabels: [ 'centos7', ],
env: [
VERSION: false,
RELEASE: false,
DRYRUN: false,
TARGET_DIR: 'target',
GIT_CRED: 'bot-ci-dgm-rsa',
DEPLOY_USER: 'root',
DEPLOY_HOST: 'pkg.in.dolden.net',
DEPLOY_DIR: '/var/mrepo',
DEPLOY_REPO: 'local',
DEPLOY_CRED: 'bot-ci-dgm-rsa',
],
noIndex: "(${releaseBranch}|.+_.+)", // Avoid automatic indexing for release and private branches
compressLog: false,
timestampsLog: true,
)
// Validate the project (parsing mostly)
lazyStage {
name = 'validate'
onlyif = ( lazyConfig['branch'] != releaseBranch ) // Skip when releasing
tasks = [
pre: {
def version = gitLastTag()
if (env.VERSION.toString() != 'true' && env.VERSION.toString() != 'false') {
version = env.VERSION.toString()
}
def release = '1'
if (version ==~ /.+-.+/) {
release = version.split('-')[1]
version = version - ~/-.+$/
}
currentBuild.displayName = "#${env.BUILD_NUMBER} ${version}-${release}"
},
run: {
def version = gitLastTag()
if (env.VERSION.toString() != 'true' && env.VERSION.toString() != 'false') {
version = env.VERSION.toString()
}
if (version ==~ /.+-.+/) {
version = version - ~/-.+$/
}
sh("make check VERSION=${version}")
},
in: '*', on: 'docker'
]
}
// Build and test the package(s) inside Docker
lazyStage {
name = 'test'
onlyif = ( lazyConfig['branch'] != releaseBranch ) // Skip when releasing
tasks = [
run: {
def version = gitLastTag()
if (env.VERSION.toString() != 'true' && env.VERSION.toString() != 'false') {
version = env.VERSION.toString()
}
def release = '1'
if (version ==~ /.+-.+/) {
release = version.split('-')[1]
version = version - ~/-.+$/
}
sh(
"""
DIST="\${LAZY_LABEL}-\$(arch)"
make \
VERSION=${version} \
RELEASE=${release} \
TARGET_DIR="\$(pwd)/\${TARGET_DIR}" \
DISTS_DIR="\$(pwd)/\${TARGET_DIR}/dists/\${DIST}" \
LOG_FILE=/dev/stdout
"""
)
sh(
"""
DIST="\${LAZY_LABEL}-\$(arch)"
cd "\${TARGET_DIR}/dists/\${DIST}"
sudo yum -y install *.rpm
"""
)
},
in: '*', on: 'docker',
]
}
// Build and store the package(s) for deployement
lazyStage {
name = 'package'
onlyif = ( lazyConfig['branch'] == releaseBranch )
tasks = [
run: {
def version = gitLastTag()
if (env.VERSION.toString() != 'true' && env.VERSION.toString() != 'false') {
version = env.VERSION.toString()
}
def release = '1'
if (version ==~ /.+-.+/) {
release = version.split('-')[1]
version = version - ~/-.+$/
}
currentBuild.displayName = "#${env.BUILD_NUMBER} ${version}-${release}"
sh(
"""
DIST="\${LAZY_LABEL}-\$(arch)"
make \
VERSION=${version} \
RELEASE=${release} \
TARGET_DIR="\$(pwd)/\${TARGET_DIR}" \
DISTS_DIR="\$(pwd)/\${TARGET_DIR}/dists/\${DIST}" \
LOG_FILE=/dev/stdout
"""
)
},
in: '*', on: 'docker',
post: {
archiveArtifacts(artifacts: "${env.TARGET_DIR}/dists/**", allowEmptyArchive: false)
},
]
}
// Release stage only if criteria are met
lazyStage {
name = 'release'
onlyif = ( lazyConfig['branch'] == workingBranch && lazyConfig.env.RELEASE )
// Ask version if release flag and set and we are in the branch to fork release from
input = [
message: 'Version string',
parameters: [string(
defaultValue: "${env.VERSION}",
description: "Version to be release: '*build*', 'micro', 'minor', 'major' or a specific string (i.e.: 1.2.3-4)",
name: 'VERSION'
)]
]
tasks = [
run: {
gitAuth(env.GIT_CRED, {
// Define next version based on optional input
def currentVersion = gitLastTag()
def nextVersion = null
if (env.lazyInput && env.lazyInput != currentVersion) {
if (env.lazyInput ==~ /[a-z]+/) {
nextVersion = bumpVersion(env.lazyInput, currentVersion)
} else {
nextVersion = env.lazyInput
}
} else {
nextVersion = bumpVersion('build', currentVersion)
}
// Merge changes from working into release branch
gitMerge(workingBranch, releaseBranch)
// Tag and publish changes in release branch
gitTag("${nextVersion}")
gitPush(remote, "${releaseBranch} ${nextVersion}")
// Update the displayed version for this build
currentVersion = gitLastTag()
currentBuild.displayName = "#${env.BUILD_NUMBER} ${currentVersion}"
})
},
// Can not be done in parallel
]
}
// Deliver the site on each environment
lazyStage {
name = 'testing'
onlyif = ( lazyConfig['branch'] == releaseBranch )
input = 'Deploy to systemtest?'
tasks = [
pre: {
unarchive(mapping:["${env.TARGET_DIR}/" : '.'])
},
run: 'repo_sync.sh', // Only to get the script copied from the lib
post: {
sshagent(credentials: [env.DEPLOY_CRED]) {
sh("./lazyDir/testing/repo_sync.sh"
+ " -s '${env.TARGET_DIR}/dists'"
+ " '${env.DEPLOY_USER}@${env.DEPLOY_HOST}:${env.DEPLOY_DIR}'"
+ " '${env.DEPLOY_REPO}-testing'"
)
}
},
// Can not be done in parallel
]
}
lazyStage {
name = 'stable'
onlyif = ( lazyConfig['branch'] == releaseBranch )
input = 'Deploy to production?'
tasks = [
pre: {
unarchive(mapping:["${env.TARGET_DIR}/" : '.'])
},
run: 'repo_sync.sh', // Only to get the script copied from the lib
post: {
sshagent(credentials: [env.DEPLOY_CRED]) {
sh("./lazyDir/stable/repo_sync.sh"
+ " -s '${env.TARGET_DIR}/dists'"
+ " -o '${env.DEPLOY_REPO}-testing'"
+ " '${env.DEPLOY_USER}@${env.DEPLOY_HOST}:${env.DEPLOY_DIR}'"
+ " '${env.DEPLOY_REPO}'"
)
}
},
// Can not be done in parallel
]
}