Going to try this…
https://gist.github.com/hayderimran7/50cb1244cc1e856873a4
This is a snippet that will create a new user in jenkins and if security has been disabled , it will enable it 🙂
import jenkins.model.*
import hudson.security.*
def instance = Jenkins.getInstance()
def hudsonRealm = new HudsonPrivateSecurityRealm(false)
hudsonRealm.createAccount("MyUSERNAME","MyPASSWORD")
instance.setSecurityRealm(hudsonRealm)
instance.save()
Bonus: Add the created user as admin for jenkins: what if you want that user to be like admin of jenkins who can access anything.. no problem..just add following lines right above the ‘instance.save()’ statement and run 🙂
def strategy = new GlobalMatrixAuthorizationStrategy()
strategy.add(Jenkins.ADMINISTER, "myUSERNAME")
instance.setAuthorizationStrategy(strategy)