-
Notifications
You must be signed in to change notification settings - Fork 6
/
Jenkinsfile-release
196 lines (167 loc) · 7.17 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
#!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"
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}
"""
}
}
}
stage ('Build Release Version'){
steps {
configFileProvider([configFile(fileId: 'settings.xml', variable: 'MAVEN_SETTINGS')]) {
sh """
mvn --version
mvn clean install \
-s ${MAVEN_SETTINGS} \
--batch-mode \
-e \
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
-PcodeQuality,release-enforcement
"""
}
}
}
stage ('Commit & Tag'){
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}
"""
}
}
}
stage ('Commit & Push'){
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")
}
}
}
}
}
}
}