2

To create a new user in Jenkins, admin needs to provide a username, emialID and password. Being an admin, is there a way to add a large number of users to Jenkins at a time by providing their username as their mail id, display name as their name and a common password*?
*Assuming that password will be reset at the time of each user logging in

Alla Sasikanth
  • 541
  • 3
  • 7
  • 22

3 Answers3

1

I am using the below groovy script to add a user to Jenkins and provide only build permission.

import hudson.model.*
import hudson.security.*
import hudson.tasks.Mailer

def userId = args[0]
def password = args[1]
def email = args[2]
def fullName= args[3]
def instance = jenkins.model.Jenkins.instance
def existingUser = instance.securityRealm.allUsers.find {it.id == userId}

if (existingUser == null) {
    def user = instance.securityRealm.createAccount(userId, password)
    user.addProperty(new Mailer.UserProperty(email));
    user.setFullName(fullName)

    def strategy = (GlobalMatrixAuthorizationStrategy) 
    instance.getAuthorizationStrategy()
    strategy.add(hudson.model.Item.BUILD,userId)
    instance.setAuthorizationStrategy(strategy)
    instance.save()
}

The script is invoked using jenkins-cli.

Upen
  • 1,388
  • 1
  • 22
  • 49
0

It is easier to connect Jenkins to LDAP. See this plugin here

Hasson
  • 1,894
  • 1
  • 21
  • 25
0

looks like the Jenkins cli doesn't support add users , but check this one - using groovy script you can do it.

Creating user in Jenkins via API

if you want give specific permissions per job , maybe you can use the CLI get-job & update-job commands.

Or you can try check this one - Jenkins Add permissions to jobs using groovy it discuses almost the same ...

Community
  • 1
  • 1
Mor Lajb
  • 2,546
  • 1
  • 15
  • 28
  • Thanks for the response @Mor Lajb. Adding users is fine.Is there a way to set permissions for each user/group at job level using this groovy scripting? – Alla Sasikanth Jan 18 '17 at 07:35