-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
235 lines (222 loc) · 9.39 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
// # Licensed to the Apache Software Foundation (ASF) under one or more
// # contributor license agreements. See the NOTICE file distributed with
// # this work for additional information regarding copyright ownership.
// # The ASF licenses this file to You under the Apache License, Version 2.0
// # (the "License"); you may not use this file except in compliance with
// # the License. You may obtain a copy of the License at
// #
// # http://www.apache.org/licenses/LICENSE-2.0
// #
// # Unless required by applicable law or agreed to in writing, software
// # distributed under the License is distributed on an "AS IS" BASIS,
// # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// # See the License for the specific language governing permissions and
// # limitations under the License.
pipeline {
agent {
docker {
label 'memphis-jenkins-big-fleet,'
image 'gradle:7.3.0'
args '-u root'
}
}
environment {
HOME = '/tmp'
TOKEN = credentials('maven-central-token')
GPG_PASSPHRASE = credentials('gpg-key-passphrase')
SLACK_CHANNEL = '#jenkins-events'
}
stages {
stage('Alpha Release') {
when {
branch '*-alpha'
}
steps {
script {
def version = readFile('version-alpha.conf').trim()
env.versionTag = version
echo "Using version from version-alpha.conf: ${env.versionTag}"
setupGPG()
publishClients()
uploadBundleAndCheckStatus()
}
}
}
stage('Beta Release') {
when {
branch '*-beta'
}
steps {
script {
sh 'git config --global --add safe.directory $(pwd)'
env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=%an', returnStdout: true).trim()
env.COMMIT_MESSAGE = sh(script: 'git log -1 --pretty=%B', returnStdout: true).trim()
def triggerCause = currentBuild.getBuildCauses().find { it._class == 'hudson.model.Cause$UserIdCause' }
env.TRIGGERED_BY = triggerCause ? triggerCause.userId : 'Commit'
}
script {
def version = readFile('version-beta.conf').trim()
env.versionTag = version
echo "Using version from version-beta.conf: ${env.versionTag}"
setupGPG()
publishClients()
uploadBundleAndCheckStatus()
}
}
}
stage('Prod Release') {
when {
branch '3.5.1'
}
steps {
script {
def version = readFile('version.conf').trim()
env.versionTag = version
echo "Using version from version.conf: ${env.versionTag}"
setupGPG()
publishClients()
uploadBundleAndCheckStatus()
}
}
}
stage('Create Release'){
when {
branch '3.5.1'
}
steps {
sh """
curl -L https://github.com/cli/cli/releases/download/v2.40.0/gh_2.40.0_linux_amd64.tar.gz -o gh.tar.gz
tar -xvf gh.tar.gz
mv gh_2.40.0_linux_amd64/bin/gh /usr/local/bin
rm -rf gh_2.40.0_linux_amd64 gh.tar.gz
"""
withCredentials([sshUserPrivateKey(keyFileVariable:'check',credentialsId: 'main-github')]) {
sh """
GIT_SSH_COMMAND='ssh -i $check -o StrictHostKeyChecking=no' git config --global user.email "[email protected]"
GIT_SSH_COMMAND='ssh -i $check -o StrictHostKeyChecking=no' git config --global user.name "Jenkins"
GIT_SSH_COMMAND='ssh -i $check -o StrictHostKeyChecking=no' git tag -a $versionTag -m "$versionTag"
GIT_SSH_COMMAND='ssh -i $check -o StrictHostKeyChecking=no' git push origin $versionTag
"""
}
withCredentials([string(credentialsId: 'gh_token', variable: 'GH_TOKEN')]) {
sh """
gh release create $versionTag /tmp/kafka-clients/kafka-client-${env.versionTag}.tar.gz --generate-notes
"""
}
}
}
}
post {
always {
cleanWs()
}
success {
script {
if (env.BRANCH_NAME == '3.5.1') {
sendSlackNotification('SUCCESS')
}
}
}
failure {
script {
if (env.BRANCH_NAME == '3.5.1') {
sendSlackNotification('FAILURE')
}
}
}
aborted {
script {
if (env.BRANCH_NAME == '3.5.1') {
sendSlackNotification('ABORTED')
}
// Get the build log to check for the specific exception
def buildLog = currentBuild.rawBuild.getLog(50)
// Log the build log for debugging purposes (you can remove this once confirmed)
echo "Build Log:\n${buildLog.join('\n')}"
// Check if the log contains the specific exception using a regular expression
if (buildLog.find { it =~ /org\.jenkinsci\.plugins\.workflow\.support\.steps\.AgentOfflineException/ }) {
echo 'AgentOfflineException found, retrying the build...'
// Check if the build has parameters and rerun the job accordingly
def paramsList = currentBuild.rawBuild.getAction(hudson.model.ParametersAction)?.parameters
if (paramsList) {
build(job: env.JOB_NAME, parameters: paramsList)
} else {
echo 'No parameters found, rerunning without parameters'
build(job: env.JOB_NAME)
}
} else {
echo 'Abort not related to AgentOfflineException, not retrying.'
}
}
}
}
}
// Function to setup GPG
def setupGPG() {
withCredentials([file(credentialsId: 'gpg-key', variable: 'GPG_KEY')]) {
sh """
apt update
apt install -y gnupg
"""
sh """
echo '${env.GPG_PASSPHRASE}' | gpg --batch --yes --passphrase-fd 0 --import $GPG_KEY
echo "allow-loopback-pinentry" > ~/.gnupg/gpg-agent.conf
echo RELOADAGENT | gpg-connect-agent
echo "D64C041FB68170463BE78AD7C4E3F1A8A5F0A659:6:" | gpg --import-ownertrust
gpg --batch --pinentry-mode loopback --passphrase '${env.GPG_PASSPHRASE}' --export-secret-keys -o clients/secring.gpg
"""
}
}
// Function to publish clients using Gradle
def publishClients() {
sh "./gradlew :clients:publish -Pversion=${env.versionTag} -Psigning.password=${env.GPG_PASSPHRASE}"
sh "rm /tmp/kafka-clients/ai/superstream/kafka-clients/maven-metadata.xml*"
}
// Function to upload a bundle and check deployment status
def uploadBundleAndCheckStatus() {
def response = sh(script: """
cd /tmp/kafka-clients
tar czvf kafka-client-${env.versionTag}.tar.gz ai
curl --request POST \\
--verbose \\
--header 'Authorization: Bearer ${env.TOKEN}' \\
--form bundle=@kafka-client-${env.versionTag}.tar.gz \\
'https://central.sonatype.com/api/v1/publisher/upload?name=kafka-clients-${env.versionTag}&publishingType=AUTOMATIC'
""", returnStdout: true).trim()
def id = response.split("\n").last().trim()
echo "Extracted ID: ${id}"
sleep(10)
def output = sh(script: """
curl --request POST \\
--verbose \\
--header 'Authorization: Bearer ${env.TOKEN}' \\
'https://central.sonatype.com/api/v1/publisher/status?id=${id}'
""", returnStdout: true).trim()
echo "Curl Output: ${output}"
if (output.contains('FAILED')) {
error "Deployment FAILED. Exiting with error."
} else {
echo "Deployment is successful."
}
}
// SlackSend Function
def sendSlackNotification(String jobResult) {
def jobUrl = env.BUILD_URL
def messageDetail = env.COMMIT_MESSAGE ? "Commit/PR by @${env.GIT_AUTHOR}:\n${env.COMMIT_MESSAGE}" : "No commit message available."
def projectName = env.JOB_NAME
// Define the color based on the job result
def color = jobResult == 'SUCCESS' ? 'good' : (jobResult == 'ABORTED' ? '#808080' : 'danger')
slackSend (
channel: "${env.SLACK_CHANNEL}",
color: color,
message: """\
*:rocket: Jenkins Build Notification :rocket:*
*Project:* `${projectName}`
*Build Number:* `#${env.BUILD_NUMBER}`
*Status:* ${jobResult == 'SUCCESS' ? ':white_check_mark: *Success*' : (jobResult == 'ABORTED' ? ':warning: *Aborted*' : ':x: *Failure*')}
:information_source: ${messageDetail}
Triggered by: ${env.TRIGGERED_BY}
:link: *Build URL:* <${jobUrl}|View Build Details>
"""
)
}