jenkinshudson

Creating user in Jenkins via API


I was wondering if I can create a new user in Jenkins using its API. I can create jobs but the API docs for Jenkins don't have anything related to user creation.

Actually, I have to create a new user followed by creating a new job for that user, all of this using an API.


Solution

  • You're right, there is no explicit CLI command for adding a user. But you could use groovy script for this (using the CLI for execution).

    The details depend on how your Jenkins is configured. For example, if your Jenkins uses its own user database, then you could add a new user by the following CLI call:

    echo 'jenkins.model.Jenkins.instance.securityRealm.createAccount("user1", "password123")' |
    java -jar jenkins-cli.jar -s http://localhost/ groovy =
    

    This shell command will create a new user with login "user1" and password "password123". Here echo feeds a line of groovy code to a Jenkins CLI (note that = means that CLI should receive code from STDIN).

    Also groovy script allows to manage user permissions, however, the exact code depends on what authorization strategy is used. You could use this sample script as a start.