作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在 Jenkins 中使用“执行系统 groovy 脚本”。以下代码是我在 Jenkins 的编辑框中编写的。
import hudson.model.*
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
// Get the list of all jobs and print them
activeJobs = hudson.model.Hudson.instance.items.findAll{job -> job.isBuildable()}
//activeJobs.each{run -> println(run.name)}
println ('total number of jobs :'+ activeJobs.size())
// find failed runs
failedRuns = activeJobs.findAll{job -> job.lastBuild != null && job.lastBuild.result == hudson.model.Result.FAILURE}
//failedRuns.each{run -> println(run.name)}
println ('total number of jobs :'+ failedRuns.size())
// find successful runs
successfulRuns = activeJobs.findAll{job -> job.lastBuild != null && job.lastBuild.result == hudson.model.Result.SUCCESS}
//successfulRuns.each{run -> println(run.name)}
println ('total number of jobs :'+ successfulRuns.size())
//get current date
Date date = new Date(System.currentTimeMillis())
String formattedDate = date.format('yyyy.MM.dd')
//writing to JSON Object
def directory = "D:\\Programs\\Jenkins\\jobs\\Groovy-Project\\workspace\\History\\"
def fileName = "build_job_data"
def extension = ".csv"
def filePath = directory+fileName+extension
File file = new File(filePath)
//check if the file exists
if(!file.exists()){
println("File doesn't exists. Creating the file ..");
}
else{
println("File already exists");
//reading the file
String fileContents = new File(filePath).text
int flag = 0;
if(fileContents.contains(formattedDate)){
println("Already deployed today at least once! Deleting old values and writing the new one..")
flag = 1;
}
def result = []
String textToWrite = "";
int count = 0;
result = fileContents.split('\n')
if(flag==1){
for(int i = 0; i<result.size();i++){
if(result[i].contains(formattedDate)){
println(result[i])
}
else{
textToWrite = textToWrite + '\n' + result[i]
}
}
}
else
{
for(int i = 0; i<result.size();i++){
textToWrite = textToWrite+ '\n' + result[i]
}
}
//make a backup of old record
def newFile = directory+fileName+"_bkp_"+new Date(System.currentTimeMillis()).format('yyyy-MM-dd_hh_mm_ss') + extension
println("creating back up file At :: " + newFile)
File bkpfile = new File(newFile)
bkpfile<<fileContents
println("file creation done")
//update the current history file
file.delete()
file = new File(filePath)
file<<textToWrite
file<<'\n'
}
def newDataToAdd = formattedDate+","+activeJobs.size()+","+failedRuns.size()+","+successfulRuns.size()+"\r\n"
file<<newDataToAdd
println('print done')
现在,我需要在 Jenkins 中运行与外部 groovy 脚本相同的脚本(我的意思是相同的功能)。我用上面的代码创建了一个 groovy 文件,并在作业工作区中添加了 groovy 文件。但是,在运行作业时,会出现此错误:
Caught: groovy.lang.MissingPropertyException: No such property: hudson for class: TestClass
groovy.lang.MissingPropertyException: No such property: hudson for class: TestClass
at TestClass.run(TestClass.groovy:7)
Build step 'Execute Groovy script' marked build as failure
谁能指出这个错误的原因是什么?
谢谢!!
最佳答案
我解决了这个问题。我错误地试图将它作为 groovy 脚本执行,但我应该使用执行系统 Groovy 脚本。
现在它运行良好。
关于groovy - 在 Jenkins 中使用时,外部 groovy 脚本给出错误 : groovy. lang.MissingPropertyException : No such property: hudson.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33893693/
我是一名优秀的程序员,十分优秀!