-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJenkinsfile
272 lines (239 loc) · 15.2 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
#!groovy
@Library(['github.com/cloudogu/[email protected]', 'github.com/cloudogu/[email protected]'])
import com.cloudogu.ces.cesbuildlib.*
import com.cloudogu.ces.dogubuildlib.*
String repositoryOwner = 'cloudogu'
String doguName = "cas"
String branch = "${env.BRANCH_NAME}"
EcoSystem ecoSystem = new EcoSystem(this, "gcloud-ces-operations-internal-packer", "jenkins-gcloud-ces-operations-internal")
Trivy trivy = new Trivy(this, ecoSystem)
Git git = new Git(this, "cesmarvin")
git.committerName = 'cesmarvin'
git.committerEmail = '[email protected]'
GitFlow gitflow = new GitFlow(this, git)
GitHub github = new GitHub(this, git)
Changelog changelog = new Changelog(this)
String productionReleaseBranch = "master"
String developmentBranch = "develop"
currentBranch = "${env.BRANCH_NAME}"
String defaultEmailRecipients = env.EMAIL_RECIPIENTS
parallel(
"source code": {
node('docker') {
timestamps {
project = "github.com/${repositoryOwner}/${doguName}"
String gradleDockerImage = 'eclipse-temurin:21-jdk-alpine'
Gradle gradlew = new GradleWrapperInDocker(this, gradleDockerImage)
stage('Checkout') {
checkout scm
}
dir('app') {
stage('Build') {
gradlew "clean build"
}
stage('Unit Test') {
gradlew 'test'
junit allowEmptyResults: true, testResults: '**/build/test-results/test/TEST-*.xml'
}
}
stage('SonarQube') {
def scannerHome = tool name: 'sonar-scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
withSonarQubeEnv {
sh "git config 'remote.origin.fetch' '+refs/heads/*:refs/remotes/origin/*'"
gitWithCredentials("fetch --all")
if (currentBranch == productionReleaseBranch) {
echo "This branch has been detected as the production branch."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${env.BRANCH_NAME}"
} else if (currentBranch == developmentBranch) {
echo "This branch has been detected as the development branch."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${env.BRANCH_NAME}"
} else if (env.CHANGE_TARGET) {
echo "This branch has been detected as a pull request."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.pullrequest.key=${env.CHANGE_ID} -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH} -Dsonar.pullrequest.base=${developmentBranch}"
} else if (currentBranch.startsWith("feature/")) {
echo "This branch has been detected as a feature branch."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${env.BRANCH_NAME}"
} else {
echo "This branch has been detected as a miscellaneous branch."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${env.BRANCH_NAME} "
}
}
timeout(time: 2, unit: 'MINUTES') { // Needed when there is no webhook for example
def qGate = waitForQualityGate()
if (qGate.status != 'OK') {
unstable("Pipeline unstable due to SonarQube quality gate failure")
}
}
}
}
}
},
"dogu-integration": {
node('vagrant') {
sh 'echo testing dogu integration with ces'
timestamps {
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(),
// Parameter to activate dogu upgrade test on demand
parameters([
booleanParam(defaultValue: false, description: 'Test dogu upgrade from latest release or optionally from defined version below', name: 'TestDoguUpgrade'),
string(defaultValue: '', description: 'Old Dogu version for the upgrade test (optional; e.g. 3.23.0-1)', name: 'OldDoguVersionForUpgradeTest'),
booleanParam(defaultValue: true, description: 'Enables cypress to record video of the integration tests.', name: 'EnableVideoRecording'),
booleanParam(defaultValue: true, description: 'Enables cypress to take screenshots of failing integration tests.', name: 'EnableScreenshotRecording'),
choice(name: 'TrivyScanLevels', choices: [TrivyScanLevel.CRITICAL, TrivyScanLevel.HIGH, TrivyScanLevel.MEDIUM, TrivyScanLevel.ALL], description: 'The levels to scan with trivy'),
choice(name: 'TrivyStrategy', choices: [TrivyScanStrategy.UNSTABLE, TrivyScanStrategy.FAIL, TrivyScanStrategy.IGNORE], description: 'Define whether the build should be unstable, fail or whether the error should be ignored if any vulnerability was found.')
])
])
stage('Checkout') {
checkout scm
}
stage('Lint') {
Dockerfile dockerfile = new Dockerfile(this)
dockerfile.lint()
}
stage('Shellcheck') {
// TODO: Change this to shellCheck("./resources") as soon as https://github.com/cloudogu/dogu-build-lib/issues/8 is solved
shellCheck("./resources/startup.sh ./resources/logging.sh ./resources/util.sh ./resources/create-sa.sh ./resources/remove-sa.sh")
}
stage('Bats Tests') {
Bats bats = new Bats(this, docker)
bats.checkAndExecuteTests()
}
try {
stage('Provision') {
ecoSystem.provision("/dogu")
}
stage('Start OIDC-Provider') {
// template realm file
ecoSystem.vagrant.sshOut "sed 's/192.168.56.2/${ecoSystem.externalIP}/g' -i /dogu/integrationTests/keycloak-realm/realm-cloudogu.json"
sh "echo \"Starting Keycloak as OIDC provider on ${ecoSystem.externalIP}:9000\""
// start keycloak
ecoSystem.vagrant.sshOut 'sudo docker run -d --name kc -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin -p 9000:8080 -e KEYCLOAK_IMPORT=\'/realm-cloudogu.json -Dkeycloak.profile.feature.upload_scripts=enabled\' -v /dogu/integrationTests/keycloak-realm/realm-cloudogu.json:/realm-cloudogu.json quay.io/keycloak/keycloak:15.0.2'
}
stage('Setup') {
ecoSystem.loginBackend('cesmarvin-setup')
ecoSystem.setup([registryConfig:"""
"cas": {
"forgot_password_text": "Contact your admin",
"legal_urls": {
"privacy_policy": "https://www.triology.de/",
"terms_of_service": "https://docs.cloudogu.com/",
"imprint": "https://cloudogu.com/"
},
"oidc": {
"enabled": "true",
"discovery_uri": "http://${ecoSystem.externalIP}:9000/auth/realms/Cloudogu/.well-known/openid-configuration",
"client_id": "casClient",
"display_name": "MyProvider",
"optional": "true",
"scopes": "openid email profile groups",
"attribute_mapping": "email:mail,family_name:surname,given_name:givenName,preferred_username:username,name:displayName"
}
},
"_global": {
"password-policy": {
"must_contain_capital_letter": "true",
"must_contain_lower_case_letter": "true",
"must_contain_digit": "true",
"must_contain_special_character": "true",
"min_length": "14"
}
}
""", registryConfigEncrypted:'''
"cas" : {
"oidc": {
"client_secret": "c21a7690-1ca3-4cf9-bef3-22f37faf5144"
}
}
'''])
}
stage('Build dogu') {
// force post-upgrade from cas version 7.0.8-4 to migrate existing services from defaultSetupConfig
ecoSystem.vagrant.sshOut "sed 's/7.0.8-4/7.0.8-5/g' -i /dogu/dogu.json"
ecoSystem.build("/dogu")
}
stage('Trivy scan') {
trivy.scanDogu("/dogu", TrivyScanFormat.HTML, params.TrivyScanLevels, params.TrivyStrategy)
trivy.scanDogu("/dogu", TrivyScanFormat.JSON, params.TrivyScanLevels, params.TrivyStrategy)
trivy.scanDogu("/dogu", TrivyScanFormat.PLAIN, params.TrivyScanLevels, params.TrivyStrategy)
}
stage('Verify') {
ecoSystem.verify("/dogu")
}
stage('Wait for dependencies') {
timeout(15) {
ecoSystem.waitForDogu("nginx")
ecoSystem.waitForDogu("cas")
}
}
stage('Integration Tests') {
echo "Create custom dogu to access OAuth endpoints for the integration tests"
ecoSystem.vagrant.ssh "sudo docker cp /dogu/integrationTests/services/ cas:/etc/cas/services/production/"
ecoSystem.vagrant.sshOut "sudo docker exec cas ls /etc/cas/services/production"
ecoSystem.runCypressIntegrationTests([
cypressImage : "cypress/included:13.13.2",
enableVideo : params.EnableVideoRecording,
enableScreenshots: params.EnableScreenshotRecording])
// run special non-encrypted password test
echo "Run unencrypted password test script"
ecoSystem.vagrant.sshOut 'chmod +x /dogu/resources/test-password-logging.sh'
def testreport = ecoSystem.vagrant.sshOut "sudo /dogu/resources/test-password-logging.sh ${ecoSystem.externalIP}"
echo "${testreport}"
}
if (params.TestDoguUpgrade != null && params.TestDoguUpgrade) {
stage('Upgrade dogu') {
// Remove new dogu that has been built and tested above
ecoSystem.purgeDogu("--keep-config --keep-service-accounts --keep-volumes " + doguName)
if (params.OldDoguVersionForUpgradeTest != '' && !params.OldDoguVersionForUpgradeTest.contains('v')) {
println "Installing user defined version of dogu: " + params.OldDoguVersionForUpgradeTest
ecoSystem.installDogu("official/" + doguName + " " + params.OldDoguVersionForUpgradeTest)
} else {
println "Installing latest released version of dogu..."
ecoSystem.installDogu("official/" + doguName)
}
ecoSystem.startDogu(doguName)
ecoSystem.waitForDogu(doguName)
ecoSystem.upgradeDogu(ecoSystem)
// Wait for upgraded dogu to get healthy
ecoSystem.waitForDogu(doguName)
}
stage('Integration Tests - After Upgrade') {
ecoSystem.runCypressIntegrationTests([
cypressImage : "cypress/included:13.13.2",
enableVideo : params.EnableVideoRecording,
enableScreenshots: params.EnableScreenshotRecording])
}
}
if (gitflow.isReleaseBranch()) {
String releaseVersion = git.getSimpleBranchName()
stage('Finish Release') {
gitflow.finishRelease(releaseVersion, productionReleaseBranch)
}
stage('Push Dogu to registry') {
ecoSystem.push("/dogu")
}
stage('Add Github-Release') {
github.createReleaseWithChangelog(releaseVersion, changelog, productionReleaseBranch)
}
}
} finally {
stage('Clean') {
ecoSystem.destroy()
}
}
mailIfStatusChanged(findEmailRecipients(defaultEmailRecipients))
}
}
}
)
void gitWithCredentials(String command) {
withCredentials([usernamePassword(credentialsId: 'cesmarvin', usernameVariable: 'GIT_AUTH_USR', passwordVariable: 'GIT_AUTH_PSW')]) {
sh(
script: "git -c credential.helper=\"!f() { echo username='\$GIT_AUTH_USR'; echo password='\$GIT_AUTH_PSW'; }; f\" " + command,
returnStdout: true
)
}
}