-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline2.txt
89 lines (77 loc) · 2.4 KB
/
pipeline2.txt
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
sudo apt update
sudo apt install python3 python3-pip -y
python3 --version
echo 'export PATH=$PATH:/usr/bin/python3' >> ~/.bashrc
source ~/.bashrc
echo $PATH -> python3 --version
sudo chmod +x /usr/bin/python3
sudo chown jenkins:jenkins /usr/bin/python3
New Item".
Select "Pipeline" and provide a name.
Build with Parameters:
Click on "Build with Parameters" on the pipeline page.
Provide values for NUM1, NUM2, and operation.
Add.py --> must be in the workspace of the job
import sys
def main():
if len(sys.argv) != 4:
print("Usage: python addition.py <num1> <num2> <op>")
return
try:
num1 = float(sys.argv[1])
num2 = float(sys.argv[2])
op=sys.argv[3]
if op=="add":
print(f"The sum is: {num1 + num2}")
elif op=="sub":
print(f"The diff is: {num1 - num2}")
elif op=="mul":
print(f"The prod is: {num1 * num2}")
elif op=="div":
print(f"The quotient is: {num1 / num2}")
except ValueError:
print("Error: Please provide valid numbers.")
if __name__ == "__main__":
main()
Pipeline script:
pipeline {
agent any
parameters {
string(name: 'NUM1', defaultValue: '5', description: 'First number')
string(name: 'NUM2', defaultValue: '10', description: 'Second number')
string(name: 'operation', defaultValue: 'add', description: 'Enter operation (add, subtract, multiply, divide)')
}
stages {
stage('Setup Python Environment') {
steps {
script {
// Check if Python is installed and accessible
sh '''
python3 --version || (echo "Python is not installed or not in PATH" && exit 1)
'''
}
}
}
stage('Run Python Script') {
steps {
script {
// Run the Python script with arguments
sh """
python3 Add.py ${params.NUM1} ${params.NUM2} ${params.operation} || (echo "Python script execution failed" && exit 1)
"""
}
}
}
}
post {
always {
echo 'Pipeline execution completed.'
}
failure {
echo 'Pipeline failed. Check logs for details.'
}
success {
echo 'Pipeline succeeded.'
}
}
}