forked from BetterCloud/scim2
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jenkinsfile
283 lines (252 loc) · 11.4 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
#!/usr/bin/env groovy
/**
* Build pipeline for a TPSVC service.
*/
def slackChannel = 'tpsvc-notifications'
def slackMonitoringDev = (env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'master') ? 'tpsvc-monitoring-dev' : 'tpsvc-notifications'
def decodedJobName = env.JOB_NAME.replaceAll("%2F", "/")
def releaseBranchPrefix = 'do-release-*'
def artifactoryCredentials = usernamePassword(
credentialsId: 'artifactory-datapwn-credentials',
passwordVariable: 'ARTIFACTORY_TALEND_PASSWORD',
usernameVariable: 'ARTIFACTORY_TALEND_LOGIN')
def nexusCredentials = usernamePassword(
credentialsId: 'nexus-artifact-zl-credentials',
passwordVariable: 'NEXUS_TALEND_PASSWORD',
usernameVariable: 'NEXUS_TALEND_USER')
def gradlew(String... args) {
sh "./gradlew -s --max-workers=2 --no-daemon ${args.join(' ')}"
}
def hasChangesIn(String module) {
// If there is no change target, we consider that the module has changed
if (env.CHANGE_TARGET == null) {
return true;
}
// Get last commit hash from origin/{TARGET}
def TARGET = sh(
returnStdout: true,
script: "git rev-parse origin/${env.CHANGE_TARGET}"
).trim()
// Get commit hash from HEAD commit
def HEAD = sh(
returnStdout: true,
script: "git rev-parse HEAD"
).trim()
// Get diff between CHANGE_SET and HEAD commits
// Return true if a diff matches with module given as parameter
return sh(
returnStatus: true,
script: "git diff --name-only ${TARGET}...${HEAD} | grep ^${module}/"
) == 0
}
static String getServiceName(String jobName) {
// expected format: platform-services/<repo>/<branch>
final String[] jobNameExploded = jobName.split('/')
if (jobNameExploded == null || jobNameExploded.length != 3) {
throw new RuntimeException("Unexpected job name format. Expected two '/' chars, actual string: ${jobName}")
}
final String repoName = jobNameExploded[1]
final String gitRepoPrefix = 'platform-services-'
return repoName.startsWith(gitRepoPrefix) ? repoName.substring(gitRepoPrefix.length()) : repoName
}
/**
* Get Release Scope
*/
static String getScopeFromBranchName(String branchName) {
def scope
if (branchName.contains('minor')) {
scope = 'minor'
} else if (branchName.contains('major')) {
scope = 'major'
} else {
scope = 'patch'
}
return scope
}
pipeline {
agent {
kubernetes {
label "${getServiceName(decodedJobName)}-${UUID.randomUUID().toString()}".take(53)
yamlFile 'builderPodTemplate.yaml'
defaultContainer 'talend-tsbi-springboot-builder'
}
}
environment {
NEXUS_TPSVC_URL = 'https://artifacts-zl.talend.com/nexus/content/repositories/tpsvc'
NEXUS_TALEND_SNAPSHOTS_URL = 'https://artifacts-zl.talend.com/nexus/content/repositories/snapshots'
NEXUS_TALEND_RELEASES_URL = 'https://artifacts-zl.talend.com/nexus/content/repositories/releases'
NEXUS_TALEND_OS_RELEASE_URL = 'https://artifacts-zl.talend.com/nexus/content/repositories/TalendOpenSourceRelease'
NEXUS_TALEND_OPEN_SOURCE_RELEASE_URL = 'https://artifacts-zl.talend.com/nexus/content/repositories/TalendOpenSourceRelease'
NEXUS_TALEND_OPEN_SOURCE_SNAPSHOT_URL = 'https://artifacts-zl.talend.com/nexus/content/repositories/TalendOpenSourceSnapshot'
PROJECT_TEAM = 'platform-services'
GIT_USER = 'build-talend-tpsvc'
GIT_EMAIL = 'build-talend-tpsvc@talend.com'
GIT_CREDS = credentials('github-credentials')
ARTIFACTORY_TALEND = 'artifactory.datapwn.com/tlnd-docker-dev'
BUILD_PROFILE = 'prod'
BUILD_ENV = 'ci'
DOCKER_REGISTRY = 'artifactory.datapwn.com/tlnd-docker-dev/talend/platform-services'
DOCKER_IMAGE_NAME_FILE = "docker.image.name.yaml"
}
options {
buildDiscarder(logRotator(artifactNumToKeepStr: '5', numToKeepStr: env.BRANCH_NAME == 'develop' ? '10' : '2'))
timeout(time: 60, unit: 'MINUTES')
skipStagesAfterUnstable()
disableConcurrentBuilds()
}
parameters {
booleanParam(name: 'PUBLISH_SNAPSHOT', defaultValue: false,
description: 'Do you want to publish a snapshot ?')
}
stages {
stage('Announce Build Start') {
steps {
slackSend(color: '#0000AA', channel: "${slackChannel}", message: "STARTED: `${decodedJobName}` #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)")
}
}
stage('Prepare Git') {
steps {
sh '''
git version
git config --global --add safe.directory "${WORKSPACE}"
'''
}
}
stage('Set Gradle options') {
steps {
withCredentials([nexusCredentials]) {
script {
// inject required credentials (then we don't need the gradle settings file)
env.ORG_GRADLE_PROJECT_nexusUsername = "${NEXUS_TALEND_USER}"
env.ORG_GRADLE_PROJECT_nexusPassword = "${NEXUS_TALEND_PASSWORD}"
env.ORG_GRADLE_PROJECT_nexusUrl = env.NEXUS_TPSVC_URL
env.ORG_GRADLE_PROJECT_nexusTalendSnapshotsUsername = "${NEXUS_TALEND_USER}"
env.ORG_GRADLE_PROJECT_nexusTalendSnapshotsPassword = "${NEXUS_TALEND_PASSWORD}"
env.ORG_GRADLE_PROJECT_nexusTalendSnapshotsUrl = env.NEXUS_TALEND_SNAPSHOTS_URL
env.ORG_GRADLE_PROJECT_nexusTalendReleasesUsername = "${NEXUS_TALEND_USER}"
env.ORG_GRADLE_PROJECT_nexusTalendReleasesPassword = "${NEXUS_TALEND_PASSWORD}"
env.ORG_GRADLE_PROJECT_nexusTalendReleasesUrl = env.NEXUS_TALEND_RELEASES_URL
env.ORG_GRADLE_PROJECT_nexusTalendOpenSourceReleaseUsername = "${NEXUS_TALEND_USER}"
env.ORG_GRADLE_PROJECT_nexusTalendOpenSourceReleasePassword = "${NEXUS_TALEND_PASSWORD}"
env.ORG_GRADLE_PROJECT_nexusTalendOSReleasesUrl = env.NEXUS_TALEND_OS_RELEASE_URL
env.ORG_GRADLE_PROJECT_nexusTalendOpenSourceReleaseUrl = env.NEXUS_TALEND_OPEN_SOURCE_RELEASE_URL
env.ORG_GRADLE_PROJECT_nexusTalendOpenSourceSnapshotUrl = env.NEXUS_TALEND_OPEN_SOURCE_SNAPSHOT_URL
env.RECKON_OPTIONS = '-Preckon.scope=patch'
}
}
}
}
stage("Pre-Release") {
when {
branch releaseBranchPrefix
}
steps {
script {
env.RELEASE_SCOPE = getScopeFromBranchName(BRANCH_NAME)
echo "Scope of the release is: ${RELEASE_SCOPE}"
env.RECKON_OPTIONS = "-Preckon.stage=final -Preckon.scope=${RELEASE_SCOPE}"
}
sh '''
set +x
echo https://$GIT_CREDS_USR:$GIT_CREDS_PSW@github.com > /tmp/.git-credentials
git checkout $BRANCH_NAME
git config credential.helper 'store --file /tmp/.git-credentials\'
git config user.name "$GIT_USER"
git config user.email "$GIT_EMAIL"
git clean -fdxn
git fetch --tags
git status
'''
}
}
stage("Pre-Build") {
steps {
gradlew("versionInformation", env.RECKON_OPTIONS)
script {
env.ARTIFACT_VERSION = readFile "version.txt"
if (BRANCH_NAME =~ /(PR-.*)/) {
env.IMAGE_VERSION = "${ARTIFACT_VERSION}-${BRANCH_NAME}"
} else {
env.IMAGE_VERSION = env.ARTIFACT_VERSION
}
env.IMAGE_VERSION_WITH_TIMESTAMP = "${IMAGE_VERSION}-${BUILD_TIMESTAMP}"
echo "Project Version is ${ARTIFACT_VERSION} - docker image version is ${IMAGE_VERSION}"
}
sh '''
rm -f version.txt
'''
}
}
stage('Build') {
steps {
gradlew("clean", "build", env.RECKON_OPTIONS)
}
}
stage("Sonarqube analysis") {
steps {
withCredentials([usernamePassword(credentialsId: 'sonar-credentials',
passwordVariable: 'SONAR_PASSWORD', usernameVariable: 'SONAR_LOGIN')]) {
gradlew("sonar",
"-x test",
"-Dsonar.login='$SONAR_LOGIN'",
"-Dsonar.password='$SONAR_PASSWORD'",
"-Dsonar.projectVersion='$IMAGE_VERSION'",
"-Dsonar.analysisCache.enabled=false",
"-Dsonar.host.url=https://sonar.datapwn.com",
"-Dsonar.projectKey=${PROJECT_TEAM}:${getServiceName(decodedJobName)}",
"-Dsonar.projectName=TPSVC_${getServiceName(decodedJobName)}"
, env.RECKON_OPTIONS)
}
}
}
stage('Release') {
when {
anyOf {
branch releaseBranchPrefix
}
}
environment {
GRGIT_USER="$GIT_CREDS_USR"
GRGIT_PASS="$GIT_CREDS_PSW"
}
steps {
gradlew("publish", "reckonTagPush", env.RECKON_OPTIONS)
sh "git push --delete origin ${BRANCH_NAME}"
}
}
stage('Publish PR Build') {
when { branch "PR-*" }
environment {
GRGIT_USER="$GIT_CREDS_USR"
GRGIT_PASS="$GIT_CREDS_PSW"
}
steps {
gradlew("publish", env.RECKON_OPTIONS)
}
}
}
post {
always {
archiveArtifacts artifacts: '*/build/reports/',
fingerprint: true, onlyIfSuccessful: true, allowEmptyArchive: true
archiveArtifacts artifacts: '*/build/reports-test/',
fingerprint: true, onlyIfSuccessful: true, allowEmptyArchive: true
junit testResults: '*/build/reports-test/*/*.xml', allowEmptyResults: true
}
success {
slackSend (color: 'good', channel: "${slackChannel}", message: "SUCCESSFUL: `${decodedJobName}` #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)\nDuration: ${currentBuild.durationString}")
}
unstable {
slackSend (color: 'warning', channel: "${slackChannel}", message: "UNSTABLE: `${decodedJobName}` #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)")
}
failure {
slackSend (color: '#AA0000', channel: "${slackMonitoringDev}", message: "FAILED: `${decodedJobName}` #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)")
}
aborted {
slackSend (color: 'warning', channel: "${slackChannel}", message: "ABORTED: `${decodedJobName}` #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)")
}
cleanup {
sh 'docker logout ${ARTIFACTORY_TALEND}'
}
}
}