How to run/execute groovy script in Jenkins?

Here are the List of way using you can apply grovy code in Jenkins

Method -1: Groovy plugin
This plugin adds the ability to directly execute Groovy code.
More info – https://wiki.jenkins.io/display/JENKINS/Groovy+plugin

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 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.

To access the console, either navigate to Manage Jenkins and then select Script Console, or simply append /script to your Jenkins host and port, like this for example: http://jenkinshost:8080/script
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.
More Info – https://wiki.jenkins.io/display/JENKINS/Scriptler+Plugin

Reference

https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console

 

Tagged : / / / /

Database of Groovy Script for Jenkins Automation

Here are the list of Groovy Script which can be used in Jenkins to automate the JOBs and CI process. Please add your list in the comment sections as well.

Main Source – 

  • https://github.com/scmgalaxy/jenkins-groovy-script-example
  • https://github.com/cloudbees/jenkins-scripts
  • https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console
  • https://pghalliday.com/jenkins/groovy/sonar/chef/configuration/management/2014/09/21/some-useful-
  • jenkins-groovy-scripts.html
  • https://github.com/cloudbees/jenkins-scripts
  • https://github.com/jenkinsci/jenkins-scripts
  • https://gist.github.com/dnozay/e7afcf7a7dd8f73a4e05
  • https://github.com/samrocketman/jenkins-bootstrap-shared
  • https://github.com/samrocketman/jenkins-script-console-scripts

Example Groovy Script 

Tagged : /

groovy.lang.MissingPropertyException: No such property: CASE for class

rajeshkumar created the topic: groovy.lang.MissingPropertyException: No such property: CASE for class
Error
Started by user Rajesh Kumar
[EnvInject] – Loading node environment variables.
[EnvInject] – Preparing an environment for the build.
[EnvInject] – Keeping Jenkins system variables.
[EnvInject] – Keeping Jenkins build variables.
[EnvInject] – Evaluation the following Groovy script content:
if (CASE==null){
return null;
}
def stringValue=”StRinG”;
if (“upper”.equals(CASE)){
def map = [product: stringValue.toUpperCase()]
return map
}

if (“lower”.equals(CASE)){
def map = [product: stringValue.toLowerCase()]
return map
}

FATAL: No such property: CASE for class: Script1
groovy.lang.MissingPropertyException: No such property: CASE for class: Script1
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at Script1.run(Script1.groovy:1)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)
at org.jenkinsci.plugins.envinject.service.EnvInjectEnvVars.executeAndGetMapGroovyScript(EnvInjectEnvVars.java:142)
at org.jenkinsci.plugins.envinject.EnvInjectListener.setUpEnvironmentJobPropertyObject(EnvInjectListener.java:188)
at org.jenkinsci.plugins.envinject.EnvInjectListener.setUpEnvironment(EnvInjectListener.java:44)
at hudson.model.AbstractBuild$AbstractBuildExecution.createLauncher(AbstractBuild.java:575)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:481)
at hudson.model.Run.execute(Run.java:1688)
at hudson.model.Run.run(Run.java:1635)
at com.cloudbees.plugins.flow.FlowRun.run(FlowRun.java:153)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:231)
at hudson.model.OneOffExecutor.run(OneOffExecutor.java:43)
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Top 10 Scripting Languages in DevOps | List of Best Scripting Languages

top-10-scripting-languages-in-devops
This is the time of DevOps in software industry and DevOps uses different different languages for deployment automation and for software development. This is the reason if you are a DevOps professional and want to be succeed in DevOps role than command on scripting languages is must. But, one can not be a master of all. Right? Therefore, In this article I am going to share a list of top 10 scripting languages which will be useful for your DevOps journey.
But before that let’s have a quick look on Scripting language.
Scripting languages are programming languages that communicate and integrate with other programming languages. In other words, scripting languages controlls interactive programs operations by giving it sequence of work to execute by one command at a time.
Now, let’s look on to the list of top 10 scripting languages

1. Microsoft PowerShell

Microsoft PowerShell

Microsoft powershell or powershell is belongs to Microsoft and an open source cross platform scripting language. If you have application infrastructure with windows than powershell is must for you. This scripting language is designed for system admins.
2. Puppet

Puppet

Puppet is a configuration management tool and it has it’s own declarative language to describe system configuration. It runs on Linux, Unix-like and also on Windows. This one is available under Apache 2.7.0 and General Pubic license.  Puppet uses a custom declarative language, it just needs to specify ‘what’ action needs to be performed on the resources.
3. Chef

Chef

Chef is basically known as configuration management which belongs to chef is also a scripting language which is designed by David Morgan. It makes programs look like cooking any food.
4. Bash

Bash

Bash is basically a command language which is available in open source and written by Brian Fox in the year 1989. It can read scripts and Bash is the most commonly used Unix shell. Bash supports Linux, Windows and Mac OS.
5. Ruby

Ruby

Ruby is amongst one of the best programming language but it is also a scripting language which is written by Yukihiro Matsumoto in the year 1995. Ruby supports cross platforms and it is available under GPL and BSD license. It supports multiple programming paradigms, including functional, object-oriented, and imperative.
6. Ansible

Ansible

Ansible is known as configuration management and application deployment tool but it is also amongst top scripting languages. This language is belongs to Ansible Inc. and written by their community members. It supports Linux, Unix-like and Windows operating system.
7. Perl

Perl

Perl is a scripting language which is used for advanced web applications development. Perl is written by Larry Wall and first released in the year 1987. Perl supports cross platforms. Perl is available under general public license.
8. Python

Python

Python is also amongst the top scripting languages which is used for high level of programming. It was first released in the year 1991 by Guido van Rossum. python is available under Python Software Foundation License. It’s supports Cross-platform.
9. Go language

Go language

Go scripting language belongs to the Internet giant Google. This scripting language is written by Robert Griesemer, Rob Pike and Ken Thompson and it was released in the year 2009. It supports  Linux, macOS, FreeBSD, NetBSD, OpenBSD, Windows, Plan 9, DragonFly BSD and Solaris operating systems. It is available in open source.
10. Groovy

Groovy

Groovy can be used as a scripting language and it is also consider as a top scripting languages for DevOps professionals. It was designed by James Strachan and developed by Guillaume Laforge, Jochen Theodorou, Paul King and Cedric Champeau. It was first released in the year 2003 and available under Apache license. It supports Java platform.
Do you agree with this list? If not than feel free to respond in the comment box with your own take on the most essential scripting languages. One more thing, I would like to add here, if you need help to learn all these scripting languages and DevOps courses than scmGalaxy can help you in this. scmGalaxy is a community of DevOps professionals who are well experienced in this domain.
Tagged : / / / / / / / / / / / / / / / / / / /