Skip to content

Instantly share code, notes, and snippets.

@bkruger99
Created September 4, 2018 19:49
Show Gist options
  • Save bkruger99/f810101853f2dfca64c5c753dde07824 to your computer and use it in GitHub Desktop.
Save bkruger99/f810101853f2dfca64c5c753dde07824 to your computer and use it in GitHub Desktop.
Generate a user token for the new jenkins subsystem (works in 2.139+)
import jenkins.security.*
import jenkins.security.apitoken.*
def username = '<pre-existing user goes here>'
def token = '<what you want to name your token>'
User u = User.get(username)
ApiTokenProperty p = u.getProperty(ApiTokenProperty.class);
ApiTokenStore.TokenUuidAndPlainValue tokenUuidAndPlainValue = p.tokenStore.generateNewToken(token);
u.save()
// Here's the token output value.
println tokenUuidAndPlainValue.plainValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment