Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 750
shubham Avatar asked Jul 18 '13 06:07

shubham


People also ask

How do I interact with Jenkins API?

Using Curl If your Jenkins server requires authentication (and it SHOULD), you'll see a message saying "Authentication Required". The Jenkins API uses HTTP BASIC authentication and requires a username as well as an API token to connect. Then click the box named "Show API Token", and copy the token to your clipboard.

Does Jenkins have an API?

Jenkins provides machine-consumable remote access API to its functionalities. Currently it comes in three flavors: XML. JSON with JSONP support.


2 Answers

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.

like image 60
izzekil Avatar answered Sep 30 '22 18:09

izzekil


This is how to create user after installation:

echo 'jenkins.model.Jenkins.instance.securityRealm.createAccount("user", "password")' | java -jar jenkins-cli.jar -auth admin:c3a5dcd6bc3f45ee8d6c9f0f5abc14c0 -s http://localhost:8080/ groovy =

Where c3a5dcd6bc3f45ee8d6c9f0f5abc14c0 is automatically generated password present in log or in file (for ubuntu): /var/lib/jenkins/secrets/initialAdminPassword

like image 21
vitaleek Avatar answered Sep 30 '22 20:09

vitaleek