Baeldung Pro – Ops – NPI EA (cat = Baeldung on Ops)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (cat=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

Jenkins has a powerful plugin management system. We may occasionally need to update a plugin using the Jenkins Dashboard when a new version of the plugin is available. However, updating plugins from the command line is useful for administrators to automate tasks.

In this tutorial, we’ll discuss how to update Jenkins plugins from the command line. After a brief discussion of the Jenkins CLI (Command-Line Interface), we’ll first learn the Jenkins CLI’s install-plugin command. Then, we’ll see how to use the groovy command of the Jenkins CLI.

The version of Jenkins we use is 2.452.3. Jenkins is running on localhost on port 8080. We need administrator rights in Jenkins to update plugins.

2. The Jenkins CLI

Jenkins provides a CLI to access Jenkins from a script or command line. This built-in CLI is known as the Jenkins CLI. It’s useful for automating routine tasks.

The jenkins-cli.jar jar file allows us to access the Jenkins CLI. We can download it from the Jenkins CLI view of the Manage Jenkins page in the Jenkins UI.

There are several commands of the Jenkins CLI. For example, the list-plugins command lists the installed Jenkins plugins. We’ll use the install-plugin and groovy commands to update Jenkins plugins.

3. Using the install-plugin Command

In this section, we’ll first get the list of plugins needing updating and then update those plugins using install-plugin.

3.1. Getting the List of Plugins

After downloading and moving the jenkins-cli.jar file to the current directory, let’s first list the installed plugins by running the java -jar command:

$ java -jar jenkins-cli.jar -s http://localhost:8080 -auth admin:admin_secret_password list-plugins
ant                                Ant Plugin                                  497.v94e7d9fffa_b_9 (511.v0a_a_1a_334f41b_)
antisamy-markup-formatter          OWASP Markup Formatter Plugin               162.v0e6ec0fcfcf6
apache-httpcomponents-client-4-api Apache HttpComponents Client 4.x API Plugin 4.5.14-208.v438351942757
...

The remaining options and commands after java -jar jenkins-cli.jar belong to jenkins-cli.jar. We pass the address of the Jenkins controller using the -s option, which is http://localhost:8080. The -auth option specifies the Jenkins username and password. So, we pass admin and admin_secret_password as the username and password. The list-plugins command of the Jenkins CLI lists the installed plugins.

We abbreviated the output to list only the first three plugins. The list consists of three columns. The first two columns contain the short and long names of the plugins, and the last column contains the version of each plugin.

The plugin at the top of the list is the Ant Plugin. Notably, there are two items in the last column. The first one, 497.v94e7d9fffa_b_9, is the currently installed plugin’s version. If there’s a newer version of a plugin, it’s shown in parentheses. According to the output, there’s a newer version of the Ant Plugin, 511.v0a_a_1a_334f41b_.

Let’s get the names of the plugins needing update by filtering the output:

$ update_list=$(java -jar jenkins-cli.jar -s http://localhost:8080 -auth admin:admin_secret_password list-plugins | grep ")$" | awk '{print $1}')
$ echo $update_list
ant commons-lang3-api echarts-api font-awesome-api git github-branch-source github gradle junit pipeline-graph-view pipeline-groovy-lib pipeline-model-api pipeline-model-definition pipeline-model-extensions pipeline-stage-tags-metadata workflow-api workflow-cps workflow-durable-task-step

We used the grep “)$” command to list only the lines ending with a closing parenthesis. Furthermore, we got the names in the first column using the awk ‘{print $1}’ command. Finally, we used command substitution to assign the list of plugins to the update_list variable.

3.2. Updating the Plugins

Now, let’s install the plugins stored in the update_list variable together with their dependencies. For this purpose, let’s use the install-plugin command of the Jenkins CLI:

$ java -jar jenkins-cli.jar -s http://localhost:8080 -auth admin: admin_secret_password install-plugin $update_list -restart
Installing ant from update center
Installing commons-lang3-api from update center
Installing echarts-api from update center
...

The plugins are installed one after the other. Upon successful installation, the -restart option restarts Jenkins. The installed plugins take effect after we restart Jenkins.

Let’s list the plugins needing update once more after the installation:

$ java -jar jenkins-cli.jar -s http://localhost:8080 -auth admin:admin_secret_password list-plugins | grep ")$" | awk '{print $1}'

Since we updated all plugins, we should not see any newer versions to update.

4. Using the groovy Command

Another way to update plugins using the Jenkins CLI is to use the groovy command, which runs Groovy scripts.

Let’s run a Groovy script, update_plugins.groovy, that updates plugins:

$ java -jar jenkins-cli.jar -s http://localhost:8080 -auth admin: admin_secret_password groovy = < update_plugins.groovy

It’s the groovy = < update_plugins.groovy part that’s important for us. Here, the groovy command runs the update_plugins.groovy script. = <  between the groovy command and update_plugins.groovy feeds the contents of the script to the Jenkins CLI using stdin.

Let’s now see what we do within the update_plugins.groovy script:

$ cat update_plugins.groovy
import jenkins.model.*

def plugins = Jenkins.instance.pluginManager.plugins.findAll {
    it -> it.hasUpdate()
}.collect {
    it -> it.getShortName()
}

Jenkins.instance.pluginManager.install(plugins, false).each{ it -> it.get() }

Jenkins.instance.safeRestart()

Firstly, we need to import the jenkins.model package using import jenkins.model.*.

Secondly, we find all the plugins with newer versions using Jenkins.instance.pluginManager.plugins.findAll { it -> it.hasUpdate() }. It returns a Groovy List. Then, we obtain another list by invoking the collect() method of this list using collect{ it -> it.getShortName() }. After that, we assign the returned list by collect() to the plugins variable, which contains the name of the plugins to be updated.

Then, calling Jenkins.instance.pluginManager.install(plugins, false).each{ it -> it.get() } updates the plugins stored in the plugins variable one after the other.

Finally, we restart Jenkins using Jenkins.instance.safeRestart().

Let’s list the plugins needing update after running the script:

$ java -jar jenkins-cli.jar -s http://localhost:8080 -auth admin:admin_secret_password list-plugins | grep ")$" | awk '{print $1}'

As expected, everything is up to date.

5. Conclusion

In this article, we discussed how to update Jenkins plugins from the command line. Firstly, we learned that the Jenkins CLI allowed users to access Jenkins from the command line. Therefore, it’s useful for automating tasks, updates, and troubleshooting using scripts.

Then, we saw that we could use the Jenkins CLI’s install-plugin command as the first option. We obtained the list of plugins needing update by filtering the output of the list-plugins command. We can automate plugin updates using a script in this option.

Finally, we learned that the Jenkins CLI’s groovy command was the second alternative. We used a Groovy script to update the plugins. Therefore, we can automate plugin updates by running the Groovy script from a shell script.