Jenkins is a highly extensible automation server, largely due to its vast ecosystem of plugins. However, managing plugins manually—especially in larger Jenkins instances—can become time-consuming and error-prone. Automating Jenkins plugin installation and configuration is key to scaling Jenkins while ensuring consistency and repeatability.
In this post, we will explore how to automate the installation and configuration of Jenkins plugins, using both the Jenkins UI, Jenkins Configuration as Code (JCasC), and other tools. This automation will streamline the setup of your Jenkins instance, making it more efficient and scalable.
For basic installation and configuration guide for Jenkins Configuratio nas Code, please check my previous post here.
1. Why Automate Jenkins Plugins Installation and Configuration?
Jenkins plugins extend the functionality of Jenkins, enabling it to integrate with a wide range of tools, services, and workflows. However, manually installing plugins and configuring them through the Jenkins UI can lead to:
- Inconsistencies: It is difficult to ensure that multiple Jenkins instances are configured the same way.
- Manual errors: Mistakes may occur during plugin installation and configuration, leading to job failures or security vulnerabilities.
- Tedious process: Manually setting up dozens or even hundreds of plugins across multiple Jenkins instances can take considerable time.
Automating plugin installation and configuration helps:
- Ensure consistency: Automate plugin installations across multiple Jenkins instances.
- Enable repeatability: Easily replicate the same configuration in different environments (e.g., development, staging, and production).
- Speed up setup: Automatically configure Jenkins with plugins without manual intervention.
- Ensure version control: Track and version control your plugin configurations using code, ensuring transparency and repeatability.
2. Prerequisites
Before proceeding, ensure you have:
- A running Jenkins instance: Preferably version 2.222.1 or higher.
- Admin privileges: Ensure you have administrative access to your Jenkins instance.
- Jenkins CLI or JCasC plugin: Depending on the approach you choose for automating plugin installation.
- Basic understanding of YAML syntax (for JCasC).
3. Installing Plugins via Jenkins UI
While not automated, it’s helpful to know how plugin installation works via the Jenkins UI:
Steps for Manual Installation:
- Log in to Jenkins with admin privileges.
- Navigate to Manage Jenkins > Manage Plugins.
- In the Available tab, search for the desired plugins.
- Select the plugins to install and click Install without restart or Install and restart.
This method is useful for testing but is inefficient and error-prone for managing production environments at scale. That's where automation comes in.
4. Automating Plugin Installation via JCasC
Jenkins Configuration as Code (JCasC) is an ideal method to automate plugin installation. You can specify plugins in a YAML configuration file, which Jenkins will apply when it starts up.
4.1 Specifying Plugins in JCasC
You can define the plugins you want Jenkins to install by specifying them in your JCasC YAML file. JCasC will automatically install and configure the specified plugins when Jenkins starts.
4.2 Example JCasC Configuration for Plugins
Below is an example YAML configuration for automating plugin installation using JCasC:
jenkins:
systemMessage: "Jenkins configured automatically with JCasC"
# Define plugins to be installed
plugins:
required:
- "git:4.10.0"
- "pipeline:2.6"
- "blueocean:1.25.0"
- "credentials:2.3.19"
- "workflow-aggregator:2.6"
- "ssh-credentials:1.19"
- "email-ext:2.83"
# Jenkins credentials example (optional)
credentials:
system:
domainCredentials:
- credentials:
- basicSSHUserPrivateKey:
scope: SYSTEM
id: "ssh-credentials"
username: "git-user"
privateKeySource:
directEntry:
privateKey: |
-----BEGIN RSA PRIVATE KEY-----
(Your Private Key Here)
-----END RSA PRIVATE KEY-----
In this example:
- The
pluginssection defines the plugins and their versions that Jenkins should install. - The Git, Pipeline, Blue Ocean, Credentials, and other plugins are automatically installed and configured when Jenkins starts.
- You can specify versions for plugins to ensure compatibility and stability across your Jenkins instances.
Loading the JCasC File
To apply the configuration:
- Set the
CASC_JENKINS_CONFIGenvironment variable to point to your YAML file location. - Restart Jenkins, and the plugins will be automatically installed based on your JCasC configuration.
5. Automating Plugin Installation via the Jenkins CLI
An alternative method to automate plugin installation is using the Jenkins CLI (Command Line Interface). This is useful for quickly installing or updating plugins without modifying your JCasC configuration.
Steps to Install Plugins via Jenkins CLI:
Restart Jenkins (if required):
Some plugins might need a Jenkins restart. You can restart Jenkins via the CLI:
java -jar jenkins-cli.jar -s http://localhost:8080 safe-restart
Install Plugins:
Use the following command to install plugins using the Jenkins CLI:
java -jar jenkins-cli.jar -s http://<jenkins-url> install-plugin <plugin-name>
Example:
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin git blueocean
Download the Jenkins CLI jar:
wget http://<jenkins-url>/jnlpJars/jenkins-cli.jar
Installing Multiple Plugins
To install multiple plugins at once, you can use a file that lists all the required plugins:
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin < plugin-list.txt
Where plugin-list.txt is a text file that contains a list of all the plugins, each on a new line.
6. Configuring Plugins Automatically
In addition to installing plugins, Jenkins Configuration as Code (JCasC) can also be used to configure plugins automatically.
For example, if you're using the Git plugin, you can configure it by adding the following to your YAML file:
unclassified:
gitPlugin:
configuration:
globalConfigName: "Jenkins Admin"
globalConfigEmail: "admin@mycompany.com"
You can configure other plugins similarly, by referring to their respective JCasC configuration schemas.
To see the current configuration of a plugin and create your JCasC file, navigate to Manage Jenkins > Configuration as Code > View Configuration, where you can download the current configuration in YAML format.
7. Best Practices for Plugin Management
When automating Jenkins plugin installation and configuration, keep the following best practices in mind:
- Pin Plugin Versions: Always specify the version of each plugin to avoid issues with incompatible updates. This ensures your builds run smoothly across Jenkins environments.
- Keep Plugins Up to Date: Regularly update plugins to benefit from security patches, bug fixes, and new features. Automating plugin updates can help with this.
- Remove Unused Plugins: Over time, your Jenkins instance may accumulate unused plugins, which can slow down the system and introduce security vulnerabilities. Periodically audit your plugin list and remove unused ones.
- Test Plugin Configurations: Test your JCasC YAML configurations in a development environment before deploying them to production.
- Document Plugin Usage: Create a detailed document explaining which plugins are installed and why. This helps with troubleshooting and collaboration.
- Monitor Plugin Compatibility: Some plugins may conflict with each other, or certain plugins might be deprecated. Monitor plugin compatibility to avoid issues.
Conclusion
Automating Jenkins plugin installation and configuration is essential for scaling and managing Jenkins effectively. By using Jenkins Configuration as Code (JCasC), the Jenkins CLI, or other automation tools, you can streamline the process of installing, updating, and configuring plugins.
In this post, we explored various methods to automate plugin management, including using JCasC for defining plugins in a YAML file, using the Jenkins CLI for quick installations, and configuring plugins automatically. We also discussed best practices for managing plugins in a Jenkins instance.
By embracing automation, you can save time, reduce errors, and maintain a consistent Jenkins environment across multiple instances and environments.