20 May 2018

How to preinstall plugins on Jenkins

If you ever wonder how to have your Jenkins server preconfigurated with your plugins before you even access to it by first time, let me show you how can you do it.

First you’ll need to create a directory called init.groovy.d at the Jenkins root path, this folder will run every script that you put inside it on every restart; then you’ll need to create a groovy file with your code. Ex. plugins-add.groovy

The plugins list (pluginParameter) will be separated by spaces or the delimiter you prefer.

import jenkins.model.*
import java.util.logging.Logger
def logger = Logger.getLogger("")
def installed = false
def initialized = false
def pluginParameter=" github maven-plugin job-dsl github-api.1.90 workflow-aggregator pipeline-utility-steps antisamy-markup-formatter ssh-agent startup-trigger-plugin pipeline-github dependency-check-jenkins-plugin"
def plugins = pluginParameter.split()
logger.info("" + plugins)
def instance = Jenkins.getInstance()
def pm = instance.getPluginManager()
def uc = instance.getUpdateCenter()
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)
    	def installFuture = plugin.deploy()
      while(!installFuture.isDone()) {
        logger.info("Waiting for plugin install: " + it)
        sleep(3000)
      }
      installed = true
    }
  }
}
if (installed) {
  logger.info("Plugins installed, initializing a restart!")
  instance.save()
  instance.restart()
}

Tags: