Jenkins

Create Groovy Hook Scripts in Jenkins

Groovy is a powerful, optionally typed, and dynamic language with static-typing and static compilation capabilities for the Java platform. It is aimed at improving developer productivity.

On the other hand, Jenkins is a free and open-source automation server that automates the repetitive parts of the software development process.

We can use Groovy scripts in Jenkins to execute some tasks. For example, Jenkins provides the Groovy build step and the Groovy Postbuild plugins that allow us to execute Groovy scripts as part of the build process, either as a build step or as a post-build action.

Additionally, Jenkins allows us to define Hook scripts executed before or after certain events, such as before or after a build. These Hook scripts can be written in Groovy, allowing users to customize Jenkins’ behavior. For example, we can use a Groovy Hook script to send a notification message to a chat room or to update an external issue tracking system when a build finishes.

This tutorial will teach us how to create and use Hook Groovy scripts in Jenkins pipelines.

How to Create Hook Groovy Scripts in Jenkins

You can create a Hook Groovy Script in Jenkins in two main ways. The first is by using the Jenkins Script Console feature.

To do this, head to the Jenkins Dashboard and click on Manage Jenkins in the left menu.


Under the Tools and Actions section, select the Script Console in the Manage Jenkins menu.


You can enter your Groovy script in the Script Console text box. Once you are ready, you can run the script by clicking the Run button to execute the script.

The second method you can use to create a Hook Groovy Script in Jenkins is by creating the Groovy script in the $JENKINS_HOME/init.groovy.d directory. During startup, Jenkins will automatically execute any Groovy scripts in this directory.

Example

In the example below, we will create a Hook Groovy script that writes the Console Output of a Jenkins build to a file.

The code is as provided below:

import hudson.model.*
def build = Hudson.instance.getItem("sample_project").getLastBuild()
def consoleOutput = build.getLog()
new File("/var/log/jenkins_build.log").write(consoleOutput)

 
This script will get the console output of the last build of a project with the name sample_project, and then it will write the output to the specified log file.

Example 2

If you wish to use the Jenkins log directory, create an init.groovy.d directory in the $JENKINS_HOME directory.

Next, create a log.groovy file to store the source code for your Hook Script. In this case, our script will write the Console Output of all the builds in a given project as shown in the source code below:

import hudson.model.*
def project = Hudson.instance.getItem("sample_project ")
for (build in project.getBuilds()) {
  def consoleOutput = build.getLog()
  new File("/var/log/logs.log").write(consoleOutput)
}

 
This script will iterate through all project builds with the name sample_project and write the console output of each build to the specified log file.

Conclusion

We discovered the various methods and techniques we can use to create Hook Groovy Scripts in Jenkins using the Jenkins Script Console and the Jenkins Groovy scripts directory.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list