-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
291 lines (274 loc) · 10.5 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
#!/usr/bin/env groovy
/*
This file is used for CoreMedia internal Deployment.
Please ignore or delete.
*/
import com.coremedia.cm.DockerAgent
import com.coremedia.cm.Jenkins
import com.coremedia.comhub.ComhubHelper
@org.jenkinsci.plugins.workflow.libs.Library(['coremedia-internal', 'coremedia-commerce', 'coremedia-aws-v2']) _
final String PROJECT_NAME = 'headless-server-commerce'
final String RELEASE_DOCKER_REPOSITORY_NAME = 'coremedia'
final String RELEASE_DOCKER_IMAGE_NAME = PROJECT_NAME
final String RELEASE_LOCAL_STAGING_DIR = '${WORKSPACE}/target/nexus-staging'
final String DOCKER_IMAGE_MAVEN = "${Jenkins.ecrPullThroughProxyRegistry}/cm-tools/maven:3.8.6-17.0.12.7-1-cm-1.1.3"
final String DOCKER_SNAPSHOTS_REGISTRY = "${Jenkins.getDockerRegistry(env)}/${RELEASE_DOCKER_REPOSITORY_NAME}"
final String DOCKER_RELEASES_REGISTRY = "${ComhubHelper.releasesCommerceRegistryUpstream}/${RELEASE_DOCKER_REPOSITORY_NAME}"
final Map<String, String> DEFAULT_MAVEN_PARAMS = [
'application.image-prefix' : DOCKER_SNAPSHOTS_REGISTRY,
'jib.allowInsecureRegistries': 'true',
'jib.goal' : 'build',
'enforcer.skip' : 'true',
'mdep.analyze.skip' : 'true',
'sort.skip' : 'true',
'sort.verifyFail' : 'stop',
'skipTests' : 'true',
'maven.javadoc.skip' : 'true',
].asImmutable()
final String GITHUB_COMMIT_STATUS_CONTEXT = "${PROJECT_NAME} pipeline"
boolean isDefaultBuild = !params.BUILD_TYPE
boolean isReleaseStaging = params.BUILD_TYPE == 'RELEASE_STAGING'
boolean isTestMode = params.BRANCH.startsWith('test/')
String gitDryRun = isTestMode ? '--dry-run' : ''
String version
String releaseTag
String releaseNextDevelopmentVersion
String tmpDockerImageTagRelease
pipeline {
agent {
label Jenkins.defaultNodeLabel
}
options {
disableResume()
timestamps()
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '10'))
durabilityHint('PERFORMANCE_OPTIMIZED')
newContainerPerStage()
}
stages {
stage('Prepare') {
steps {
script {
String projectVersion = comhubXmlStarletSelect(xmlFile: 'pom.xml', xPath: 'project/version')
if (isReleaseStaging) {//input example: 2.0.40-RC-SNAPSHOT
version = projectVersion.replace('-SNAPSHOT', '') //example: 2.0.40-RC
String versionExtension = version.replaceAll('[\\d.]*','') //example: -RC
tmpDockerImageTagRelease = "${version}-tmp-rc-SNAPSHOT"
cmBuildDescription(getUser: true, gitLink: true, information: ['Release Staging': version])
env.RELEASE_VERSION = version // Show release version in build description of (outer) release pipeline
releaseTag = "${PROJECT_NAME}-${version}"
int nextDeVPatchVersion = 1 + Integer.valueOf(version.replaceAll('.*\\.', '').replaceAll('[^\\d.]*','')) //example: 41
releaseNextDevelopmentVersion = version.replaceAll("\\d+${versionExtension}", "${nextDeVPatchVersion}${versionExtension}-SNAPSHOT") //example: 2.0.41-RC-SNAPSHOT
cmGitLocalAuth()
} else {
String gitShortRef = sh(label: 'Get short ref from HEAD commit', returnStdout: true,
script: 'git rev-parse --short HEAD').trim()
version = projectVersion.replace('-SNAPSHOT', "-${gitShortRef}-SNAPSHOT")
cmBuildDescription(getUser: true, gitLink: true, information: ['Build Version': version])
cmSetGitCommitStatus(context: GITHUB_COMMIT_STATUS_CONTEXT, repository: PROJECT_NAME)
}
}
}
}
stage('Maven') {
when {
expression { isDefaultBuild || isReleaseStaging }
beforeAgent true
}
agent {
docker {
image DOCKER_IMAGE_MAVEN
args DockerAgent.defaultMavenArgs
reuseNode true
}
}
stages {
stage('Set Version') {
steps {
cmMaven(cmd: 'versions:set',
mavenParams: ['newVersion' : "${version}",
'generateBackupPoms' : 'false',
'updateMatchingVersions': 'false', // https://stackoverflow.com/questions/16865743/updating-the-versions-in-a-maven-multi-module-project
'processAllModules' : 'true'],
scanMvnLog: true,
)
}
}
stage('Build') {
steps {
cmMaven(cmd: 'deploy -Pdefault-image',
mavenParams: DEFAULT_MAVEN_PARAMS + (
isReleaseStaging
? ['performRelease' : 'true',
'altReleaseDeploymentRepository': "local::default::file://${RELEASE_LOCAL_STAGING_DIR}",
'application.image-tag' : tmpDockerImageTagRelease]
: [:]),
scanMvnLog: true,
)
}
}
stage('Test') {
steps {
cmMaven(cmd: 'verify -Pintegration-test',
mavenParams: DEFAULT_MAVEN_PARAMS + ['skipTests': 'false'],
scanMvnLog: true,
)
}
post {
always {
junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml,**/target/failsafe-reports/*.xml'
}
}
}
stage('Check Enforcer Rules') {
steps {
cmMaven(cmd: 'validate',
mavenParams: DEFAULT_MAVEN_PARAMS + ['enforcer.skip': 'false'],
scanMvnLog: true,
)
}
}
stage('Check Dependencies') {
steps {
cmMaven(cmd: 'verify',
mavenParams: DEFAULT_MAVEN_PARAMS + ['mdep.analyze.skip': 'false'],
scanMvnLog: true,
)
}
}
stage('Check POM Formatting') {
steps {
cmMaven(cmd: 'validate',
mavenParams: DEFAULT_MAVEN_PARAMS + ['sort.skip': 'false'],
scanMvnLog: true,
)
}
}
stage('Sonar') {
steps {
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
cmMaven(cmd: 'sonar:sonar -Pci-sonar',
mavenParams: ['sonar.userHome' : "${env.WORKSPACE}",
'sonar.projectKey': "${PROJECT_NAME}_${Jenkins.getNodeName(env.JENKINS_URL)}_${env.BRANCH.replace('/', '-')}"],
useJavaTrustStore: true,
scanMvnLog: true,
)
}
}
post {
success {
archiveArtifacts artifacts: "**/target/sonar/report-task.txt"
}
}
}
}
}
stage('Checks Sonar Results') {
when {
expression { isDefaultBuild || isReleaseStaging }
beforeAgent true
}
agent {
docker {
image 'groovy:2.5.8-jdk11'
args params.DOCKER_BUILD_NODE_ARGS
reuseNode true
}
}
steps {
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
sh(label: "Check Sonar results", returnStdout: false,
script: "groovy workspace-config/sonar/SonarStatusCheck.groovy 'target/sonar/report-task.txt'")
}
}
}
stage('Release Staging') {
when {
expression { isReleaseStaging }
beforeAgent true
}
stages {
stage('Push Docker Image') {
when {
expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') }
beforeAgent true
}
steps {
script {
String tmpImage = "${DOCKER_SNAPSHOTS_REGISTRY}/${RELEASE_DOCKER_IMAGE_NAME}:${tmpDockerImageTagRelease}"
String releaseImage = "${DOCKER_RELEASES_REGISTRY}/${RELEASE_DOCKER_IMAGE_NAME}:${version}"
cmBash(label: 'Pull and tag release image', returnStdout: true,
script: "docker pull ${tmpImage} && docker tag ${tmpImage} ${releaseImage}")
comhubDockerPush(tag: "${releaseImage}",
awsCredentialsId: ComhubHelper.ecrCredentialsId,
ec2Region: ComhubHelper.ecrRegion,
dryRun: isTestMode)
}
}
}
stage('Git Push Release') {
when {
expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') }
beforeAgent true
}
steps {
sh(label: 'Commit release version and create tag', returnStdout: true, script: """#!/usr/bin/env bash
set -euo pipefail
git ls-files --modified | xargs -n1 git add
git commit --message="Release ${releaseTag}"
git push origin HEAD:refs/heads/${params.BRANCH}
git tag --annotate --message="Release ${releaseTag}" ${releaseTag}
git push ${gitDryRun} origin refs/tags/${releaseTag}:refs/tags/${releaseTag}
""")
}
}
stage('Set next SNAPSHOT') {
when {
expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') }
beforeAgent true
}
agent {
docker {
image DOCKER_IMAGE_MAVEN
args DockerAgent.defaultMavenArgs
reuseNode true
}
}
steps {
cmMaven(cmd: 'versions:set',
mavenParams: ['newVersion' : "${releaseNextDevelopmentVersion}",
'generateBackupPoms': 'false',
'processAllModules' : 'true'],
scanMvnLog: true,
)
}
}
stage('Git Push next SNAPSHOT') {
when {
expression { currentBuild.resultIsBetterOrEqualTo('SUCCESS') }
beforeAgent true
}
steps {
sh(label: 'Commit next dev version and push to remote', returnStdout: true, script: """#!/usr/bin/env bash
set -euo pipefail
git ls-files --modified | xargs -n1 git add
git commit --message="Set next development version ${releaseNextDevelopmentVersion}"
git push origin HEAD:refs/heads/${params.BRANCH}
""")
}
}
}
}
}
post {
always {
script {
if (isDefaultBuild) {
cmSetGitCommitStatus(context: GITHUB_COMMIT_STATUS_CONTEXT, repository: PROJECT_NAME)
}
}
cmsCleanup()
}
}
}