Configure Plugins with JCasC
This blog post is for anyone interested to know how to configure a plugin using the Jenkins Configuration as a Code (JCasC) plugin, more specifically, this blog will guide you to get the YAML equivalent of a plugin’s configuration and use it to do some changes to the plugin without using the Jenkins UI.
If you’re a beginner at JCasC and want to learn more about it, you can head over to the following resources to understand JCasC better:
So, these are the steps we will be following to achieve our aim:
Brief Introduction to jenkins.yaml file
jenkins.yamlfile contains the configuration of the Jenkins instance in YAML format. The JCasC plugin refers to this file to configure the Jenkins instance.
The default location of
$JENKINS_HOME/jenkins.yaml, from where it can be fetched into the Jenkins server whenever you apply a new configuration.
jenkins.yamlfile by going to
Configuration as Code>
Make sure this file is saved at location
Let’s change the
Reload the existing configuration to apply the system message change
Now, go back to the Dashboard and you can see the updated System Message on top:
This file will be used later to configure the plugin using JCasC.
Configure the plugin on the UI
For this demo, install the
View Job Filtersplugin.
Let’s create a view by clicking on the
New Viewoption on the left side of the Dashboard.
Give it a name (say, “testView”) and set its type to
List View, and click on the
Now click on
Add Job Filterto add any kind of filter, so let’s select
Build Duration Filterand fill the field with any value (say, "60" minutes),
To view the full configuration, check out your main
jenkins.yamlconfiguration file, by clicking on
Configuration as Code>
Go to the
viewssection in this YAML file to see details related to the view,
Download the Configuration
Now that you have successfully configured your plugin by UI, let’s download the configuration by going to
Manage Jenkinson the Dashboard, then click on
Configuration as Codeunder "System Configuration".
Now click on the
Download Configurationbutton to save the configuration file locally.
Update JCasC file locally
Add some changes in your downloaded copy of the
jenkins.yamlfile, to see those changes being automatically reflected on the UI.
For demo purposes, let’s change the
nameto “YoutubeDemoView” and set the
Save the file.
Load the jenkins.yaml file on the Jenkins server
Now to reflect the local changes done in the
jenkins.yamlfile onto the Jenkins server, click on the
Reload existing configurationbutton.
Verify the changes on the UI
Go back to the main page by clicking on the Jenkins logo on the top-left side.
And you will notice that the name of your view has been changed from "testView" to “YoutubeDemoView”,
And the field value of
Build Duration Filterhas been changed from "60" to “55”.
These two are the exact changes that we did locally in our
Congratulations! You’ve successfully configured a plugin (“View Job Filter”) automatically with the help of the “Jenkins Configuration as Code” plugin! You can repeat the same process for other plugins as well.