“Hidden” Jenkins Feature

Jenkins Logo

TLDR: For those that use a Jenkinsfile instead of build steps created within Jenkins, have you ever used the Snippet Generator feature?

What Is Jenkins

To begin, what is Jenkins? According to their website, Jenkins is the leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project. In “not too technical” words, Jenkins is a tool used to help companies release software.

While using Jenkins, users have the ability to use different plugins within their build processes. With each plugin used throughout the build, you have to configure the plugin accordingly. Let’s look at an example:

Here’s a build step to Invoke Ansible Vault via the Jenkins GUI (Not using a Jenkinsfile). We are not interested in exactly what this build step does, just showing how it looks to configure a plugin without a Jenkinsfile.

Jenkins Build Step

Let’s Generate some Snippets

Going forward, as mentioned before, the pipelines I work with are configured using a Jenkinsfile. With that being said, we can’t configure a plugin in the GUI, we have to write the code equivalent in Groovy (another programming language for those that are not familiar). Looking at that screenshot of the configurations for Ansible, what would the Groovy code equivalent be? In other words, how would you call that same plugin in Groovy? Yeah, I don’t know either 😂. The first thing I would do is look at Ansible’s documentation to see if they’ve published a Groovy example. But in my latest case, I couldn’t find any documentation. “How do I call the plugin in Groovy?” “What is the name of the plugin?” These and a couple others, were questions I asked myself, but not in such a polite way.

After chopping away at this for a day or so, I finally discovered the great Snippet Generator. According to Jenkins, the Snippet Generator will help you learn the Pipeline Script code which can be used to define various steps. Pick a step you are interested in from the list, configure it, click Generate Pipeline Script, and you will see a Pipeline Script statement that would call the step with that configuration. You may copy and paste the whole statement into your script, or pick up just the options you care about. (Most parameters are optional and can be omitted in your script, leaving them at default values.) To access this Jenkins feature, the easiest way would be to simple navigate via url:

YOUR-JENKINS-INSTANCE/pipeline-syntax (exm: hackmyjenkinsinstance.com/pipeline-syntax)

Note: I don’t know if that’s an actual site, so click at your own risk

You’ll notice under the Sample Step dropdown, there are a multitude of options. For the sake of this blog, we are only concerned with the step: General Build Step option.

Sample Step DropDown

Next, change the Build Step dropdown to the plugin you are configuring, in our case, Invoke Ansible Vault. You’ll notice that this looks exactly like the Build Step screenshot I included earlier. This time, we are going to fill in the form with our configuration values. After inputting your necessary values, click the blue Generate Pipeline Script button. And BOOM! The Groovy code equivalent has been generated for you.

Pipeline Snippet

Another Short Conclusion

As always, thanks for taking time out to read my blog. I hope this saves you time in the future.

Feel free to leave feedback or comments!

Thanks!


May 23, 2021

Written by@Keith Davis, Jr.
Hi, I'm Keith! Welcome to my blog site. I have my Bachelor's in Computer Science & my Master's in Cybersecurity.