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:
JCasC Documentation
Overview of JCasC (Video Presentation)
Manage JCasC (DevOps World 2018)
Configuring your first plugin using JCasC (Video Demo)
Overview
So, these are the steps we will be following to achieve our aim:
Brief Introduction to jenkins.yaml file
Configure the plugin on the UI
Download the Configuration
Update JCasC file locally
Load the jenkins.yaml file on the Jenkins server
Verify the changes on the UI
Brief Introduction to jenkins.yaml file
The jenkins.yaml file 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.yaml is $JENKINS_HOME/jenkins.yaml, from where it can be fetched into the Jenkins server whenever you apply a new configuration.
Download your jenkins.yaml file by going to Manage Jenkins Configuration as Code Download Configuration.
Make sure this file is saved at location $JENKINS_HOME/jenkins.yaml.
Let’s change the systemMessage field to:
Figure 1. Updating the jenkins.yaml file
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:
Figure 2. Viewing the changes on Dashboard
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 Filters plugin.
Let’s create a view by clicking on the New View option on the left side of the Dashboard.
Give it a name (say, “testView”) and set its type to List View, and click on the OK button.
Figure 3. Creating the View
Now click on Add Job Filter to add any kind of filter, so let’s select Build Duration Filter and fill the field with any value (say, "60" minutes),
Figure 4. Adding filter to the view
Click on Apply Save.
To view the full configuration, check out your main jenkins.yaml configuration file, by clicking on Manage Jenkins Configuration as Code View Configuration
Go to the views section in this YAML file to see details related to the view,
Figure 5. Here, details regarding the view (which we just created) is visible
Download the Configuration
Now that you have successfully configured your plugin by UI, let’s download the configuration by going to Manage Jenkins on the Dashboard, then click on Configuration as Code under "System Configuration".
Now click on the Download Configuration button to save the configuration file locally.
Figure 6. Downloading the Configuration
Update JCasC file locally
Add some changes in your downloaded copy of the jenkins.yaml file, to see those changes being automatically reflected on the UI.
For demo purposes, let’s change the name to “YoutubeDemoView” and set the buildDurationMinutes as "55".
Figure 7. Changing the View details locally
Save the file.
Load the jenkins.yaml file on the Jenkins server
Now to reflect the local changes done in the jenkins.yaml file onto the Jenkins server, click on the Reload existing configuration button.
Figure 8. Applying the New Configuration to the Jenkins instance
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 Filter has been changed from "60" to “55”.
These two are the exact changes that we did locally in our jenkins.yaml file.
Figure 9. Verifying the changes
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.