如何获取Jenkins的API令牌

Luk*_*101 72 jenkins

我正在尝试使用jenkins rest api.在说明中它说我需要有api密钥.我查看了整个配置页面以找到它.我如何获得詹金斯的API密钥?

Bri*_*ker 117

自Jenkins 2.129以来,API令牌配置已更改:

您现在可以拥有多个令牌并为其命名.它们可以单独撤销.

  1. 登录Jenkins.
  2. 单击您的名称(右上角).
  3. 单击" 配置"(左侧菜单).
  4. 使用"添加新令牌"按钮生成一个新按钮然后命名.
  5. 您必须在生成令牌时复制令牌,因为之后您无法查看令牌.
  6. 不再需要时撤销旧令牌.

在Jenkins 2.129之前:显示API令牌如下:

  1. 登录Jenkins.
  2. 单击您的姓名(右上角).
  3. 单击" 配置"(左侧菜单).
  4. 单击" 显示API令牌".

API令牌已显示.

您可以通过单击" 更改API令牌"按钮来更改令牌.

  • 这个令牌会过期吗? (4认同)
  • 有没有API可以做到这一点? (2认同)

RaG*_*aGe 13

进行Jenkins 2.129发布的非UI方法是:

curl 'https://<jenkinsURL>/me/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken' \
--data 'newTokenName=foo' \
--user username:Password
Run Code Online (Sandbox Code Playgroud)

返回:

{
  "status": "ok",
  "data": {
    "tokenName": "foo",
    "tokenUuid": "<uuid>",
    "tokenValue": "<redacted>"
  }
}
Run Code Online (Sandbox Code Playgroud)

詹金斯2.129

curl http://<username>:<password>@<jenkins-url>/me/configure 
Run Code Online (Sandbox Code Playgroud)

  • 您需要在curl请求中包含CSRF crumb(https://wiki.jenkins.io/display/JENKINS/Remote+access+API),否则它将失败并显示403:Forbidden。 (2认同)
  • @SibiCoder 尝试将“@”替换为“%40” (2认同)

Car*_*as 6

在 Jenkins 2.225 中测试

经过几个小时的研究,我找到了答案:

使用 Api 令牌代替 CSFR 令牌。但是,如果您想从任何其他客户端(POSTMAN、CLI、curl 等)进行身份验证,会发生什么情况。

首先,您需要获取 CSFR 令牌并将信息保存在 cookie 中 --cookie-jar

  • 要求

curl -s --cookie-jar /tmp/cookies -u 用户名:密码 http://localhost:8080/crumbIssuer/api/json

  • 回复

{“_class”:“hudson.security.csrf.DefaultCrumbIssuer”,“crumb”:“bc92944100d12780cfc251c9255f3f323a475562b4ee0d8b9cc6e4121f50a450”,“JenkinsbRequest”

然后我们可以读取 cookie--cookie并生成新的令牌:

  • 要求

curl -X POST -H 'Jenkins-Crumb:your_crumb_token_generated_above' --cookie /tmp/cookies http://localhost:8080/me/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken?newTokenName=\your_token_name -u username:password

  • 回复

{ "status": "ok", "data": { "tokenName": "my android token", "tokenUuid": "c510e26c-b2e8-4021-bf79-81d1e4c112af", "tokenValue": "11a2a0c91913d187c14d5e87c14d


ycr*_*ycr 5

如何生成 Jenkins API 令牌

以下命令需要curl和jq。在同一个会话中执行它们。

# Change the following appropriately
JENKINS_URL="http://localhost:8080"
JENKINS_USER=admin
JENKINS_USER_PASS=admin
Run Code Online (Sandbox Code Playgroud)

获取面包屑

# Change the following appropriately
JENKINS_URL="http://localhost:8080"
JENKINS_USER=admin
JENKINS_USER_PASS=admin
Run Code Online (Sandbox Code Playgroud)

获取访问令牌

JENKINS_CRUMB=$(curl -u "$JENKINS_USER:$JENKINS_USER_PASS" -s --cookie-jar /tmp/cookies $JENKINS_URL'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
Run Code Online (Sandbox Code Playgroud)

连续API调用

您需要使用带有用户名的令牌以及生成的面包屑,而不是密码。

ACCESS_TOKEN=$(curl -u "$JENKINS_USER:$JENKINS_USER_PASS" -H $JENKINS_CRUMB -s \
                    --cookie /tmp/cookies $JENKINS_URL'/me/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken' \
                    --data 'newTokenName=GlobalToken' | jq -r '.data.tokenValue')
Run Code Online (Sandbox Code Playgroud)