Skip to content

Demo logic that load .groovy files and executes them allowing referencing each other and passing parameters up and down

License

Notifications You must be signed in to change notification settings

Orekaria/com.orekaria.groovy.executescriptfiles

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

Contains logic to load .groovy files and execute them; allowing replacing methods, catching missing methods, referencing each other and passing parameters up and down, etc.

Only handles Groovy scripts

Note: the structure of the directories is add-hoc (ala Jenkins shared pipelines style), but other structures will work

Quickstart

You may start running all tests, and checking which ones you are interested on. Then, look at the ...Test class that runs it, then at the scripts it loads

command line

  • clone the repo
  • run gradlew test

IntelliJ

  • open the project
  • in the gradle tab -> Tasks -> verification, execute test

Code explained

The project is divided in 3 groups of components: scripts (in vars), script wrappers (in test/src) and tests (in test/pipelines)

And organized in case 0, 1, 2, 3

  • case 0: with only the standaloneTest, that can run by its own

  • case 1: some random approaches

  • case 2: gives more weight to the deeper script. c21_ciPipeline in this case

  • case 3: gives more weight to the main script. c30_pipeline in this case

    this is the approach that I will finally implement in a Jenkins Shared Library project

    it is thread safe and includes a thread safe test

  • case 4:

    this is a very interesting variant in which delegates are defined in order to run a DSL script

GroovyScriptHelper

This is the class that:

  • loads the script to be run
  • loads all the scripts in the vars folder and adds references to them in the script to run
  • injects new variables to the script
  • redirects missing method and properties to the caller; overriding .metaClass.methodMissing and .metaClass.propertyMissing

The method that loads the Groovy script:

private static Script loadScriptFromVars(String filename) {
   GroovyScriptEngine gse = new GroovyScriptEngine('vars/')
   Class<Script> scriptClass = gse.loadScriptByName("${filename}.groovy")
   Script script = scriptClass.newInstance()
   return script
}

The method that injects new methods to the Groovy script:

private static void injectMocks(Script script) {
   script.getBinding().with {
      setVariable("echo", { println it })
      setVariable("step", { String s, Closure cl ->
         println "inside step ${s}"
         cl.call(s)
      })
      ...
   }
}

Thanks to the https://github.com/ExpediaGroup/jenkins-spock project for insights on how to better load a Groovy script

About

Demo logic that load .groovy files and executes them allowing referencing each other and passing parameters up and down

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published