-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile.bak
More file actions
136 lines (112 loc) · 4.46 KB
/
Jenkinsfile.bak
File metadata and controls
136 lines (112 loc) · 4.46 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
pipeline {
agent any
environment {
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 = "sivaprakash123/python_gmail_smtp"
}
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('Add Gerrit Change-Id if missing') {
when { changeRequest() }
steps {
script {
echo "➡️ Checking Change-Id..."
// Detect 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"
}
}
stage('Check Gerrit Vote') {
when { changeRequest() }
steps {
script {
echo "🔍 Checking Gerrit votes..."
def votesJson = sh(
script: """
ssh -p ${GERRIT_PORT} -i ${GERRIT_KEY} ${GERRIT_USER}@${GERRIT_HOST} \
"gerrit query --format=JSON change:${CHANGE_ID} --current-patch-set"
""",
returnStdout: true
).trim()
echo "📄 Gerrit Response: ${votesJson}"
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 vote yet"
echo "✅ Gerrit Vote = ${vote}"
if (vote.toInteger() < 1) {
error "❌ Gerrit review failed (vote=${vote})"
}
}
}
}
stage('Auto 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"
}
}
}
}
}