Jenkins Authentication Token-Generate Jenkins Rest Api Token

Jenkins Authentication Token-Generate Jenkins Api Token:

Jenkins authentication token is used to access Jenkins remotely. In the following steps i will show you how to generate Jenkins authentication token or Jenkins API token.

We can generate Jenkins authentication token in two ways

  • one is from dashboard
  • another one is using rest api.

Generate Jenkins Authentication Token From dashboard

go to manage Jenkins=>manage users=> here you can see all the users list. In the image you can see i have devops user in my jenkins server. Now i will generate Jenkins authentication token for devops user.

Jenkins REST Api

Right side you can see gear symbol click on this symbol it will redirect to the user configuration page. In this page we can generate jenkins api token. In API token section click on add new token.

build jenkins job using the REST API

 

jenkins rest api authentication

give any name to token here i given token name as my-token. That's it we have successfully generated jenkins api token for devops user.

Generate Jenkins Authentication Token Using Rest Api

we can generate Jenkins Authentication Token Using Rest Api. In the following steps i will show you how to Jenkins Authentication Token Using Rest Api.

Get a Crumb

When you are using the Jenkins API to make changes (POST/PUT/DELETE), Jenkins requires a crumb in addition to an API token. To prevent CSRF attacks we use this crumb. Using below commands you can generate crumb.

curl -s -u username:password   http://3.85.63.219:8080/crumbIssuer/api/json

Output:

 curl -s -u devops:dev123 http://3.85.63.219:8080/crumbIssuer/api/json

{
"_class":"hudson.security.csrf.DefaultCrumbIssuer",
"crumb":"03a97f9f8d0071148f82da4d057790f9",
"crumbRequestField":"Jenkins-Crumb"
}

here crumb value is  03a97f9f8d0071148f82da4d057790f9

Generate Jenkins Authentication Token

using below command we can generate jenkins api token.

curl 'http://3.85.63.219:8080/me/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken' --data 'newTokenName=my-second-token' --user devops:dev123  -H 'Jenkins-Crumb:03a97f9f8d0071148f82da4d057790f9'

replace all parameters values with your server values.

Output:

DELL@DELL-PC MINGW64 /
$ curl 'http://3.85.63.219:8080/me/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken' --data 'newTokenName=my-second-token' --user devops:dev123 -H 'Jenkins-Crumb:03a97f9f8d0071148f82da4d057790f9'

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   183  100   155  100    28    268     48 --:--:-- --:--:-- --:--:--   326
{
"status":"ok",
"data":{"tokenName":"my-second-token",
"tokenUuid":"8b080b3f-7313-4390-a59a-7a038e87a66f",
"tokenValue":"110b4250c15f78d6bd26c9b27a7d36efaa"
}}

DELL@DELL-PC MINGW64 /
$

token value is Jenkins Authentication Token.  finally we got Jenkins Authentication Token or Jenkins rest Api Token.

Leave a Reply

Your email address will not be published. Required fields are marked *