Automatically setup jenkins users with CLI
Asked Answered
B

3

9

I did not find any reference to user related commands for the jenkins-cli tool.

I need this to automate deployment.

Any comeback?

Brandie answered 8/4, 2012 at 21:9 Comment(0)
T
3

Maybe you don't want to use Jenkins' internal user database at all. There are a host of "Authentication and User Management" plugins.

If you like MySQL, there is a MySQL authenticator (it reads a table of users and passwords), and your "adduser" command could do an insert on that table.

If you like flat files, there is a "Script Security Realm", where you can authenticate with an arbitrary script. Write a file with user and password combos in your favorite format, write an "adduser" script that writes to it, and write an auth script that reads the file and determines whether to authenticate the user.

You can also hook up to an LDAP server, Active Directory, Atlassian Crowd, Unix user accounts (pw_auth), or whatever authentication your application server uses (if it's running off of a Tomcat server, for instance, you can tell Jenkins to let Tomcat authenticate users, and set up Tomcat to do it however you want.

Tenfold answered 9/4, 2012 at 17:21 Comment(0)
I
6

To use jenkins internal database simply use the following command.

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

This will create user=user1 with password=password123


If you have any existing user and have restricted anonymous access to your jenkins, you can specify the username and password with

--username "user_name" and --password "password"

Infirmary answered 5/10, 2015 at 10:22 Comment(3)
I can't make it work with --username and --password. Can you leave some example?Cruise
echo 'jenkins.model.Jenkins.instance.securityRealm.createAccount("user1", "password123")' | java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s "localhost:8080" -noKeyAuth groovy = --username admin --password xxxMonogamy
just for the record, for me this didnt work with jenkins ver. 2.138.2. instead for me worked: echo 'jenkins.model.Jenkins.instance.securityRealm.createAccount("newuser", "newpassword")' | sudo java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s "http://localhost:8080" -auth adminuser:adminpassword -noKeyAuth groovy =Brookins
S
3

If you specify in more detail what you are trying to do people here may help you better. That said, here are some pointers:

All CLI commands are available via http://[jenkins-server]/cli. What's not found there is not available via CLI. You can specify user name / password via --username and --password (or --password-file) options in CLI commands.

Another option for Jenkins automation is to use Python JenkinsAPI.

You can also use tools like wget and curl to perform certain actions (such as starting a build). There you may use user-specific tokens instead of username/password.

Here's another link that can be helpful.

Surfbird answered 9/4, 2012 at 16:30 Comment(0)
T
3

Maybe you don't want to use Jenkins' internal user database at all. There are a host of "Authentication and User Management" plugins.

If you like MySQL, there is a MySQL authenticator (it reads a table of users and passwords), and your "adduser" command could do an insert on that table.

If you like flat files, there is a "Script Security Realm", where you can authenticate with an arbitrary script. Write a file with user and password combos in your favorite format, write an "adduser" script that writes to it, and write an auth script that reads the file and determines whether to authenticate the user.

You can also hook up to an LDAP server, Active Directory, Atlassian Crowd, Unix user accounts (pw_auth), or whatever authentication your application server uses (if it's running off of a Tomcat server, for instance, you can tell Jenkins to let Tomcat authenticate users, and set up Tomcat to do it however you want.

Tenfold answered 9/4, 2012 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.