且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过 API 在 Jenkins 中创建用户

更新时间:2023-12-04 08:54:16

没错,没有用于添加用户的显式 CLI 命令.但是您可以为此使用 groovy 脚本(使用 CLI 执行).

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).

详细信息取决于您的 Jenkins 的配置方式.例如,如果您的 Jenkins 使用自己的用户数据库,那么您可以通过以下 CLI 调用添加一个新用户:

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 =

这个shell命令将创建一个新用户,登录名user1"和密码password123".此处echo 将一行常规代码提供给 Jenkins CLI(注意 = 表示 CLI 应从 STDIN 接收代码).

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).

还有 groovy 脚本允许管理用户权限,但是,确切的代码取决于使用的授权策略.您可以使用 这个示例脚本 作为开始.

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.