29

Is there any option to install jenkins plugins from command line ?

I found a command for this after a bit google search :

java -jar /var/lib/jenkins/jenkins.war -s http://127.0.0.1:8080/ install-plugin ${Plugin_Name}

But it's not working.

Arun Ramachandran
  • 1,270
  • 5
  • 23
  • 36
  • 1
    Although answering 3 years late, I still think "not working" does not contribute to any knowledge towards the solution. Please provide the error info with inputs. This might help any seeker to know if inputs params, and error responses are matching his/her criteria. – ashutosh Oct 01 '19 at 12:26

7 Answers7

45

As per the Jenkins command line interface documentation, you need to use the client JAR file (not the server WAR file you're using), which you can obtain directly from Jenkins, e.g. via the links on http://localhost:8080/cli

Then you can run the command using this JAR:

java -jar jenkins-cli.jar -s http://127.0.0.1:8080/ install-plugin <name>

This will download install the plugin you want, along with any of its dependencies.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • 7
    If you have run jenkins from a war file then the jenkins-cli.jar will most likely have been unpacked and cached here: /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar – JamesP Jul 07 '16 at 12:27
  • I didn't find it there, but it was in `/var/jenkins_home/war/WEB-INF/jenkins-cli.jar`. – David Moles Oct 15 '18 at 21:27
  • 7
    Thanks for this. For anyone having issues running the command and getting this error: `ERROR: anonymous is missing the Overall/Read permission` you'll need to provide the `-auth` command switch like so: `java -jar jenkins-cli.jar -s http://localhost:8080 -auth : install-plugin ...` and, also, make sure that the `-auth :` portion comes before the `install-plugin` argument. In other words, the order of the arguments matters. It goes without saying that the Jenkins user you are using must have sufficient privileges. – racl101 Oct 31 '18 at 22:02
  • I am using a bootstrap script in EC2 instance to use Jenkins CLI. Once I finish installing Jenkins I run "sudo wget http://127.0.0.1:8080/jenkins/jnlpJars/jenkins-cli.jar" to get CLI but I am receiving 503 Service not available error for a specific time. I can not use loop to wait for the service to be available. Is there any nice way to do this in EC2 Bootstrap script? – Babar Baig Dec 21 '19 at 14:09
7
import jenkins.model.* 
import java.util.logging.Logger

def logger = Logger.getLogger("") 
def installed = false 
def initialized = false

def pluginParameter="gitlab-plugin hipchat swarm" 
def plugins =pluginParameter.split() 
logger.info("" + plugins) 
def instance =Jenkins.getInstance() 
def pm = instance.getPluginManager() 
def uc =instance.getUpdateCenter() 
uc.updateAllSites()

plugins.each {   logger.info("Checking " + it)   if
(!pm.getPlugin(it)) {
    logger.info("Looking UpdateCenter for " + it)
    if (!initialized) {
      uc.updateAllSites()
      initialized = true
    }
    def plugin = uc.getPlugin(it)
    if (plugin) {
      logger.info("Installing " + it)
        plugin.deploy()
      installed = true
    }   } }

if (installed) 
   {  
      logger.info("Plugins installed, initializing a   restart!")   
       instance.save()  
       instance.doSafeRestart()
 }
Ijaz Ahmad
  • 11,198
  • 9
  • 53
  • 73
  • 2
    Why are you calling `uc.updateAllSites()` more than once? Is it intentional? – giorgian Apr 20 '17 at 08:24
  • what if the plugin is not successfully installed, how do you retry? – SSF Sep 26 '19 at 00:08
  • Ok I take that back. It seems to retry. But it seems that it goes into a loop of endlessly restarting. Any idea why @giorgian – SSF Sep 26 '19 at 00:52
  • I should add that I put this in the 'init.groovy.d' directory so that installation happens during the bootup. How do I install the plugins using the above and avoid going into this loop? – SSF Sep 26 '19 at 02:20
  • The loop appears because plugin.deploy() actually returns Future. Because of that jenkins restarts before installation is complete. And after next start the same thing happens. So you need to wait until the plugin is installed. Instead of plugin.deploy() it should be plugin.deploy().get() for example – Ilya Sazonov May 08 '21 at 21:31
6

Since Sept 2019, you can also use the Plugin Installation Manager Tool. It takes a yaml file listing the plugins, and downloads plugins to a folder of your choice. There is no need to have a running Jenkins instance. You can specify the plugin versions in the yaml file, but be aware of JENKINS-60205.

Get the plugin manager:

version=1.0.1
curl \
    -L \
    -X GET "https://github.com/jenkinsci/plugin-installation-manager-tool/releases/download/plugin-management-parent-pom-$version/jenkins-plugin-manager-$version.jar" \
    -o jenkins-plugin-manager-$version.jar

Download the plugins:

java -jar jenkins-plugin-manager-$version.jar \
    --plugin-download-directory pluginsFolder \
    --plugin-file plugins.yml \
    --war jenkins.war

The plugins are in the pluginsFolder.

user1241663
  • 191
  • 1
  • 11
2

You will need jenkins-cli.jar file. It can be downloaded from ${JENKINSURL}/jnlpJars/jenkins-cli.jar

wget http://localhost:8080/jnlpJars/jenkins-cli.jar

To install a plugin :

java -jar jenkins-cli.jar -auth ${USER}:${PASSWORD} -s ${JENKINSURL} install-plugin ${PLUGIN_NAME}

If you dont provide auth you will get error:

ERROR: anonymous is missing the Overall/Read permission

2

My command:

jenkins-plugin-cli --plugins "snyk-security-scanner:2.12.1 file-operations:1.11 branch-api:2.5.6 pipeline-build-step:2.13 workflow-support:3.8 aws-credentials:1.28 subversion:2.13.1 github-branch-source:2.7.1 publish-over-ftp:1.15 cloudbees-folder:6.740.ve4f4ffa_dea_54"

And the one I used in dockerfile

FROM jenkins/jenkins:lts

USER root

# Install custom plugins
RUN jenkins-plugin-cli --plugins "snyk-security-scanner:2.12.1 file-operations:1.11 branch-api:2.5.6 pipeline-build-step:2.13 workflow-support:3.8 aws-credentials:1.28 subversion:2.13.1 github-branch-source:2.7.1 publish-over-ftp:1.15 cloudbees-folder:6.740.ve4f4ffa_dea_54"

# Set environment variables
# ENV <ENV_VARIABLE_1> <VALUE>
# ENV <ENV_VARIABLE_2> <VALUE>

USER jenkins
encrypted name
  • 149
  • 3
  • 17
0

If you don't find some straight forward command for installing plugins. Please take a look at this link: How to install a plugin in Jenkins manually?

But it needs to download the plugin first(*.hpi file) and run it manually.

Community
  • 1
  • 1
aldrien.h
  • 3,437
  • 2
  • 30
  • 52
0

This is my soluction!

version: '3'
services:
  jenkins:
    image: jenkins/jenkins:latest
    restart: always
    ports:
      - "8080:8080"
      - "50000:50000"
    environment:
      JAVA_OPTS: "-Djenkins.install.runSetupWizard=false"
    volumes:
      - ${PWD}/plugins.txt:/usr/share/jenkins/ref/plugins.txt
    command: >
        bash -c "jenkins-plugin-cli -f /usr/share/jenkins/ref/plugins.txt
        && ./usr/local/bin/jenkins.sh"

Contents of plugins.txt:

ant:latest
antisamy-markup-formatter:latest
build-timeout:latest
cloudbees-folder:latest
configuration-as-code:latest
credentials-binding:latest
email-ext:latest
git:latest
github-branch-source:latest
gradle:latest
ldap:latest
mailer:latest
matrix-auth:latest
pam-auth:latest
pipeline-github-lib:latest
pipeline-stage-view:latest
ssh-slaves:latest
timestamper:latest
workflow-aggregator:latest
ws-cleanup:latest
job-dsl:latest
blueocean:latest
authorize-project:latest
psicopante
  • 353
  • 3
  • 5