Created
October 30, 2022 10:27
-
-
Save anton-yurchenko/f038ab6f683c69a46122577af7711f12 to your computer and use it in GitHub Desktop.
Jenkins Console Groovy Script to Generate New User Token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://docs.cloudbees.com/docs/cloudbees-ci-kb/latest/client-and-managed-masters/how-to-generate-change-an-apitoken | |
import hudson.model.* | |
import jenkins.model.* | |
import jenkins.security.* | |
import jenkins.security.apitoken.* | |
// script parameters | |
def userName = 'admin' | |
def tokenName = 'token' | |
def user = User.get(userName, false) | |
def apiTokenProperty = user.getProperty(ApiTokenProperty.class) | |
def result = apiTokenProperty.tokenStore.generateNewToken(tokenName) | |
user.save() | |
return result.plainValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment