Skip to content

Instantly share code, notes, and snippets.

@gwsu2008
Created April 23, 2020 19:29
Show Gist options
  • Save gwsu2008/eadaba556e2700751397030ba0a09705 to your computer and use it in GitHub Desktop.
Save gwsu2008/eadaba556e2700751397030ba0a09705 to your computer and use it in GitHub Desktop.
jenkins-groovy-create-user
import hudson.model.*
import hudson.security.*
import hudson.tasks.Mailer
def userId = 'tester1'
def password = 'password!'
def email = '[email protected]'
def fullName = 'tester1 tester2'
def instance = jenkins.model.Jenkins.instance
def existingUser = instance.securityRealm.allUsers.find {it.id == userId}
if (existingUser == null) {
println("Create user ${userId} in jenkins")
def user = instance.securityRealm.createAccount(userId, password)
user.addProperty(new Mailer.UserProperty(email));
user.setFullName(fullName);
} else {
println("User ${userId} is already exists")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment