20 May 2018

How to preconfigure user on Jenkins

If you want to have preconfigurated your admin user at Jenkins and also your path to your maven and JDK you could use the next groovy script that will be placed at init.groovy.d


import hudson.security.*
import jenkins.model.*

def instance = Jenkins.getInstance()

def desc = instance.getDescriptor("hudson.tasks.Maven")
def minst =  new hudson.tasks.Maven.MavenInstallation("maven353", "/usr/local/maven");
desc.setInstallations(minst)
desc.save()


def dis = new hudson.model.JDK.DescriptorImpl();
dis.setInstallations( new hudson.model.JDK("jdk8", "/usr/java/jdk1.8.0_171-amd64"));
dis.save();

def hudsonRealm = new HudsonPrivateSecurityRealm(false)
hudsonRealm.createAccount('admin', 'admin')
instance.setSecurityRealm(hudsonRealm)

def strategy = new FullControlOnceLoggedInAuthorizationStrategy()
instance.setAuthorizationStrategy(strategy)
instance.save()

Also if you want to override the Wizard Plugins screen you should add this 2 files at your Jenkins root path with the Jenkins version you are using.

Files

  • jenkins.install.InstallUtil.lastExecVersion
  • jenkins.install.UpgradeWizard.state

Content example

 2.122

Tags: