After setup the master-slave and own private docker registry(cloud), now we would like to discuss about how to run the groovy script in Jenkins. Before we start, lets know some information about groovy.
What is a Groovy Script?
Apache Groovy is an Object-oriented programming language used for Java platform. This dynamic language has many features which are like Python, Ruby. It can be used as a scripting language for the Java platform.
It is almost like a super version of Java which offers Java’s enterprise capabilities. It also offers many productivity features like DSL support, closures, and dynamic typing. Unlike some other languages, it is designed as a companion, not a replacement for Java.
Groovy source code gets compiled in Java Bytecode, so it can run on any platform if JRE is installed on that operating system. Groovy also performs a lot of tasks behind the scene that makes it more agile and dynamic.
Why Groovy?
Here, are major reasons why you should use Groovy-
- Groovy is an agile and dynamic language
- Seamlessly integration with all existing Java objects and libraries
- Feels easy and natural to Java developers
- More concise and meaningful code compares to Java
- You can use it as much or as little as you like with Java apps
Features of Groovy
- List, map, range, regular expression literals
- Multimethod and metaprogramming
- Groovy classes and scripts are usually stored in .groovy files
- Scripts contain Groovy statements without any class declaration.
- Scripts can also contain method definitions outside of class definitions.
- It can be compiled and fully integrated with traditional Java application.
- Language level support for maps, lists, regular expressions
- Supports closures, dynamic typing, metaobject protocol
- Support for static and dynamic typing & operator overloading
- Literal declaration for lists (arrays), maps, ranges, and regular expressions
Cons of using Groovy
- JVM and Groovy script start time is slow which limits OS-level scripting
- Groovy is not entirely accepted in other communities.
- It is not convenient to use Groovy without using IDE
- Groovy can be slower which increased the development time
- Groovy may need lots of memory
- Knowledge of Java is imperative.
To know more: http://groovy-lang.org/
Here are the List of way using you can apply groovy code in Jenkins
Method -1: Groovy plugin
This plugin adds the ability to directly execute Groovy code.
Method -2: Jenkins script console
The inbuilt script console is a very useful and powerful Jenkins extension and ideally suits certain types of tasks. This is simple but powerful built-in web console allows you to run Groovy scripts on your Jenkins instance (or its slave nodes) from directly within the user interface and is installed as standard.
Running Script Console on the master
This feature can be accessed from “Manage Jenkins” > “Script Console”. Or visit the sub-URL /script on your Jenkins instance.
Running Script Console on agents
Visit “Manage Jenkins” > “Manage Nodes”. Select any node to view the status page. In the menu on the left, a menu item is available to open a “Script Console” on that specific agent.
More info – https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console
Method -3: Scriptler Plugins
Scriptler allows you to store/edit groovy scripts and execute it on any of the slaves/nodes… no need to copy/paste groovy code anymore.
Note: The current version of this plugin may not be safe to use. Please review the warnings before use.
More Info – https://wiki.jenkins.io/display/JENKINS/Scriptler+Plugin