通过 API 在 Jenkins 中创建用户 [英] Creating user in Jenkins via API

查看:72
本文介绍了通过 API 在 Jenkins 中创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用 Jenkins 的 API 在 Jenkins 中创建一个新用户.我可以创建作业,但 Jenkins 的 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.

实际上,我必须创建一个新用户,然后为该用户创建一个新作业,所有这些都使用 API.

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

推荐答案

没错,没有用于添加用户的显式 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.

这篇关于通过 API 在 Jenkins 中创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆