-
Notifications
You must be signed in to change notification settings - Fork 3
/
Jenkinsfile
294 lines (249 loc) · 11.7 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!groovy
@Library(['github.com/cloudogu/[email protected]', 'github.com/cloudogu/[email protected]', 'github.com/cloudogu/zalenium-build-lib@3092363']) _
import com.cloudogu.ces.cesbuildlib.*
import com.cloudogu.ces.dogubuildlib.*
def NAMESPACES = ["testing", "official"]
def IGNORE_TAG = "ignore-tag"
def BUILD_TAG = "build-existing-tag"
def TAG_STRATEGIES = [IGNORE_TAG, BUILD_TAG]
Git git = new Git(this, "cesmarvin")
GitHub github = new GitHub(this, git)
Changelog changelog = new Changelog(this)
node('vagrant') {
String cronTrigger = BRANCH_NAME == "develop" ? "@daily" : ""
timestamps {
def props = [];
props.add(string(defaultValue: getScmReleaseVersion(), description: 'SCM Version', name: 'ScmVersion', trim: true))
props.add(string(defaultValue: getDoguReleaseCounter(), description: 'Dogu Version Counter', name: 'DoguVersionCounter', trim: true))
props.add(choice(name: 'Tag_Strategy', choices: TAG_STRATEGIES))
for (namespace in NAMESPACES) {
props.add(booleanParam(defaultValue: isReleaseBuild() || isHotfixBuild(), description: "Push new dogu into registry with namespace '${namespace}'", name: "Push_${namespace}"))
}
properties([
// Keep only the last x builds to preserve space
buildDiscarder(logRotator(numToKeepStr: '10')),
// Don't run concurrent builds for a branch, because they use the same workspace directory
disableConcurrentBuilds(),
parameters(props),
pipelineTriggers([cron(cronTrigger)]),
])
catchError {
EcoSystem ecoSystem = new EcoSystem(this, "gcloud-ces-operations-internal-packer", "jenkins-gcloud-ces-operations-internal")
stage('Checkout') {
checkout([
$class : 'GitSCM',
branches : scm.branches,
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
extensions : scm.extensions + [[$class: 'CleanBeforeCheckout'], [$class: 'LocalBranch']],
userRemoteConfigs : scm.userRemoteConfigs
])
}
stage('Check for tag') {
if (params.Tag_Strategy != IGNORE_TAG) {
sh "git fetch --tags"
def tags = sh(returnStdout: true, script: 'git tag -l').trim().readLines()
if (params.Tag_Strategy == BUILD_TAG) {
if (!tags.contains(version)) {
error("Git tag ${version} does not exist.")
}
sh "git checkout 'refs/tags/${version}'"
} else if (isReleaseBuild() && tags.contains(version)) {
error("Git tag ${version} already exists.")
}
}
}
stage('Apply Parameters') {
if (version != null) {
ecoSystem.setVersion(version)
}
if (isNightly()) {
echo 'use snapshot dependencies for nightly build'
docker.image('groovy:3.0.9-jdk11').inside {
sh 'groovy build/latestsnapshot.groovy'
}
} else if (isReleaseBuild()) {
echo 'update dependencies for release build'
docker.image('groovy:3.0.9-jdk11').inside {
sh "groovy build/release.groovy ${releaseVersion}"
}
sh 'git add Dockerfile dogu.json'
commit "Release version ${releaseVersion}"
// fetch all remotes from origin
sh 'git config --replace-all "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"'
sh 'git fetch --all'
// checkout, reset and merge
sh 'git checkout master'
sh 'git reset --hard origin/master'
sh "git merge --ff-only ${env.BRANCH_NAME}"
tag getVersion()
} else if (isHotfixBuild()) {
echo 'update dependencies for hotfix build'
docker.image('groovy:3.0.9-jdk11').inside {
sh "groovy build/release.groovy ${releaseVersion}"
}
sh 'git add Dockerfile dogu.json'
commit "Release version ${releaseVersion}"
// fetch all remotes from origin
sh 'git config --replace-all "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"'
sh 'git fetch --all'
tag getVersion()
}
}
stage('Lint') {
// we cannot use the Dockerfile Linter because it fails without the labels `name` and `version` which we don't use
// lintDockerfile()
shellCheck("./resources/pre-upgrade.sh ./resources/startup.sh ./resources/upgrade-notification.sh")
}
try {
stage('Provision') {
ecoSystem.provision("/dogu");
}
stage('Setup') {
ecoSystem.loginBackend('cesmarvin-setup')
ecoSystem.setup()
}
stage('Wait for dependencies') {
timeout(15) {
ecoSystem.waitForDogu("cas")
ecoSystem.waitForDogu("usermgt")
}
}
stage('Build') {
ecoSystem.build("/dogu")
}
stage('Verify') {
ecoSystem.verify("/dogu")
}
stage('e2e Tests') {
ecoSystem.runCypressIntegrationTests([cypressImage: "cypress/included:12.17.1", enableVideo: true, enableScreenshots: true, additionalCypressArgs: "--browser chrome"])
}
stage('Push changes to remote repository') {
if (isReleaseBuild()) {
sh returnStatus: true, script: "git branch -D develop"
sh "git checkout develop"
sh "git -c user.name='CES Marvin' -c user.email='[email protected]' merge master"
authGit 'cesmarvin', 'push origin master --tags'
authGit 'cesmarvin', 'push origin develop --tags'
authGit 'cesmarvin', "push origin :${env.BRANCH_NAME}"
} else if (isHotfixBuild()) {
authGit 'cesmarvin', 'push origin --tags'
}
}
stage('Push') {
// No dogu release without tag allowed
if (params.Tag_Strategy != IGNORE_TAG || isReleaseBuild() || isHotfixBuild()) {
for (namespace in NAMESPACES) {
if (params."Push_${namespace}" != null && params."Push_${namespace}") {
ecoSystem.purge("scm")
ecoSystem.changeNamespace(namespace, "/dogu")
ecoSystem.build("/dogu")
ecoSystem.push("/dogu")
}
}
}
}
stage('Website') {
echo "update website for ${version}"
if (params.Push_official) {
dir('website') {
git branch: 'master', changelog: false, credentialsId: 'SCM-Manager', poll: false, url: 'https://ecosystem.cloudogu.com/scm/repo/scm-manager/website'
String releaseFile = "content/releases/${params.ScmVersion.replace('.', '-')}.yml"
def release = readYaml file: releaseFile
if (!containsReleasePackage(release, 'ces')) {
release.packages.add([type: 'ces'])
writeYaml file: releaseFile, data: release, overwrite: true
sh "git add ${releaseFile}"
sh "git -c user.name='CES_Marvin' -c user.email='[email protected]' commit -m 'Add ces package to release ${params.ScmVersion}' ${releaseFile}"
authGit 'SCM-Manager', 'push origin master'
} else {
echo "release ${params.ScmVersion} contains ces package already"
}
}
} else {
echo 'we only update the website if the official dogu is pushed'
}
}
stage('Add Github-Release') {
if (isReleaseBuild()) {
github.createReleaseWithChangelog(version, changelog)
}
}
} finally {
stage('Clean') {
ecoSystem.destroy()
if (isReleaseBuild()) {
sh "git tag -d ${getVersion()} || true"
}
}
}
}
if (currentBuild.currentResult == 'FAILURE') {
mail to: "[email protected]",
subject: "${JOB_NAME} - Build #${BUILD_NUMBER} - ${currentBuild.currentResult}!",
body: "Check console output at ${BUILD_URL} to view the results."
}
}
}
String getVersion() {
if (params.ScmVersion != null && !params.ScmVersion.isEmpty()) {
return "${params.ScmVersion}-${params.DoguVersionCounter}"
}
return null
}
boolean containsReleasePackage(release, packageType) {
boolean exists = false
for (int i=0; i < release.packages.size(); i++ ) {
if (release.packages[i].type == packageType) {
exists = true
break
}
}
return exists
}
boolean isNightly() {
return currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause').size() > 0
}
boolean isReleaseBuild() {
return env.BRANCH_NAME.startsWith("release/")
}
boolean isHotfixBuild() {
return env.BRANCH_NAME.startsWith("hotfix/")
}
String getReleaseVersion() {
if (isReleaseBuild()) {
return env.BRANCH_NAME.substring("release/".length())
} else if (isHotfixBuild()) {
return env.BRANCH_NAME.substring("hotfix/".length())
}
return ""
}
String getScmReleaseVersion() {
String doguVersion = getReleaseVersion()
int separator = doguVersion.lastIndexOf('-')
if (separator > 0) {
return doguVersion.substring(0, separator)
}
return ""
}
String getDoguReleaseCounter() {
String doguVersion = getReleaseVersion()
int separator = doguVersion.lastIndexOf('-')
if (separator > 0) {
return doguVersion.substring(separator + 1)
}
return '1'
}
void authGit(String credentials, String command) {
withCredentials([
usernamePassword(credentialsId: credentials, usernameVariable: 'AUTH_USR', passwordVariable: 'AUTH_PSW')
]) {
sh "git -c credential.helper=\"!f() { echo username='\$AUTH_USR'; echo password='\$AUTH_PSW'; }; f\" ${command}"
}
}
void commit(String message) {
sh "git -c user.name='CES Marvin' -c user.email='[email protected]' commit -m '${message}'"
}
void tag(String version) {
String message = "Release version ${version}"
sh "git -c user.name='CES Marvin' -c user.email='[email protected]' tag -m '${message}' ${version}"
}