Skip to content

Instantly share code, notes, and snippets.

@RahulMahale
Forked from hayderimran7/groovy-create-user.md
Created August 6, 2021 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RahulMahale/7d6c34a59369e850bfb355ac72e9da83 to your computer and use it in GitHub Desktop.
Save RahulMahale/7d6c34a59369e850bfb355ac72e9da83 to your computer and use it in GitHub Desktop.
Jenkins Groovy enable security and create a user in groovy script

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment