-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
150 lines (133 loc) · 4.92 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
pipeline {
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
spec:
containers:
- name: python
image: python:3.9-slim
command:
- sh
- -c
- |
apt-get update && apt-get install -y git tzdata jq
cp /usr/share/zoneinfo/Asia/Jakarta /etc/localtime
echo "Asia/Jakarta" > /etc/timezone
git config --global --add safe.directory /home/jenkins/agent/workspace/ongky_test
exec cat
tty: true
env:
- name: TZ
value: "Asia/Jakarta"
"""
}
}
parameters {
string(name: 'JIRA_URL', description: 'Enter the JIRA URL')
choice(name: 'KAFKA_CLUSTER', choices: ['kafka-cluster-platform', 'kafka-cluster-data'], description: 'Select the Kafka cluster')
}
stages {
stage('Auto Approve Scripts') {
steps {
script {
// Trigger the AutoApproveJob
build job: 'AutoApproveJob', wait: true
}
}
}
stage('Clone Repository') {
steps {
container('python') {
deleteDir() // Ensure the workspace is clean
sh 'git clone https://github.com/ongkyoktafian1/kafka-automate.git .'
}
}
}
stage('Install Dependencies') {
steps {
container('python') {
sh 'pip install kafka-python'
}
}
}
stage('Add Git Exception') {
steps {
container('python') {
sh 'git config --global --add safe.directory /home/jenkins/agent/workspace/ongky_test'
}
}
}
stage('Extract JIRA Key') {
steps {
container('python') {
script {
// Extract the JIRA key from the URL
def jiraKey = params.JIRA_URL.tokenize('/').last()
env.JIRA_KEY = jiraKey
}
}
}
}
stage('Create Kafka Producer Script') {
steps {
container('python') {
// Create the Python script
writeFile file: 'kafka_producer.py', text: """
from kafka import KafkaProducer
import json
import sys
topic = sys.argv[1]
messages = json.loads(sys.argv[2])
brokers = sys.argv[3].split(',')
producer = KafkaProducer(
bootstrap_servers=brokers, # brokers is a list of broker addresses
value_serializer=lambda v: json.dumps(v).encode('utf-8')
)
for message in messages:
producer.send(topic, value=message)
producer.flush()
"""
}
}
}
stage('Publish to Kafka') {
steps {
container('python') {
script {
def kafkaCluster = params.KAFKA_CLUSTER
def jiraKey = env.JIRA_KEY
def jsonDirectory = "${env.WORKSPACE}/${kafkaCluster}/${jiraKey}"
def jsonFilePattern = "${jsonDirectory}/*.json"
// Find all JSON files in the specified directory
def jsonFiles = sh(script: "ls ${jsonFilePattern}", returnStdout: true).trim().split('\n')
jsonFiles.each { jsonFile ->
if (fileExists(jsonFile)) {
// Read and convert the JSON file using jq
def messagesJson = sh(script: "jq -c .messages < ${jsonFile}", returnStdout: true).trim()
def topic = sh(script: "jq -r .topic < ${jsonFile}", returnStdout: true).trim()
// Write the JSON string to the messages.json file
writeFile file: 'messages.json', text: messagesJson
// Determine the Kafka brokers based on the selected Kafka cluster
def kafkaBrokers = sh(script: "cat kafka-config/${kafkaCluster} | tr '\\n' ',' | sed 's/,\$//'", returnStdout: true).trim()
// Run the Python script
sh "python kafka_producer.py ${topic} \"${messagesJson.replace('"', '\\"')}\" ${kafkaBrokers}"
} else {
error "File not found: ${jsonFile}"
}
}
}
}
}
}
}
post {
success {
echo 'Messages published successfully!'
}
failure {
echo 'Failed to publish messages.'
}
}
}