-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
200 lines (173 loc) · 6.77 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
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
// @NonCPS
def resetJobBuilds(String jobName) {
// ดึงข้อมูล Job
def job = Jenkins.instance.getItem(jobName)
if (job == null) {
println "Job '${jobName}' not found!"
return
}
// ลบเฉพาะบิลด์ที่เสร็จสิ้นแล้ว
println "Deleting completed builds for job: ${jobName}"
job.getBuilds().each { build ->
if (!build.isBuilding()) { // ตรวจสอบว่าไม่ได้กำลังรันอยู่
try {
println "Deleting build #${build.number} (Status: ${build.result})"
build.delete()
} catch (Exception e) {
println "Error deleting build #${build.number}: ${e.message}"
}
} else {
// รีเซ็ตหมายเลขบิลด์ถัดไปเป็น 1
println "Skipping build #${build.number} (Currently running)"
println "Resetting the next build number for job: ${jobName}"
try {
job.nextBuildNumber = build.number
job.save()
println "Successfully reset the next build number for job: ${jobName} build number: ${build.number}"
} catch (Exception e) {
println "Error resetting build number: ${e.message}"
}
}
}
}
def setPropertys() {
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","sandbox allow-scripts; default-src 'none'; img-src 'self' data: ; style-src 'self' 'unsafe-inline' data: ; script-src 'self' 'unsafe-inline' 'unsafe-eval' ;")
}
def convertpath(String files) {
// Load the data file
def dataFile = load files
// Use the parameter as the key
def keyToRetrieve = params.BATFILE_NAME
// Retrieve the data
def convertData = dataFile.getData(keyToRetrieve)
// Check if the data was found
if (convertData == null) {
// Handle missing key
echo "Warning: No data found for key '${keyToRetrieve}'"
// Optionally set default values here or decide how to handle this case
env.CONVERT_TAG = 'DEFAULT_TAG'
env.CONVERT_RESULT_PATH = 'DEFAULT_RESULT_PATH'
env.CONVERT_ROBOT_PATH = 'DEFAULT_ROBOT_PATH'
} else {
// Check if the data is valid
if (convertData instanceof String) {
error "Data retrieval failed: ${convertData}"
}
// Set environment variables
env.CONVERT_TAG = convertData.tag
env.CONVERT_RESULT_PATH = convertData.resultpath
env.CONVERT_ROBOT_PATH = convertData.robotpath
// Output the results
echo "Converted result path: ${env.CONVERT_TAG}"
echo "Converted result path: ${env.CONVERT_RESULT_PATH}"
echo "Converted result path: ${env.CONVERT_ROBOT_PATH}"
}
}
def logoutput(){
def scriptOutput = bat(script: "findoutput.bat ${env.INITIAL_RESULT_PATH}${env.CONVERT_RESULT_PATH}", returnStdout: true).trim()
echo "Output from external script: ${scriptOutput}"
def outputLength = scriptOutput.length()
def startIndex = outputLength - 16
def trimmedName = scriptOutput.substring(startIndex)
echo "Trimmed variable name: ${trimmedName}"
def outputFileName = "output${trimmedName}.xml"
def reportFileName = "report${trimmedName}.html"
def logFileName = "log${trimmedName}.html"
// Publish Robot results with dynamic file names
step([
$class : 'RobotPublisher',
outputPath : "${env.INITIAL_RESULT_PATH}${env.CONVERT_RESULT_PATH}",
outputFileName : outputFileName,
reportFileName : reportFileName,
logFileName : logFileName,
disableArchiveOutput: true,
otherFiles : '*.png,*.jpg'
])
}
pipeline {
agent {
node {
label 'built-in'
}
}
options {
ansiColor('xterm')
}
environment {
INITIAL_BATFILE_PATH = 'C:/ProgramData/Jenkins/.jenkins/workspace/RobotFramework_demo_pipeline/'
INITIAL_LIBRARY_PATH = 'C:/ProgramData/Jenkins/.jenkins/workspace/RobotFramework_demo_pipeline/Resources/Library/'
INITIAL_RESULT_PATH = 'C:\\ProgramData\\Jenkins\\.jenkins\\workspace\\Result\\RobotFramework_demo_pipeline\\'
}
stages {
stage('Check Python version') {
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
bat label: 'Check Python version', script: '''
python --version
'''
}
}
}
stage('Check chrome version') {
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
bat label: 'Check chrome version', script: """
echo Running file: AutoupdateChrome.py
cd ${env.INITIAL_LIBRARY_PATH}
python AutoupdateChrome.py
"""
}
}
}
// stage('Check pip list version') {
// steps {
// catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
// bat label: 'Check pip version', script: '''
// pip list
// '''
// }
// }
// }
// stage('Check pip install requirements') {
// steps {
// catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
// bat label: 'Install Requirements', script: '''
// pip install -r requirements.txt
// '''
// }
// }
// }
stage('Convert Parameter') {
steps {
script {
convertpath('jenkinsdata')
}
}
}
stage('RUN E2E') {
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
bat label: 'RUN E2E', script: """
echo Running batch file ${env.CONVERT_TAG} ${env.INITIAL_RESULT_PATH}${env.CONVERT_RESULT_PATH} ${env.INITIAL_BATFILE_PATH}${env.CONVERT_ROBOT_PATH}
call jenkins_batfile.bat ${env.CONVERT_TAG} ${env.INITIAL_RESULT_PATH}${env.CONVERT_RESULT_PATH} ${env.INITIAL_BATFILE_PATH}${env.CONVERT_ROBOT_PATH}
"""
}
}
}
stage('Run Groovy Script') {
steps {
script {
setPropertys()
// resetJobBuilds("Automate")
}
}
}
}
post {
always {
script {
logoutput()
}
}
}
}