-
Notifications
You must be signed in to change notification settings - Fork 1
/
jenkin
258 lines (190 loc) · 5.78 KB
/
jenkin
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
pipeline {
agent any
parameters {
choice(name: 'ENVIRONMENT', choices: ['prod', 'staging', 'dev'], description: 'Choose an environment')
string(name: 'NAME', defaultValue: 'world', description: 'Enter a name')
}
stages {
stage('Run Python Script') {
steps {
sh "python myscript.py ${params.ENVIRONMENT} ${params.NAME}"
}
}
}
}
import sys
if len(sys.argv) != 3:
print('Usage: python myscript.py <environment> <name>')
sys.exit(1)
environment = sys.argv[1]
name = sys.argv[2]
if environment == 'prod':
print(f'Hello, {name}! This is the production environment.')
elif environment == 'staging':
print(f'Hello, {name}! This is the staging environment.')
elif environment == 'dev':
print(f'Hello, {name}! This is the development environment.')
else:
print('Invalid environment!')
# do something else based on the environment and name
----
pipeline {
agent any
parameters {
choice(name: 'MY_CHOICE', choices: ['Option 1', 'Option 2', 'Option 3'], description: 'Choose an option')
}
stages {
stage('Run Python Script') {
steps {
sh "python myscript.py ${params.MY_CHOICE}"
}
}
}
}
---
import sys
my_choice = sys.argv[1]
if my_choice == 'Option 1':
# do something
elif my_choice == 'Option 2':
# do something else
else:
# do something else
---
import sys
if len(sys.argv) != 2:
print('Usage: python myscript.py <fruit>')
sys.exit(1)
fruit = sys.argv[1]
if fruit == 'apple':
print('You chose an apple')
elif fruit == 'banana':
print('You chose a banana')
elif fruit == 'orange':
print('You chose an orange')
else:
print(f'Unknown fruit: {fruit}')
pipeline {
agent any
parameters {
choice(name: 'FRUIT', choices: ['apple', 'banana', 'orange'], description: 'Choose a fruit')
}
stages {
stage('Run Python Script') {
steps {
sh "python myscript.py ${params.FRUIT}"
}
}
}
}
import jenkins.model.Jenkins
def inst = Jenkins.getInstance()
def emailExt = instance.getDescriptor(
"hudson.plugins.emailext.ExtendedEmailPublisher")
emailExt.setSmtpAuth("username",
"password")
emailExt.setDefaultReplyTo("[email protected]")
emailExt.setSmtpServer("smtp.example.com")
emailExt.setUseSsl(true)
emailExt.setSmtpPort("587")
emailExt.setCharset("utf-8")
emailExt.setDefaultRecipients("[email protected]")
emailExt.save()
---
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
post {
always {
cleanWs()
}
success {
emailext body: '${JOB_NAME} succeed', subject: 'Jenkins test', to: 'dharmik.thakkar@'
}
failure {
emailext (attachLog: true, body: '${JOB_NAME} failed. Open ${BUILD_URL} to check for more details.', subject: '${BUILD_TAG} Failed', to: 'dharmik.thakkar@')
}
}
}
---
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the project...'
}
}
stage('Notify') {
steps {
script {
def sendEmail(String recipient, String subject, String body, String smtpHost, int smtpPort, String smtpUser, String smtpPassword) {
def mail = new EmailBuilder()
.from("[email protected]")
.to(recipient)
.subject(subject)
.text(body)
.smtp(host: smtpHost, port: smtpPort, username: smtpUser, password: smtpPassword)
.build()
mail.send()
}
sendEmail("[email protected]", "Jenkins Build Result", "The Jenkins build has completed. Check the results for more details.", "smtp.example.com", 587, "jenkins", "password1")
sendEmail("[email protected]", "Jenkins Build Result", "The Jenkins build has completed. Check the results for more details.", "smtp.example.net", 465, "jenkins", "password2")
}
}
}
}
}
---
pipeline {
agent any
parameters {
string(name: 'to', defaultValue: '[email protected]', description: 'Email recipient')
string(name: 'from', defaultValue: '[email protected]', description: 'Email sender')
}
stages {
stage('Test Email Configuration') {
steps {
script {
def mailer = new org.jenkinsci.plugins.mailer.DefaultMailer()
def to = params.to
def from = params.from
def subject = "Test Email from Jenkins"
def body = "This is a test email sent from Jenkins pipeline"
boolean response = mailer.send(to, subject, body, from)
if (response) {
echo "Email sent successfully!"
} else {
echo "Failed to send email!"
}
}
}
}
}
}
---
pipeline {
agent any
stages {
stage('Test Email Configuration') {
steps {
script {
def to = "[email protected]"
def subject = "Test Email from Jenkins"
def body = "This is a test email sent from Jenkins pipeline"
def buildResult = currentBuild.currentResult
if (buildResult == 'SUCCESS') {
emailnotify to: "${to}", subject: "${subject}", body: "${body}",
replyTo: "${params.from}", mimeType: 'text/html'
}
}
}
}
}
}