简体   繁体   中英

external groovy script is giving error : groovy.lang.MissingPropertyException: No such property: hudson. while using in Jenkins

I am currently using "Execute System groovy script" in Jenkins. The following code I have written inside the editor box inside 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')

Now, I need to run this same script( I mean same functionality) as an external groovy script in Jenkins. I created a groovy file with the above code and added the groovy file in the job workspace. But, while running the job, this error is coming :

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

Can anyone please point out what is the cause of this error?

Thanks!!

I solved the problem. By mistake I was trying to execute it as a groovy script , but I should have used Execute System Groovy script.
Now it is running fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM