Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattr-/de6a9638fc6657538a45326d7ea69d66 to your computer and use it in GitHub Desktop.
Save mattr-/de6a9638fc6657538a45326d7ea69d66 to your computer and use it in GitHub Desktop.
jenkins groovy get API token of a user

This is pretty simple snippet to get API Token of a user , lets say "MYUser" in jenkins.
its pretty useful when you are working with 'jenkins-job-builder' to update jobs in jenkins, and you need to get the api token which JJB needs inorder to update jobs to ..
run this code in either jenkins script console , or as i prefer, in form of init.groovy.
so when jenkins master starts, i create a user for JJb.
after that i get the token right away and pass it to my JJB jobs folder to file 'jenkins_jobs.ini' :)_ .

import jenkins.security.*       
User u = User.get("Myuser")  
ApiTokenProperty t = u.getProperty(ApiTokenProperty.class)  
def token = t.getApiTokenInsecure()
println "token is $token "
@jfisherith
Copy link

This only works with legacy tokens it seems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment