-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile.diff
More file actions
192 lines (163 loc) · 6.79 KB
/
Jenkinsfile.diff
File metadata and controls
192 lines (163 loc) · 6.79 KB
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
pipeline {
agent any
environment {
// Gerrit settings
GERRIT_USER = "jenkins"
GERRIT_HOST = "10.175.2.49"
GERRIT_PORT = "29418"
TARGET_BRANCH = "master"
GERRIT_KEY = "/var/lib/jenkins/.ssh/gerrit_jenkins"
// GitHub repo details
GITHUB_REPO = "sivaprakash123/python_gmail_smtp"
// SonarQube instance (configured in Jenkins global config)
SONARQUBE_ENV = "sonarqube-server"
// Jira URL base (replace with your real URL)
JIRA_BASE = "https://karmayogibharat.atlassian.net"
}
stages {
stage('Detect PR') {
when { changeRequest() }
steps {
echo "✅ GitHub PR detected: ID=${env.CHANGE_ID}, Branch=${env.CHANGE_BRANCH}"
}
}
stage('Fetch PR Code') {
when { changeRequest() }
steps {
sh """
git fetch origin pull/${CHANGE_ID}/head:pr-${CHANGE_ID}
git checkout pr-${CHANGE_ID}
"""
}
}
stage('SonarQube Code Quality Check (Mock Pass)') {
when { changeRequest() }
steps {
script {
echo "🧠 [TEST MODE] Simulating SonarQube analysis..."
// Simulate Sonar scan (you can still run mvn if you want)
sh 'echo "Pretending SonarQube scan succeeded..."'
// Fake Quality Gate result as PASS
currentBuild.result = 'SUCCESS'
echo "✅ [TEST MODE] SonarQube Quality Gate passed (mocked)"
}
}
}
stage('Add Gerrit Change-Id if missing') {
when { changeRequest() }
steps {
script {
echo "➡️ Checking Change-Id..."
def changeId = sh(
script: "git log -1 | grep 'Change-Id:' || true",
returnStdout: true
).trim()
if (!changeId) {
echo "⚠️ No Change-Id found — generating one..."
sh """
git config user.name "Jenkins"
git config user.email "jenkins@local"
git commit --amend -m "\$(git log -1 --pretty=%B)\n\nChange-Id: I\$(uuidgen | tr -d '-')"
"""
} else {
echo "✅ Change-Id exists: ${changeId}"
}
}
}
}
stage('Push to Gerrit for Review') {
when { changeRequest() }
steps {
sh """
git remote add gerrit ssh://${GERRIT_USER}@${GERRIT_HOST}:${GERRIT_PORT}/${GITHUB_REPO}.git || true
GIT_SSH_COMMAND="ssh -i ${GERRIT_KEY} -o StrictHostKeyChecking=no" \
git push gerrit HEAD:refs/for/${TARGET_BRANCH}%topic=PR-${CHANGE_ID}
"""
echo "📤 Code pushed to Gerrit for review (topic=PR-${CHANGE_ID})"
}
}
// ---------- PATCHED STAGE ----------
stage('Check Gerrit Approval') {
when { changeRequest() }
steps {
script {
echo "🔍 Checking Gerrit votes for topic=PR-${CHANGE_ID}..."
def votesJson = sh(
script: """
ssh -p ${GERRIT_PORT} -i ${GERRIT_KEY} ${GERRIT_USER}@${GERRIT_HOST} \
"gerrit query --format=JSON topic:PR-${CHANGE_ID} --current-patch-set"
""",
returnStdout: true
).trim()
echo "📄 Gerrit Response: ${votesJson}"
def vote = sh(
script: """
echo '${votesJson}' | jq -r '
select(.currentPatchSet) |
.currentPatchSet.approvals[]? |
select(.type=="Code-Review") |
.value
' | sort -nr | head -1
""",
returnStdout: true
).trim()
if (!vote) {
error "❌ No Gerrit review yet for PR-${CHANGE_ID}"
}
echo "✅ Highest Gerrit Code-Review vote = ${vote}"
if (vote.toInteger() < 2) {
error "❌ Gerrit review not approved (vote=${vote})"
}
echo "🎉 Gerrit review approved for PR-${CHANGE_ID}"
}
}
}
// ----------------------------------
stage('Merge GitHub PR After Gerrit Approval') {
when { changeRequest() }
steps {
script {
echo "🔁 Gerrit approved — merging GitHub PR"
withCredentials([string(credentialsId: 'gerrit-github-token', variable: 'GITHUB_TOKEN')]) {
sh """
curl -X PUT \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-d '{ "merge_method": "squash" }' \
https://api.github.com/repos/${GITHUB_REPO}/pulls/${CHANGE_ID}/merge
"""
}
echo "🎉 GitHub PR #${CHANGE_ID} merged successfully"
}
}
}
// ---------- NEW ----------
stage('Update Jira Ticket') {
when { changeRequest() }
steps {
script {
def jiraKey = sh(
script: "git log -1 --pretty=%B | grep -oE '[A-Z]+-[0-9]+' || true",
returnStdout: true
).trim()
if (jiraKey) {
echo "📎 Found Jira ticket: ${jiraKey}"
withCredentials([string(credentialsId: 'jira-api-token', variable: 'JIRA_TOKEN')]) {
sh """
curl -X POST \
-H "Authorization: Basic ${JIRA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{ "transition": { "id": "31" } }' \
${JIRA_BASE}/rest/api/3/issue/${jiraKey}/transitions
"""
}
echo "🎯 Jira issue ${jiraKey} updated successfully."
} else {
echo "⚠️ No Jira key found in commit message."
}
}
}
}
// --------------------------
}
}