-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJenkinsfile-release
263 lines (230 loc) · 9.4 KB
/
Jenkinsfile-release
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
#!groovy
@Library("titan-library") _
pipeline {
agent any
tools {
jdk "java-21"
maven 'default'
git 'git'
}
environment {
TRUNK_BRANCH_NAME = 'main'
REPO_NAME = "${GIT_URL.split('/')[4].split('\\.')[0]}"
BRANCH_NAME = "${GIT_BRANCH.startsWith('origin/') ? GIT_BRANCH['origin/'.length()..-1] : GIT_BRANCH}"
GITLAB_OWNER = "${GIT_URL.split('/')[3]}"
GITLAB_REPO = "https://gitlab.tinkarbuild.com/${GITLAB_OWNER}/${REPO_NAME}.git"
GITLAB_RELEASE_API = "https://gitlab.tinkarbuild.com/api/v4/projects/${GITLAB_OWNER}%2F${REPO_NAME}/releases"
GITLAB_CREDS_ID = 'vault-gitlab-user-pat'
}
options {
buildDiscarder logRotator(
numToKeepStr: '10'
)
// Console debug options
timestamps()
ansiColor('xterm')
}
stages {
stage('Initialize') {
steps {
// Clean before checkout & build
cleanWs()
checkout scm
script {
trunkBranchName = "main"
BRANCH_NAME = "${GIT_BRANCH.startsWith('origin/') ? GIT_BRANCH['origin/'.length()..-1] : GIT_BRANCH}"
pomModel = readMavenPom(file: 'pom.xml')
pomVersion = pomModel.getVersion()
isSnapshot = pomVersion.contains("-SNAPSHOT")
echo "pomVersion: ${pomVersion}"
if(!isSnapshot) {
echo "ERROR: Version is set to incompatible version '${pomVersion}'. Only SNAPSHOT development versions can be converted to a release version."
fail()
}
if(BRANCH_NAME != trunkBranchName) {
echo "ERROR: Attempting to release from branch ${BRANCH_NAME}. Release from ${trunkBranchName} branch only..."
fail()
}
releaseVersion = pomVersion.split("-")[0]
nextDevVersion = semanticVersion.getIncrementedVersionString(releaseVersion) + "-SNAPSHOT"
echo "releaseVersion: ${releaseVersion}"
echo "nextDevVersion: ${nextDevVersion}"
echo "GIT_BRANCH: ${GIT_BRANCH}"
echo "BRANCH_NAME: ${BRANCH_NAME}"
echo "${currentBuild.buildCauses}"
}
}
}
stage ('Set Release Version'){
steps {
configFileProvider([configFile(fileId: 'settings.xml', variable: 'MAVEN_SETTINGS')]) {
sh """
mvn versions:set \
-s ${MAVEN_SETTINGS} \
-DgenerateBackupPoms=false \
-DnewVersion=${releaseVersion} \
-Dmaven.build.cache.enabled=false
"""
}
}
}
stage("Build ProtoC Image") {
steps {
script {
docker.build("tinkar-schema-protoc:latest", "-f protoc.dockerfile")
}
}
}
stage("Build CSharp Image") {
steps {
script {
docker.build("tinkar-schema-csharp:latest", "-f csharp.dockerfile")
}
}
}
// Running protoc to generate Java generated classes.
stage("Build Java Code") {
agent {
docker {
image 'tinkar-schema-protoc:latest'
reuseNode true
args '-u root:root'
}
}
steps {
sh '''
mkdir -p $(pwd)/src/main/java-generated
protoc -I $(pwd) $(pwd)/Tinkar.proto \
--java_out=$(pwd)/src/main/java-generated
pwd
ls -R /home/proto-builder/
ls -R src/
'''
stash(name: "java-schema-proto", allowEmpty: false, useDefaultExcludes: false, includes: 'src/**')
}
}
// Running protoc to generate C# generated classes.
stage("Build CSharp Code") {
agent {
docker {
image 'tinkar-schema-protoc:latest'
reuseNode true
args '-u root:root'
}
}
steps {
sh '''
mkdir -p $(pwd)/src/main/csharp-generated
protoc -I $(pwd) $(pwd)/Tinkar.proto \
--csharp_out=$(pwd)/src/main/csharp-generated
'''
stash(name: "csharp-schema-proto", includes: 'src/**')
}
}
stage ('Test Release Build'){
steps {
configFileProvider([configFile(fileId: 'settings.xml', variable: 'MAVEN_SETTINGS')]) {
sh """
mvn --version
mvn clean install \
-s ${MAVEN_SETTINGS} \
-P codeQuality,release-enforcement \
--batch-mode \
-e \
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
-Dmaven.build.cache.enabled=false
"""
}
}
}
stage ('Commit & Tag'){
tools {
git 'git'
}
steps {
withCredentials([gitUsernamePassword(credentialsId: GITLAB_CREDS_ID, gitToolName: 'git')]) {
sh """
git add .
git config user.name jenkins
git commit -m 'Release ${releaseVersion}'
git tag -a ${releaseVersion} -m '${releaseVersion}' --force
"""
}
}
}
stage ('Set next dev version'){
steps {
configFileProvider([configFile(fileId: 'settings.xml', variable: 'MAVEN_SETTINGS')]) {
sh """
mvn versions:set \
-s ${MAVEN_SETTINGS} \
-DgenerateBackupPoms=false \
-DnewVersion=${nextDevVersion} \
-Dmaven.build.cache.enabled=false
"""
}
}
}
stage ('Commit & Push'){
tools {
git 'git'
}
steps {
withCredentials([gitUsernamePassword(credentialsId: GITLAB_CREDS_ID, gitToolName: 'git')]) {
sh """
git add .
git config user.name jenkins
git commit -m 'Set next dev version to ${nextDevVersion}'
git push origin HEAD:${BRANCH_NAME}
git push --tags origin
"""
}
}
}
stage ('GitLab Release'){
tools {
git 'git'
}
steps {
script {
withCredentials([usernamePassword(credentialsId: GITLAB_CREDS_ID, passwordVariable: 'token', usernameVariable: 'user')]) {
echo "GitLab User ${user}"
def releaseByTagUrl = GITLAB_RELEASE_API + "/" + releaseVersion
echo "GitLab API Release by Tag URL: ${releaseByTagUrl}"
def response = sh(
script: """
curl -L \
-H "PRIVATE-TOKEN: ${token}" \
${releaseByTagUrl}
""",
returnStdout: true
).trim()
echo "Done request"
def jsonResponse = readJSON text: response
echo "${jsonResponse}"
if (jsonResponse['message'] && jsonResponse['message'].contains('Not Found')) {
echo "This release does not exist yet"
def data = "{\"name\": \"Release ${releaseVersion}\",\"tag_name\": \"${releaseVersion}\", \"description\": \"Release ${releaseVersion} from tag ${releaseVersion}\" }"
def postResponse = sh(
script: """
curl -L \
-H "PRIVATE-TOKEN: ${token}" \
-H "Content-Type: application/json" \
-X POST \
--data '${data}' \
${GITLAB_RELEASE_API}
""",
returnStdout: true
).trim()
echo "Done attempting to create Release"
def jsonPostResponse = readJSON text: postResponse
echo "${jsonPostResponse}"
} else {
error("Unexpected result")
}
}
}
}
}
}
}