-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
219 lines (186 loc) · 7.49 KB
/
Jenkinsfile
File metadata and controls
219 lines (186 loc) · 7.49 KB
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
#!groovy
//
// Shared values for different stages:
//
def projectName = "pjmeisch-cc-adi-spring-boot-test"
def gitRepoUrl = "https://github.com/pjmeisch-cc/adi-spring-boot-test.git"
// Branches whose Kubernetes resources will never be deleted
persistentBranches = ["master", "staging", "qa", "production"]
// Kubernetes resource time-to-live for non-persistent branches
nonPersistentBranchesTTL = 12 // hours
// the ODP kubernetes cluster to deploy to
def clusterId = 'eq5am'
// from .git folder or per jenkins Env BRANCH_NAME
def branchName = "unspecified"
// shortened git branch name
def simplifiedBranchName
// will be set later, for e.g. e2e test
def deployedUrl
// kubernetes namespace, lowercase department abbreviation:
def k8sNamespace = "cfd-dev"
def imageName
def currentBranchKubernetesExpiry
// cleanup: only store max 20 for max 15 days, and only 5 artifacts
properties([buildDiscarder([$class: 'EnhancedOldBuildDiscarder', artifactDaysToKeepStr: '', artifactNumToKeepStr: '5', daysToKeepStr: '15', discardOnlyOnSuccess: true, numToKeepStr: '20'])])
@Library('adidas@1.2.0') _
// Determines how long Kubernetes resources for a given branch are kept in the target cluster.
//
// The default behavior treats all branches except those specified in `persistentBranches`
// as temporary development branches.
//
// This must return a valid UNIX timestamp. See docs for the `giantswarm` function for details.
def branchKubernetesExpiry(String branchName) {
if (branchName in persistentBranches) {
return "never"
}
unixTimestamp.hoursFromNow(nonPersistentBranchesTTL)
}
node('java') {
stage('Prepare') {
checkout scm
pipelineUtils.initEnvironment()
projectName = metadata.withJenkinsIdSuffix(projectName)
imageName = metadata.getImageName(k8sNamespace, projectName, GIT_COMMIT)
currentBranchKubernetesExpiry = branchKubernetesExpiry(GIT_BRANCH)
currentBuild.displayName = "#$BUILD_NUMBER ($GIT_COMMIT)"
currentBuild.description = GIT_COMMIT_MESSAGE
}
stage('Deploy (dry-run)') {
giantswarm(
dryRun: true,
gitRepoUrl: gitRepoUrl,
branchName: GIT_BRANCH,
imageName: imageName,
simplifiedBranchName: SIMPLIFIED_GIT_BRANCH,
projectName: projectName,
commitId: GIT_COMMIT,
giantswarmClusterId: clusterId,
logEnvironment: 'dev',
logLocation: 'aws Ireland',
k8sNamespace: k8sNamespace,
teamId: 'dof',
expires: currentBranchKubernetesExpiry,
sourceFolder: "src/main/k8s"
)
}
stage('Compile') {
withMaven(
maven: 'maven'
) {
sh "mvn -Pci-build -Dmaven.test.failure.ignore clean compile compiler:testCompile"
gitUtils.createGitProperties('target/classes')
}
}
def testEnvironmentVariables = [
'K8S_CATALOG_SERVICE_PROTOCOL=http',
'K8S_CATALOG_SERVICE_HOST=example.com',
'K8S_CATALOG_SERVICE_PORT=8080'
]
stage('Unit test') {
withEnv(testEnvironmentVariables) {
withMaven(
maven: 'maven'
) {
sh "mvn -Dmaven.test.failure.ignore surefire:test"
}
junit '**/target/surefire-reports/TEST-*.xml'
}
}
stage('Coverage') {
withEnv(testEnvironmentVariables) {
withMaven(
maven: 'maven'
) {
sh "mvn -Dmaven.test.failure.ignore test jacoco:report"
}
step([$class: 'JacocoPublisher'])
}
}
stage('Integration test') {
withEnv(testEnvironmentVariables) {
withMaven(
maven: 'maven'
) {
sh "mvn compile compiler:testCompile failsafe:integration-test failsafe:verify"
}
junit '**/target/failsafe-reports/TEST-*.xml'
}
}
stage('jar') {
withMaven(
maven: 'maven'
) {
sh "mvn -DskipTests package"
}
}
stage('Package and publish') {
dockerUtils.buildAndPush image: imageName, repo: Constants.DOCKER_REGISTRY_AWS, credentials: Constants.DOCKER_REGISTRY_AWS_CREDENTIAL_ID, dockerFileLocation: '-f src/main/docker/Dockerfile .'
}
stage('Local Docker cleanup') {
dockerUtils.deleteOldImages(projectName, GIT_BRANCH)
}
stage('Vulnerability scan') {
def clairResult = clair.scanPushedImage(Constants.DOCKER_REGISTRY_AWS, imageName, Constants.DOCKER_REGISTRY_AWS_CREDENTIAL_ID)
if (clairResult.vulnerabilities) {
def clairResultJson = clairResult.json
clairResult = null // avoid NotSerializableException
clairResultJsonFile = 'clair-results.json'
writeFile file: clairResultJsonFile, text: clairResultJson
archiveArtifacts allowEmptyArchive: true, artifacts: clairResultJsonFile
echo "Vulnerabilities detected, marking build UNSTABLE"
currentBuild.result = "UNSTABLE"
} else {
clairResult = null // avoid NotSerializableException
echo "No vulnerabilities detected."
}
}
stage('Deploy') {
def g = giantswarm(
gitRepoUrl: gitRepoUrl,
branchName: GIT_BRANCH,
imageName: imageName,
simplifiedBranchName: SIMPLIFIED_GIT_BRANCH,
projectName: projectName,
commitId: GIT_COMMIT,
giantswarmClusterId: clusterId,
logEnvironment: 'dev',
logLocation: 'aws Ireland',
k8sNamespace: k8sNamespace,
teamId: 'dof',
expires: currentBranchKubernetesExpiry,
sourceFolder: "src/main/k8s"
)
deployedUrl = g.deployedUrl
echo "deployed on cluster: ${deployedUrl}"
}
stage('Smoke test') {
def maxTries = 10
def backOffFactor = 2
assertions.assertDeployedCommitId(GIT_COMMIT, "${deployedUrl}/_manage/info", maxTries, backOffFactor)
slackUtils.notify message: "$projectName deployed at $deployedUrl for branch *$GIT_BRANCH*", level: 'info', credentials: 'adi-devops-slack', team: 'adi-devops', channel: 'jenkins-feedback', addBuildInfo: false
}
stage('UI Test') {
withMaven(
maven: 'maven'
) {
withCredentials([usernamePassword(credentialsId: 'selenium-server', passwordVariable: 'SELENIUM_PASSWORD', usernameVariable: 'SELENIUM_USER')]) {
sh """
set +x
export SELENIUM_URL=http://${SELENIUM_USER}:${SELENIUM_PASSWORD}@selenium.dof.4c2tf.k8s.asgard.dub.aws.k8s.3stripes.net/wd/hub
export SPRINGBOOT_SEED_URL=$deployedUrl
mvn -f ui-test/pom.xml clean test
"""
}
}
}
/* Jenkins fails the step if reports are older than a certain time
* Some report results may not change per run, therefore we run 'touch'
* 'touch' on existing files, updates the timestamp to current time, thus fixing issue.
*/
sh 'touch ui-test/target/surefire-reports/TEST-*.xml'
sh 'touch ui-test/target/cucumber.json'
junit 'ui-test/target/surefire-reports/TEST-*.xml'
// Disabled due to Jenkins not having plugin [CucumberTestResultArchiver]
//step([$class: 'CucumberTestResultArchiver', testResults: 'ui-test/target/cucumber.json'])
archiveArtifacts allowEmptyArchive: true, artifacts: 'ui-test/target/cucumber/**'
}