Created
February 28, 2022 11:06
-
-
Save lwille/fad2548d66d5a47de56587f024e51457 to your computer and use it in GitHub Desktop.
Jenkins: generate API token for another user
This file contains 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
import hudson.model.* | |
import jenkins.model.* | |
import jenkins.security.* | |
import jenkins.security.apitoken.* | |
def user = User.get("jane-doe", false) | |
def prop = user.getProperty(ApiTokenProperty.class) | |
def forceRecreate = false | |
if(!prop || forceRecreate) { | |
def result = prop.tokenStore.generateNewToken("generated-by-${User.current()}" | |
) | |
user.save() | |
print(result.plainValue) | |
} else { | |
print("A token already exists; set `forceRecreate` to create a new one. Old tokens are not invalidated by this.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment