Our legacy site: www.webwob.com   

Load Groovy code from an external file (or from git)

I do like to make use of the Jenkins 'Groovy Postbuild' step. It does offer me a lot of extra functionality. In particular I find it useful to set the Build status on the issues I see as important. So for instance, I check the Jenkins log for ".*Overall SLA status: FAIL.*" and if found, set the build to Fail, otherwise set the build to Pass.

    JV2 Edit LRS     Pipeline code     Jenkins with Git     Git2     bash2     bash3     bash4     Git setup     analysis     injectors     scripts     display     shells     repository     Jenkins x2     LR from cygwin     Groovy!     bash5     Calling Bamboo     Bamboo Calling


    

How to load Groovy code from an external file


  This is just a quick note on how to load groovy code in from an external file.
  Took me ages to work this out! (with some googling in the end of course!)

Screenshot

  
Use this code:


    
evaluate(new File("Z:\\github\\PerformanceTesting\\jenkins\\postbuild.groovy"));

  
OR

    
evaluate(new File("Z:\\github\\PerformanceTesting\\jenkins\\postbuild_no_slas.groovy"));

  
  
  These files can then contain the code that you'd usually enter in the Postbuild dialog box directly. e.g:
  
  //NEW approach now:
  manager.listener.logger.println("starting Groovy post build (no slas)")
  if(manager.logContains(".*last jtl file sets value to fail.*"))
  {
      //manager.listener.logger.println("Groovy setting build to FAILED")
      //manager.build.@result = hudson.model.Result.FAILURE
      
      manager.listener.logger.println("IGNORING jtl file FAIL because this is postbuild_no_slas.groovy")
      manager.listener.logger.println("Groovy setting build to PASSED")
      manager.build.@result = hudson.model.Result.SUCCESS
  }
  else
  {
      manager.listener.logger.println("Groovy setting build to PASSED")
      manager.build.@result = hudson.model.Result.SUCCESS
  }
  
  if(manager.logContains(".*Controller IS IN USE.*"))
  {
      manager.listener.logger.println("Groovy setting build to FAILED")
      manager.listener.logger.println("Controller IS IN USE")
      manager.build.@result = hudson.model.Result.FAILURE
  }
  
  if(manager.logContains(".*Fiddler IS IN USE.*"))
  {
      manager.listener.logger.println("Groovy setting build to FAILED")
      manager.listener.logger.println("Fiddler IS IN USE")
      manager.build.@result = hudson.model.Result.FAILURE
  }
  
  if(manager.logContains(".*Analysis IS IN USE.*"))
  {
      manager.listener.logger.println("Groovy setting build to FAILED")
      manager.listener.logger.println("Analysis IS IN USE")
      manager.build.@result = hudson.model.Result.FAILURE
  }
  
  manager.listener.logger.println("ending Groovy post build (no slas)")