-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
192 lines (183 loc) · 8.64 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
pipeline {
agent {
docker {
label 'memphis-jenkins-big-fleet,'
image 'python:3.11.9-bullseye'
args '-u root'
}
}
environment {
HOME = '/tmp'
TOKEN = credentials('maven-central-token')
GPG_PASSPHRASE = credentials('gpg-key-passphrase')
SLACK_CHANNEL = '#jenkins-events'
}
stages {
stage('Prepare Environment') {
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'
}
sh """
export DEBIAN_FRONTEND=noninteractive
apt update -y
apt install -y wget software-properties-common lsb-release gcc make python3 python3-pip python3-dev libsasl2-modules-gssapi-mit krb5-user
wget -qO - https://packages.confluent.io/deb/7.0/archive.key | apt-key add -
add-apt-repository "deb https://packages.confluent.io/clients/deb \$(lsb_release -cs) main"
apt update
apt install -y librdkafka-dev
pip install --user pdm
pip install requests
"""
}
}
stage('Beta Release') {
when {
branch '*-beta'
}
steps {
script {
def version = readFile('version-beta.conf').trim()
env.versionTag = version
echo "Using version from version-beta.conf: ${env.versionTag}"
}
sh """
sed -i -r "s/superstream-confluent-kafka/superstream-confluent-kafka-beta/g" pyproject.toml
"""
sh "sed -i \'s/version = \"[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+\"/version = \"${env.versionTag}\"/g\' pyproject.toml"
sh """
C_INCLUDE_PATH=/usr/include/librdkafka LIBRARY_PATH=/usr/include/librdkafka /tmp/.local/bin/pdm build
"""
withCredentials([usernamePassword(credentialsId: 'superstream-pypi', usernameVariable: 'USR', passwordVariable: 'PSW')]) {
sh """
python3 patch/patch.py --src "dist/superstream_confluent_kafka_beta-${env.versionTag}-cp311-cp311-linux_x86_64.whl" --output "dist/" --prefix "superstream_confluent_kafka_beta-${env.versionTag}"
"""
sh"""
rm dist/superstream_confluent_kafka_beta-${env.versionTag}-cp311-cp311-linux_x86_64.whl
/tmp/.local/bin/pdm publish --no-build --username $USR --password $PSW
"""
}
}
}
stage('Prod Release') {
when {
branch '2.4.0'
}
steps {
script {
def version = readFile('version.conf').trim()
env.versionTag = version
echo "Using version from version.conf: ${env.versionTag}"
}
sh "sed -i \'s/version = \"[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+\"/version = \"${env.versionTag}\"/g\' pyproject.toml"
sh """
C_INCLUDE_PATH=/usr/include/librdkafka LIBRARY_PATH=/usr/include/librdkafka /tmp/.local/bin/pdm build
"""
withCredentials([usernamePassword(credentialsId: 'superstream-pypi', usernameVariable: 'USR', passwordVariable: 'PSW')]) {
sh """
python3 patch/patch.py --src "dist/superstream_confluent_kafka-${env.versionTag}-cp311-cp311-linux_x86_64.whl" --output "dist/" --prefix "superstream_confluent_kafka-${env.versionTag}"
"""
sh"""
rm dist/superstream_confluent_kafka-${env.versionTag}-cp311-cp311-linux_x86_64.whl
/tmp/.local/bin/pdm publish --no-build --username $USR --password $PSW
"""
}
}
}
stage('Create Release'){
when {
branch '2.4.0'
}
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 "jenkins@memphis.dev"
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 dist/superstream_confluent_kafka-${env.versionTag}.tar.gz --generate-notes
"""
}
}
}
}
post {
always {
cleanWs()
}
success {
script {
if (env.BRANCH_NAME == '2.4.0') {
sendSlackNotification('SUCCESS')
}
}
}
failure {
script {
if (env.BRANCH_NAME == '2.4.0') {
sendSlackNotification('FAILURE')
}
}
}
aborted {
script {
if (env.BRANCH_NAME == '2.4.0') {
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.'
}
}
}
}
}
// 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>
"""
)
}